[英]Shell Script with sqlplus and special characters on password
I have a question that mix Linux / Unix shell-scripting and sqlplus (Oracle) that is driving me crazy.我有一个混合 Linux / Unix shell 脚本和 sqlplus (Oracle) 的问题,这让我发疯。 :-)
:-)
sqlplus utilize a syntax like this: sqlplus 使用这样的语法:
./sqlplus johnF/mypassword@127.0.0.1:1521/SID
And it works fine.它工作正常。 However my password is not simple as "mypassword", it utilize "!"
但是我的密码并不像“mypassword”那么简单,它使用“!” and "@" and sometimes even "\\".
和“@”,有时甚至是“\\”。 For this example, let's suppose that my password is !p@ssword
在这个例子中,假设我的密码是 !p@ssword
If I use the following syntax in sqlplus it works:如果我在 sqlplus 中使用以下语法,它会起作用:
./sqlplus johnF/'"!p@ssword"'@127.0.0.1:1521/SID
That's great.那太好了。 However I wanted to use it in a shell script that get call sqlplus and get many parameters from files (username, password, SID and SQL QUERY), just for example let me use a reduced code.
但是,我想在 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
I have attempted to fix it in many ways, including:我尝试通过多种方式修复它,包括:
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
And many others and all fails, in a few cases the first echo print the output exactly as it should be passed to sqlplus, but it never works, returns login denied (wrong password) or connection issues (maybe the @ being intercepted as wrong target).和许多其他人一样都失败了,在少数情况下,第一个 echo 完全按照应传递给 sqlplus 的方式打印输出,但它永远不会工作,返回登录被拒绝(密码错误)或连接问题(也许 @ 被拦截为错误的目标)。
How to solve this puzzle?这个谜题怎么解?
Thanks.谢谢。
Configure the config file sqlnet.ora
for an easy connection.配置配置文件
sqlnet.ora
以方便连接。
NAMES.DIRECTORY_PATH= (TNSNAMES,ezconnect)
Change the password @T!ger to the user "Scott".将密码@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.
Example 1 Script is test_echo.sh示例 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
Example 2 Run script test_echo.sh in silent mode sqlplus示例 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
Example 3 A little bit Another syntax示例 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
i assume you issued this to alter your user's password :我假设你发出这个来改变你的用户密码:
alter user johnF identified by "!p@ssword";
since因为
alter user johnF identified by !p@ssword;
doesn't conforms oracle password definition rules.不符合oracle密码定义规则。
and then it's enough to write such a script in your file to connect your schema :然后在您的文件中编写这样一个脚本来连接您的架构就足够了:
#!/bin/bash
# cnn.sh
line '"!p@ssword"'
echo line
sqlplus johnF/$line@127.0.0.1:1521/yourSID
and call from prompt :并从提示调用:
$ . cnn.sh
I've encountered the same problem here as well (which really drives me crazy), and this is my answer.我在这里也遇到了同样的问题(这真的让我发疯了),这就是我的答案。
All the special characters allowed in Oracle could be found on this page: https://docs.oracle.com/cd/E11223_01/doc.910/e11197/app_special_char.htm#MCMAD416 Oracle 中允许的所有特殊字符都可以在此页面上找到: https : //docs.oracle.com/cd/E11223_01/doc.910/e11197/app_special_char.htm#MCMAD416
You can use it like this:你可以这样使用它:
sh test.sh oracle_user '"#o@%+!$(){}[]/\^?:`~"' ip port service_name
You can use it like this:你可以这样使用它:
sh test.sh oracle_user '"#o@%+!$(){}[]/\^?:`~'"'"'"' ip port service_name
Please be noted that the single quotation mark ' should be replaced with '""'.请注意,单引号 ' 应替换为 '""'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.