shell如何调用sqlplus(各种情况示例)

 时间:2026-02-14 04:04:09

1、最简单的shell里调用sqlplus.

$ vi test1.sh

#!/bin/bash

sqlplus -S /nolog  <<EOF

set heading off feedback off pagesize 0 verify off echo off

conn test/test

select * from tab;

exit

EOF

$ chmod +x test1.sh

$ ./test1.sh

shell如何调用sqlplus(各种情况示例)

2、把sqlplus执行结果传递给shell方法一

$ vi test2.sh

#!/bin/bash

VALUE=`sqlplus -S /nolog <<EOF

set heading off feedback off pagesize 0 verify off echo off numwidth 4

conn test/test

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

shell如何调用sqlplus(各种情况示例)

3、把sqlplus执行结果传递给shell方法二

$ vi test3.sh

#!/bin/bash

sqlplus -S /nolog <<EOF

set heading off feedback off pagesize 0 verify off echo off numwidth 4

conn test/test

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

shell如何调用sqlplus(各种情况示例)

4、把shell程序参数传递给sqlplus

$1表示第一个参数, sqlplus里可以直接使用, 赋变量的等号两侧不能有空格不能有空格.

$ vi test4.sh

#!/bin/bash

NAME="$1"

sqlplus -S test/test <<EOF

select * from tab where tname = upper('$NAME');

exit

EOF

$ chmod +x test4.sh

$ ./test4.sh ttt

shell如何调用sqlplus(各种情况示例)

5、为了安全要求每次执行shell都手工输入密码

$ vi test5.sh

#!/bin/bash

echo -n "Enter password for u_test:"

read PASSWD

sqlplus -S /nolog <<EOF

conn test/$PASSWD

select * from tab;

exit

EOF

$ chmod +x test5.sh

$ ./test5.sh

shell如何调用sqlplus(各种情况示例)

6、为了安全从文件读取密码

对密码文件设置权限, 只有用户自己才能读写.

$ echo 'test' > 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 test/$PASSWD

select * from tab;

exit

EOF

$ chmod +x test6.sh

$ ./test6.sh

shell如何调用sqlplus(各种情况示例)

  • 苹果13手机如何打开喜马拉雅app个性化广告推荐
  • 牛油果的功效及作用
  • 金十数据怎么关闭退出后台自动小窗口播放
  • opera如何开启扩展的使用情况统计
  • 怎么设置苹果13提醒事项角标
  • 热门搜索
    互助旅游 珲春旅游 双显卡怎么切换 如何开公司 怎么关闭qq设备锁 沈阳哪里好玩 羽绒服洗后成团怎么办 印度旅游报价 如何降低甘油三酯 会计求职意向怎么写