# Not able to connect to Oracle server from shell script

**URL:** <https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066>\
**Category:** Shell Programming and Scripting\
**Created:** [November 2, 2018, 11:45am UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066 "2018-11-02T11:45:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuvi](https://community.unix.com/letter_avatar/yuvi/32/6_449c533f586939b6e5f246632a46421f.png) [@yuvi](https://community.unix.com/u/yuvi)\
**Post date:** [November 2, 2018, 11:45am UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/1 "2018-11-02T11:45:58Z")

</div>

Hi,

I am facing an issue in connecting to database from Linux shell script. Here is my code

```nohighlight
export ORACLE_BASE=xxx
export ORACLE_SID=xxx
export ORACLE_HOME=xxxx

DB_PATH=xxx/'yyy@yy'

UserId=`sqlplus -s $DB_PATH <<EOF
set head off feedback off
select max(USER_ID)+1 from USERS;
exit;
EOF`
echo "User ID is - $UserId"

```

When I run the script, I am getting the below error.

```nohighlight
User ID is - ERROR:
ORA-12545: Connect failed because target host or object does not exist

SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

```

I tried with ping and telnet to the DB Server from Linux server then connection was success.  
I verified tnsnames.ora and tried by giving IP address of host etc but did not work. Can anyone please let me know any solution for this?

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 2, 2018, 12:00pm UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/2 "2018-11-02T12:00:44Z")

</div>

Have you tried: `tnsping ${ORACLE_SID}` ?  
Also:

```nohighlight
UserId=$(sqlplus -s $DB_PATH <<EOF 
set head off feedback off 
select max(USER_ID)+1 from USERS;
exit; 
EOF
)

```

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [November 2, 2018, 12:00pm UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/3 "2018-11-02T12:00:46Z")

</div>

How can you connect to a database without a userid and password for the database?

Your script does not seem to perform the basic connection call with the right parameters; and that is also what the error message indicates.

---

<div class="post-metadata">

**Author:** ![yuvi](https://community.unix.com/letter_avatar/yuvi/32/6_449c533f586939b6e5f246632a46421f.png) [@yuvi](https://community.unix.com/u/yuvi)\
**Post date:** [November 2, 2018, 12:40pm UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/4 "2018-11-02T12:40:12Z")

</div>

Thanks for your suggestions.  
@vgersh99  
Hi, I have tried with the code provided by you but same error is coming. I also tried to tnsping but getting the below error.  
-bash: tnsping: command not found.  
Please suggest.  
@Neo  
Hi, I have given userid and password in the variable DB\_PATH and used the same. Since there are special characters in the password I enclosed in parenthesis.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 2, 2018, 12:51pm UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/5 "2018-11-02T12:51:38Z")

</div>

`tnsping` lives in the same path as `sqlplus`

---

<div class="post-metadata">

**Author:** ![yuvi](https://community.unix.com/letter_avatar/yuvi/32/6_449c533f586939b6e5f246632a46421f.png) [@yuvi](https://community.unix.com/u/yuvi)\
**Post date:** [November 2, 2018, 1:20pm UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/6 "2018-11-02T13:20:16Z")

</div>

I tried from oracle home directory and getting the below error.

```nohighlight
TNS Ping Utility for Linux: Version 11.2.0.3.0 

Used parameter files:
/opt/app/oracle/product/11.2.0/network/admin/sqlnet.ora
TNS-03505: Failed to resolve name

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 2, 2018, 1:58pm UTC](https://community.unix.com/t/not-able-to-connect-to-oracle-server-from-shell-script/374066/7 "2018-11-02T13:58:21Z")

</div>

> [@yuvi](#):
>
> I tried from oracle home directory and getting the below error.
> 
> ```plaintext
> TNS Ping Utility for Linux: Version 11.2.0.3.0 
> 
> Used parameter files:
> /opt/app/oracle/product/11.2.0/network/admin/sqlnet.ora
> TNS-03505: Failed to resolve name
> 
> ```

there you have it - your `sqlnet.ora` is not configured (correctly?) for the SID you're trying to `tnsping` .
