Passing variable user,pass,sid in sqlplus comman

两盒软妹~` 提交于 2019-12-11 19:26:37

问题


I want to pass variables in sqlplus command in my bash script, answer might be given but doesn't work for me.

I tried some thing like this

#!/bin/bash
ssh oracle@10.116.12.26 <<XX
echo Please enter an Oracle Username:
read USERNAME

echo "Please enter the Oracle Username's Password:"
read -s PASS 

SID=XE

export conn_str=$USERNAME/$PASS@$SID

sqlplus $conn_str  << EOF
select * FROM tabs;

exit
EOF
XX

also tried

sqlplus $USERNAME/$PASS@SID            #with option -s  and -l

I also find solution like this but not worked for me.

oracle : 11g ,os : fedora 18

Is there any solution available for this ? thanks.


回答1:


You haven't said what actually happens, but I'm guessing you aren't prompted for the credentials, and that maybe it can't find sqlplus. On a Red Hat box this works:

#!/bin/bash
echo Please enter an Oracle Username:
read USERNAME
echo "Please enter the Oracle Username's Password:"
read -s PASS
SID=XE
conn_str=$USERNAME/$PASS@$SID

ssh oracle@10.116.12.26 << EOF
# set these for your specific environment
ORACLE_HOME=<path to ORACLE_HOME>
PATH=$PATH:$ORACLE_HOME/bin # or without .../bin depending on client
TNS_ADMIN=<path to tnsnames.ora directory, if not default>

sqlplus -s /nolog
connect $conn_str
select * FROM user_tables;
exit
EOF

This is collecting the values from the user on the local machine to avoid the 'Pseudo-terminal will not be allocated because stdin is not a terminal' issue.

It is then setting up the Oracle environment once on the remote server - what you need to set depends on which client you're using (particularly whether you're using the instant client, though if you're connecting as oracle that seems unlikely, and you can probably run oraenv).

I've also tweaked it to run SQL*Plus with /nolog and then connect once running, so the credentials aren't exposed in the ps output. And I've switched from the old tabs to the more common user_tables.

The nested heredocs work but aren't necessary; the SQL commands are already being entered in the right place and will be seen by SQL*Plus rather than the remote shell.

Of course, since I don't know what error you were actually seeing this is largely speculation. It would probably be simpler to have the client installed locally and use a SQL*Net connection rather than SSH, but there could be firewall restrictions we don't know about.




回答2:


Try quoting your here doc delimiter to prevent stale variable expansions on the local machine.

ssh oracle@10.116.12.26 <<'XX'


来源:https://stackoverflow.com/questions/18954264/passing-variable-user-pass-sid-in-sqlplus-comman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!