0

如何在数组中执行管道命令?

例如:

$ ORACLE_USER=user1
$ ORACLE_PASS=password
$ ORACLE_CONN=database1
$ ORACLE_EXPDP_JOB_NAME=expdp-job-name
$ ORACLE_SQLPLUS_BIN=/opt/oracle/product/10.2.0/client/bin/sqlplus
$ ORACLE_SQLPLUS_PARAMS=( -S "${ORACLE_USER}"/"${ORACLE_PASS}"@"${ORACLE_CONN}" )
$ CMD=( echo -e \"DROP TABLE "${ORACLE_USER}".'\"${ORACLE_EXPDP_JOB_NAME}\"'\;\\nCOMMIT\;\\n\" \| ${ORACLE_SQLPLUS_BIN} "${ORACLE_SQLPLUS_PARAMS[@]}" )

$ "${CMD[@]}"

这输出:

"DROP TABLE user1.\"${ORACLE_EXPDP_JOB_NAME}\";
COMMIT;
" | /opt/oracle/product/10.2.0/client/bin/sqlplus -S user1/password@database1

如果我进行 eval,则该命令已正确执行,如下所示:

$ eval "${CMD[@]}"
DROP TABLE user1."expdp-job-name"
                      *
ERROR at line 1:
ORA-00942: table or view does not exist

Commit complete.

但是,如果结果内部有一个星号“*”,并且我想将它存储在一个变量中,那么 eval 命令将解释星号,从而使整个结果变得混乱:

$ ls -l
total 2,1M
-rw-r--r-- 1 root root    0 2011-04-07 14:33 a.log
-rw-r--r-- 1 root root 186K 2011-04-06 12:05 DEBUG_AD.log
-rw-r--r-- 1 root root 1,7M 2011-04-06 10:38 REPORT.log
-rw------- 1 root root  57K 2011-04-06 10:38 sent

$ b=`eval "${CMD[@]}"`
$ echo $b
DROP TABLE user1."expdp-job-name" a.log DEBUG_AD.log REPORT.log sent ERROR at line 1: ORA-00942: table or view does not exist Commit complete.

你有没有看到??--> a.log DEBUG_AD.log REPORT.log 发送的文件是 eval 在结果中遇到星号时解释的文件。我希望结果会像上面显示的那样,没有星号的解释。

4

1 回答 1

1

自己解决了!

我只需要用双引号回显变量即可避免星号解释。

echo "$b"
DROP TABLE user1."expdp-job-name"
                  *
ERROR at line 1:
ORA-00942: table or view does not exist

Commit complete.

对不起!

于 2011-04-07T18:08:10.830 回答