Examples of various situations in which shell calls sqlplus

 1. Call sqlplus in the simplest shell.

$ vi test1.sh

#!/bin/bash
sqlplus -S /nolog > result.log <<EOF
set heading off feedback off pagesize 0 verify off echo off
conn u_test/iamwangnc
select * from tab;
exit
EOF

$ chmod +x test1.sh
$ ./test1.sh

Second, pass the sqlplus execution result to the shell method 1

Note that the boss key is used in the sqlplus segment, and there can be no spaces on both sides of the equal sign of the assigned variable.

$ vi test2.sh

#!/bin/bash
VALUE=`sqlplus -S /nolog <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
select count(*) from tab;
exit
EOF`
if [ "$VALUE" -gt 0 ]; then
        echo "The number of rows is $VALUE."
        exit 0
else
        echo "There is no row in the table."
fi

$ chmod +x test2.sh
$ ./test2.sh

3. Pass the execution result of sqlplus to the shell method 2

Note that the sqlplus segment uses col .. new_value .. to define a variable with parameter exit, and then automatically assign it to the shell's $?

$ vi test3.sh

#!/bin/bash
sqlplus -S /nolog > result.log <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
col coun new_value v_coun
select count(*) coun from tab;
exit v_coun
EOF
VALUE="$?"
echo "The number of rows is $VALUE."

$ chmod +x test3.sh
$ ./test3.sh

Fourth, pass the shell program parameters to sqlplus

$1 represents the first parameter, which can be used directly in sqlplus. There can be no spaces on both sides of the equal sign of the assigned variable.

$ vi test4.sh

#!/bin/bash
NAME="$1"
sqlplus -S u_test/iamwangnc <<EOF
select * from tab where tname = upper('$NAME');
exit
EOF

$ chmod +x test4.sh
$ ./test4.sh ttt

5. For security, the password is required to be manually entered every time the shell is executed.

$ vi test5.sh

#!/bin/bash
echo -n "Enter password for u_test:"
read PASSWD
sqlplus -S /nolog <<EOF
conn u_test/$PASSWD
select * from tab;
exit
EOF

$ chmod +x test5.sh
$ ./test5.sh

6. Read passwords from files for security

Set permissions on the password file, only the user can read and write.

$ echo 'iamwangnc' > u_test.txt
$ chmod g-rwx,o-rwx u_test.txt
$ vi test6.sh

#!/bin/bash
PASSWD=`cat u_test.txt`
sqlplus -S /nolog <<EOF
conn u_test/$PASSWD
select * from tab;
exit
EOF

$ chmod +x test6.sh
$ ./test6.sh

Seven, package function call

Execute oracle sql script in Linux background: execute sql
of oracle database under shell
[plain] view plain copy
#### Function 
#### Description: execute sql statement 
#### Input parameters: {sql statement}{database tns} {database user name}{database user password} 
#### Output parameters: sql execution result 
function exe_sql 

sql=$1 
oracle_sid=$2 
user_name=$3 
user_pwd=$4 
error_code=` 
sqlplus -S -L /nolog<<EOF 
connect $ user_pwd/$user_pwd@$oracle_sid 
set termout off; 
set echo off; 
set feedback off; 
set heading off; 
set pagesize 0; 
$sql; 
commit; 
quit 
EOF` 
echo "$error_code" 
if [ -z "$error_code" ]; then 
    return 0 
else 
  return 1 
fi 

--End--

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326308836&siteId=291194637