[英]Shell Script with sqlplus and special characters on password
我有一个混合 Linux / Unix shell 脚本和 sqlplus (Oracle) 的问题,这让我发疯。 :-)
sqlplus 使用这样的语法:
./sqlplus johnF/mypassword@127.0.0.1:1521/SID
它工作正常。 但是我的密码并不像“mypassword”那么简单,它使用“!” 和“@”,有时甚至是“\\”。 在这个例子中,假设我的密码是 !p@ssword
如果我在 sqlplus 中使用以下语法,它会起作用:
./sqlplus johnF/'"!p@ssword"'@127.0.0.1:1521/SID
那太好了。 但是,我想在 shell 脚本中使用它来调用 sqlplus 并从文件中获取许多参数(用户名、密码、SID 和 SQL 查询),例如让我使用简化的代码。
#!/bin/bash
while IFS=: read -r line
do
echo "./sqlplus johnF/$line@127.0.0.1:1521/SID"
echo -e 'select 1 from dual;\nexit;' | ./sqlplus johnF/$line@127.0.0.1:1521/SID
done < $1
我尝试通过多种方式修复它,包括:
echo -e 'select 1 from dual;\nexit;' | ./sqlplus johnF/'"$line"'@127.0.0.1:1521/SID
echo -e 'select 1 from dual;\nexit;' | ./sqlplus johnF/'\"$line\"'@127.0.0.1:1521/SID
echo -e 'select 1 from dual;\nexit;' | ./sqlplus johnF/\'\"$line\"\'@127.0.0.1:1521/SID
和许多其他人一样都失败了,在少数情况下,第一个 echo 完全按照应传递给 sqlplus 的方式打印输出,但它永远不会工作,返回登录被拒绝(密码错误)或连接问题(也许 @ 被拦截为错误的目标)。
这个谜题怎么解?
谢谢。
配置配置文件sqlnet.ora
以方便连接。
NAMES.DIRECTORY_PATH= (TNSNAMES,ezconnect)
将密码@T!ger 更改为用户“Scott”。
oracle@esmd:~>
oracle@esmd:~> sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 29 11:05:04 2018
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL> alter user "Scott" identified by "@T!ger";
User altered.
示例 1脚本为 test_echo.sh
#!/bin/sh
username=\"Scott\"
password=\"@T!ger\"
ezconnect=10.89.251.205:1521/esmd
echo username: $username
echo password: $password
echo ezconnect $ezconnect
echo -e 'show user \n select 1 from dual;\nexit;' | sqlplus $username/$password@$ezconnect
oracle@esmd:~> ./test_echo.sh
username: "Scott"
password: "@T!ger"
ezconnect 10.89.251.205:1521/esmd
SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 29 11:02:52 2018
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL> USER is "Scott"
SQL>
1
----------
1
SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
示例 2以静默模式 sqlplus 运行脚本 test_echo.sh
#!/bin/sh
username=\"Scott\"
password=\"@T!ger\"
ezconnect=10.89.251.205:1521/esmd
echo username: $username
echo password: $password
echo ezconnect $ezconnect
echo -e 'show user \n select 1 from dual;\nexit;' | sqlplus -s $username/$password@$ezconnect
oracle@esmd:~> oracle@esmd:~> ./test_echo.sh
username: "Scott"
password: "@T!ger"
ezconnect 10.89.251.205:1521/esmd
USER is "Scott"
1
----------
1
示例 3一点点另一种语法
#!/bin/sh
username=\"Scott\"
password=\"@T!ger\"
ezconnect=10.89.251.205:1521/esmd
echo username: $username
echo password: $password
echo ezconnect: $ezconnect
testoutput=$(sqlplus -s $username/$password@$ezconnect << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user
SELECT to_char(sysdate,'DD-MM-YYYY HH24:MI')||' Test passed' from dual
exit;
EOF
)
echo $testoutput
oracle@esmd:~> ./test_Upper_case.sh
username: "Scott"
password: "@T!ger"
ezconnect: 10.89.251.205:1521/esmd
USER is "Scott" 29-01-2018 11:55 Test passed
我假设你发出这个来改变你的用户密码:
alter user johnF identified by "!p@ssword";
因为
alter user johnF identified by !p@ssword;
不符合oracle密码定义规则。
然后在您的文件中编写这样一个脚本来连接您的架构就足够了:
#!/bin/bash
# cnn.sh
line '"!p@ssword"'
echo line
sqlplus johnF/$line@127.0.0.1:1521/yourSID
并从提示调用:
$ . cnn.sh
我在这里也遇到了同样的问题(这真的让我发疯了),这就是我的答案。
Oracle 中允许的所有特殊字符都可以在此页面上找到: https : //docs.oracle.com/cd/E11223_01/doc.910/e11197/app_special_char.htm#MCMAD416
你可以这样使用它:
sh test.sh oracle_user '"#o@%+!$(){}[]/\^?:`~"' ip port service_name
你可以这样使用它:
sh test.sh oracle_user '"#o@%+!$(){}[]/\^?:`~'"'"'"' ip port service_name
请注意,单引号 ' 应替换为 '""'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.