#!/bin/ksh
# usage $1 = username $2=pwd, assumes ORACLE_SID and TWO_TASK (if needed) are set
test_conn()
{
usr="$1"
pwd="$2"
sqlplus -s ${usr}/${pwd} << EOF | tr -d '[:space:]'
set feed off
set head off
set pages 0
set verify off
select 'ok' from dual;
exit
EOF
}
oops=1
while [ $oops -eq 1]
do
print 'Enter username: '; read uname
print 'Password: ';
stty -echo
read pass
stty echo
result=$(test_conn $uname $pass)
if [ "$result" = "ok"] then
oops=0
fi
You probably should catch or disable interrupts to the script while reading the password. If the user hits CTRL-C while the script is trying to read the password the TTY will be left in a non-echoing state.
$ ./test_oracle_conn.sh
./test_oracle_conn.sh: line 18: syntax error near unexpected token `from'
./test_oracle_conn.sh: line 18: ` select "ok" from dual;'
Sorry, I am new with shell script
With Regards
---------- Post updated at 05:10 PM ---------- Previous update was at 04:31 PM ----------
sqlplus << EOF
.
.
.
EOF -- the last one
is the trailer for a here document. It has to be in column #1 (on the left hand side
as far as you can go) in your script. You can use any word that is not a shell keyword or command for a delimiter
Us old guys use << ! .... ! for here doc delimiters
because ! is a single character that is never part of POSIX shell syntax. And we can't type.
Log into an idle instance first, so you can set 'whenever sqlerror ... '
user=<username>
pass=<password>
sqlplus /nolog <<EOF
whenever sqlerror exit 99
connect $user/$pass
whenever sqlerror exit 98 -- change this so only the connect attempt uses '99' as the error code
-- do your normal sql stuff now
exit -- assuming a good login and no other error you log out normally
EOF
echo $? # will be 99 if bad login attempt, 98 if other sql error happened, or 0 if everything was fine.
#!/bin/sh
res=`sqlplus $user_name/$password@$tns <<ENDOFSQL
set feed off
set head off
set pages 0
set verify off
select 'ok' from dual;
exit
ENDOFSQL`
if [ "$res" == "ok"]
then
echo "SUCCESS"
else
echo "FAIL"
fi