简体   繁体   English

Sqlplus 到 unix shell 命令由于某种原因正在丢失变量值

[英]Sqlplus to unix shell command is losing variable value for some reason

I am trying to drop my sqlplus session into a unix command, run an executable and format the output into an insert statement for a database.我正在尝试将我的 sqlplus session 放入 unix 命令中,运行可执行文件并将 output 格式化为数据库的插入语句。

This is what I want to end up with, and actually what I have DOES work:这就是我想要得到的结果,实际上我所拥有的确实有效:

    -bash-4.2$ cat blah.log
INSERT INTO TMP_LIST_SEC_STATUS VALUES ('Security ON: Local OS Authentication');

This is the command I am using to give me this output in blah.log.这是我用来在 blah.log 中给我这个 output 的命令。

It formats the output of lsnrctl status and uses the listener name as a parameter to get the output, sorry for poor format, it is all in one line.它将lsnrctl状态的output格式化,并使用监听器名称作为参数来获取output,抱歉格式不好,都在一行中。 Anyone with oracle on unix can run this without harm:任何在 unix 上拥有 oracle 的人都可以运行它而不会造成伤害:

var1=`grep ' =$' $ORACLE_HOME/network/admin/listener.ora | grep -v '(' | grep -v 'SID_LIST' | sed 's/=//g' | xargs -n 1 lsnrctl status | sed -e 's/ \{2,\}/ /g' | grep -i Security | sed -n '1p'|sed 's/  */ /g' | sed -e "s/.*/'&'/"` | echo "INSERT INTO TMP_LIST_SEC_STATUS VALUES ($var1);" > blah.log

So, this works fine at a command prompt as user oracle.因此,这在命令提示符下作为用户 oracle 可以正常工作。 However, I run into problems when I run it from a sqlplus session但是,当我从 sqlplus session 运行它时遇到问题

SQL> ! var1=`grep ' =$' $ORACLE_HOME/network/admin/listener.ora | grep -v '(' | grep -v 'SID_LIST' | sed 's/=//g' | xargs -n 1 lsnrctl status | sed -e 's/ \{2,\}/ /g' | grep -i Security | sed -n '1p'|sed 's/  */ /g' | sed -e "s/.*/'&'/"` | echo "INSERT INTO TMP_LIST_SEC_STATUS VALUES ($var1);" > blah.log

SQL> !cat blah.log
INSERT INTO TMP_LIST_SEC_STATUS VALUES ();

The problem is that the content of the variable is lost.问题是变量的内容丢失了。 So, this is getting stripped out:所以,这被剥离了:

'Security ON: Local OS Authentication'

Does anyone see what I am missing?有人看到我错过了什么吗?

Why does it all work fine on a unix command prompt, but sqlplus loses the variable values?为什么在 unix 命令提示符下一切正常,但 sqlplus 丢失了变量值?

I feel like it's a special character / escape problem but I can't see where despite tying myself in knots with trial and error.我觉得这是一个特殊的角色/逃生问题,但我看不到哪里,尽管我在反复试验中纠结不已。

You have a lot going there.你有很多事情要去那里。 If you post example data and expected output, someone can probably answer with a much tidier method for parsing the text that you need.如果您发布示例数据和预期的 output,有人可能会用一种更简洁的方法来解析您需要的文本。

However, this part of the command in particular is flawed:但是,这部分命令尤其存在缺陷:

... sed -e "s/.*/'&'/"` | echo "INSERT INTO TMP_LIST_SEC_STATUS VALUES ($var1);" > blah.log

You shouldn't have a pipe here.你不应该在这里有 pipe。 Try replacing |尝试更换| with ;; . .

I suspect it only works in the shell because you already set $var1 at some point earlier.我怀疑它只在 shell 中有效,因为您之前已经设置$var1

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

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