Note: Technical Talk being moved to:
www.nazimcricket.com/wiki


Monday, 12 April 2010

Oracle Shell Script with Password Prompts

An example of a shell script which connects to multiple unix hosted databases and runs a simple SQL. This version prompts for the username/password.
#!/bin/ksh
#
# Shell script that connects to multiple database servers and runs a SQL
#

# Script expects the username and password to be parsed as arguments.
# If not found, it will error with message ...

if [[ $# -lt 2 ]]
then
        print "\nUSAGE: script_name.ksh username password\n"
        print "\nExample: script_name.ksh scott tiger\n"
        exit 0
fi

username=$1
password=$2

# Server List
SERVERS='server1 server2 server3'

LOG="/tmp/logfile.log"

> $LOG

for SERVER in $SERVERS
do

sqlplus -s $username/$password@$SERVER << EOF | tee -a $LOG
select * from global_name;
exit;
EOF

done

No comments: