广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

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

    来源:网络收集  点击:  时间:2024-05-19
    【导读】:
    shell 调用 sqlplus 各种情况示例。工具/原料moreLinux系统oracle数据库方法/步骤1/6分步阅读

    最简单的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

    2/6

    把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 ; 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/6

    把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

    4/6

    把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

    5/6

    为了安全要求每次执行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

    6/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

    注意事项

    二步骤中sqlplus段使用老板键“`”了, 赋变量的等号两侧不能有空格.

    三步骤中sqlplus段使用 col .. new_value .. 定义了变量并带参数exit, 然后自动赋给了shell的$?

    SQL
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_800201.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-05 14:11:11  耗时:0.026
    0.0259s