简体   繁体   English

如何将变量从bash传递到sqlplus

[英]how to passing variable from bash to sqlplus

I have below code in bash: 我在bash中有以下代码:

#/!bin/bash


USERS=tom1,tom2,tom3

and I want to drop all users from my variable "USERS" via sqlplus 我想通过sqlplus从变量“ USERS”中删除所有用户

#/!bin/bash

USERS=tom1,tom2,tom3

sqlplus -s system/*** <<EOF
*some code*
DROP USER tom1 CASCADE;
DROP USER tom2 CASCADE;
DROP USER tom2 CASCADE;
EOF

pls help 请帮助

Splitting in database using sql / plsql by using the USERS variable is complex and requires understanding of REGEXP functions and I will not recommend that to you. 使用USERS变量使用sql / plsql在数据库中进行拆分很复杂,并且需要了解REGEXP函数,因此我不建议您这样做。 Moreover, it would also require Dynamic Sql to execute a drop query. 而且,它还需要Dynamic Sql来执行drop查询。

I would suggest you to split it within the shell script,create a script and execute it in sqlplus 我建议您在shell脚本中拆分它,创建一个脚本并在sqlplus中执行它

USERS="tom1,tom2,tom3"
    echo ${USERS} | tr ',' '\n' | while read word; do
        echo "drop user $word;"
    done >drop_users.sql

sqlplus -s system/pwd <<EOF
#some code
@drop_users.sql
EOF

Your code is OK except for DDL commands for Oracle DB which are derived from USERS parameter of the script file, call userDrop.sh . 您的代码正常, userDrop.sh脚本文件的USERS参数派生的Oracle DB的DDL命令调用userDrop.sh To make suitable relation with USERS parameter and DB , an extra file ( drpUsr.sql ) should be produced, and invoked inside EOF tags with an @ sign prefixing. 为了与USERS参数和DB建立适当的关系,应生成一个额外的文件( drpUsr.sql ),并在带有@符号前缀的EOF标记内调用该文件。

Let's edit by vi command : 让我们用vi命令编辑:

$ vi userDrop.sh

#!/bin/bash
USERS=tom1,tom2,tom3

IFS=',' read -ra usr <<< "$USERS" #--here IFS(internal field separator) is ','
for i in "${usr[@]}"; do
  echo "drop user "$i" cascade;"
done > drpUsr.sql

#--to drop a user a privileged user sys or system needed.    
sqlplus / as sysdba <<EOF
@drpUsr.sql;
exit;
EOF

Call as 称为

$ . userDrop.sh

and do not forget to come back to command prompt by an exit command of DB. 并且不要忘记通过DB的exit命令返回到命令提示符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM