Pages

Thursday, January 23, 2014

Generic bash script for calling sqlplus and getting back the results

The scenario: 
bash script need to perform sqlplus.

The flow: 
bash script main.sh is calling a generic bash script,  sql_caller.sh, which in turn calls sqlplus.

Parameters:
main.sh is passing to sql_caller.sh number of mandatory and optional parameters:
Mandatory: user, password, connections string, sql file name
Optional: Up to 10 optional parameters.

============
main.sh
============

#!/bin/bash
#****************************************************************
#Generic execute SQL statement script
#****************************************************************
#usage:
#user="my_user"
#paswd="my_pass"
#connect_str="my_sid"
#sql_file="some parameters come here"
#****************************************************************

user="ALEC"
pswd="ALEC"
sid="igt"
sql_file="Get_Export_Directory.sql"

sql_result=$(./second_script.sh -u $user -p $pswd -s $sid -q $sql_file -a "" -b "" -c "" -d "" -e "" -f "" -g "" -h "" -j "" -k "")
echo " "
echo "first SQL completed"
echo "SQL Result: " "$sql_result"

sql_file="Check_User_Exists.sql"
sql_result=$(./second_script.sh -u $user -p $pswd -s $sid -q $sql_file -a "ALEC" -b "" -c "" -d "" -e "" -f "" -g "" -h "" -j "" -k "")
echo " "
echo "second SQL completed"
echo "SQL Result: " "$sql_result"

exit 0

============
sql_caller.sh
============
#!/bin/bash

#echo **********************************************************"
#echo " Generic execute SQL statement script
#echo "*********************************************************"

while getopts u:p:s:q:a:b:c:d:e:f:g:h:j:k option
do
        case "${option}"
        in
                u) user=${OPTARG};;
                p) paswd=${OPTARG};;
                s) connect_str=${OPTARG};;
                q) sql_file=${OPTARG};;
a) prm1=${OPTARG};;
b) prm2=${OPTARG};;
c) prm3=${OPTARG};;
d) prm4=${OPTARG};;
e) prm5=${OPTARG};;
f) prm6=${OPTARG};;
g) prm7=${OPTARG};;
h) prm8=${OPTARG};;
j) prm9=${OPTARG};;
k) prm10=${OPTARG};;
        esac
done
#echo "sqlplus -S ${user}/${paswd}@${connect_str}"

sql_output=`sqlplus -S ${user}/${paswd}@${connect_str} <<EOF
  @${sql_file} $prm1 $prm2 $prm3 $prm4 $prm5 $prm6 $prm7 $prm8 $prm9 $prm10 
EOF
`
status=$?
if [[ $status -ne 0 ]]; then
  echo " ===> ERROR From sqlplus: $sql_output"
  exit $status
fi

for record in "$sql_output"
do
  echo "$record" 
done

exit 0


==================
Get_Export_Directory.sql
==================
set pages 0
set verify off
set feedback off

SELECT directory_path
FROM dba_directories
WHERE directory_name = 'IG_EXP_DIR';

EXIT;

==================
Check_User_Exists.sql
==================
set pages 0 
set verify off 
set feedback off

SELECT DECODE(num,1,'Exists','Not Exists')
FROM(SELECT count(1) num 
       FROM ALL_USERS 
WHERE USERNAME = UPPER('&&1')
);
EXIT;

============
output
============
first SQL completed
SQL Result:  /my_path/proj/workarea/ora_exp

second SQL completed

SQL Result:  Exists

No comments:

Post a Comment