使用Shell进行SQL查询:无效选项

时间:2018-02-01 07:47:45

标签: oracle shell unix sqlplus

所以我试图在我的脚本shell中执行SQL查询(Oracle数据库)。

#!/bin/bash
#!/bin/ksh
login='mylogin'
pss='mypassword'

CE=`sqlplus -s $login/$pss << END1
set pause off heading off feed off pagesize 0;
select * from table1;
END1`  
echo $CE

但我收到了这个错误:

  

用户名/密码无效;登录被拒绝SP2-0306:无效选项。   用法:CONN [ECT] [{logon | / | proxy} [AS {SYSDBA | SYSOPER | SYSASM}]   [edition = value]]其中:: =   [/] [@] :: =   [] [/] [@] SP2-0306:   选项无效。用法:CONN [ECT] [{logon | / | proxy} [AS   {SYSDBA | SYSOPER | SYSASM}] [edition = value]]其中:: =   [/] [@] :: =   [] [/] [@] SP2-0157:   3次尝试后无法连接到ORACLE,退出SQL * Plus

有任何帮助吗? 谢谢,

1 个答案:

答案 0 :(得分:0)

示例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

#!/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

示例3

#!/bin/sh

username=\"Scott\"
password=\"@T!ger\"


echo username:  $username
echo password:  $password

testoutput=$(sqlplus -s $username/$password  << 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"
USER is "Scott" 01-02-2018 11:04 Test passed

配置配置文件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.