日期:2014-05-16  浏览次数:20815 次

linux下批量执行oracle脚本的shell脚本
#! /bin/bash -l
create_dbtable()
{
    set -x
    dbuser=$1
    dbpassword=$2
    sqlfile=$3
    conn=${dbuser}/${dbpassword};
    echo "create data of $dbuser..."
    if [ ${dbuser} = 'sys' ]
      then
        conn="${conn} as sysdba";
    fi
    sqlplus "${conn}" <<EOF
      whenever sqlerror exit 1;
         @$sqlfile
EOF
    if [  $? != 0 ]
    then
        echo "Error:execute ${sqlfile} error"
        exit 1
    fi
    echo "execute ${sqlfile} completelly!"
}

main()
{
        export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
        create_dbtable $*
        if [ $? -ne 0 ];then
            echo "Error:execute ${sqlfile} error"
            exit 1
        fi
        echo "execute ${sqlfile} successfully."
}
main $*

?将上述内容保存在create_database.sh中

?

BEGIN EXECUTE IMMEDIATE 'DROP TABLE T_NOTIFICATION_INFO'; EXCEPTION WHEN OTHERS THEN NULL; END;
/
/*==============================================================*/
/* TABLE: "T_NOTIFICATION_INFO"                                 */
/*==============================================================*/
CREATE TABLE T_NOTIFICATION_INFO  (
  "SUBSCRIPTIONID"     VARCHAR(36)                     NOT NULL,
  "MANAGERREFERENCE"   VARCHAR(2000),
  "TIMETICK"           INT,
  "NOTIFICATIONCATEGORIES" VARCHAR(4000),
  "FILTER"             VARCHAR(4000),
  "SUBSTATE"           INT,
  "ADDTIME"            DATE,
  CONSTRAINT PK_T_NOTIFICATION_INFO PRIMARY KEY ("SUBSCRIPTIONID")
);

?将上述内容保存在createTable.sql中

?

1)root用户登录后
将create_database.sh,createTable.sql两个文件上传

2)修改create_database.sh文件可执行权限
chmod 775 create_database.sh

3)退出,用oracle用户登录
所在目录下执行
./create_database.sh sys i2000db 'createTable.sql'

注:./create_database.sh 后面带3个参数,根据实际情况调整
1 用户名 sys
2 密码?? i2000db
3 sql文件 createTable.sql

?