[英]Double quotes inside double quotes in shell script
I try to run a command that has double quotes inside it: 我尝试运行其中带有双引号的命令:
insdizi=`ps -ef| grep -v grep | grep asm_pmon_ | awk ' {print $2}'`
proddizi=`ps -ef | grep asm_smon_ | grep -v grep | cut -d"_" -f3`
insname=()
homename=()
sid=()
for i in $insdizi; do
insname+=( "$i" )
a=`ls -l /proc/${insname[ii]}/cwd | awk '{print ($NF) }' |sed 's#/dbs##'`
olsnodes="${a}/bin/olsnodes"
homename+=( "$olsnodes" )
ii=`expr $ii + 1`
done
ii=`expr $ii - 1`
for i in `seq 0 $ii`; do
nodeNum= "${olsnodes}"
nodeNumm= `bash -c "${nodeSayi} |grep -c '""'"`
echo $nodeNumm
echo "nodeNumm= $nodeNumm"
for node in `bash -c "${homename[i]}"`; do
echo $node
cokluBellekKontrol $node
cokluSessionSayi $node
done
done DONE
olsnodes
variable is a command which is run from a directory like: olsnodes
变量是从以下目录运行的命令:
/app/oracle/grid/bin/olsnodes
Here is what i need to run: 这是我需要运行的:
/app/oracle/grid/bin/olsnodes | grep -c ""
I tried this: 我尝试了这个:
nodeNumm= `bash -c "${nodeNum} |grep -c '""'"`
But it gave me error: 但这给了我错误:
"0: command not found."
EDIT 编辑
output of olsnodes is : olsnodes的输出是:
ax1
ax2
ax3
ax4
Also, i can grep the line count with this command: 另外,我可以使用以下命令grep行数:
/u01/app/11.2.0.4/grid/bin/olsnodes |grep -c ""
Single and double quotes don't nest with respect to each other. 单引号和双引号不会相互嵌套。 Only parenthesized and braced substitutions (
${}
, $()
, $(())
) do. 只有带括号和大括号的替换(
${}
, $()
, $(())
)才可以。
You can escape quotes within quotes with \\
. 您可以使用
\\
引号内的引号引起来。
nodeNumm= `bash -c "${nodeSayi} |grep -c '""'"`
should be 应该
nodeNumm= $(bash -c "${nodeSayi} |grep -c '\"\"'")
Or should it? 还是应该? Did you want to do
你想做吗
grep -c ""
or 要么
grep -c '""'
? ?
If the former, it could have been written simply as 如果是前者,它本来可以写成
grep -c ''
and there is no problem putting that in double quotes. 并用双引号将它毫无问题。
Then I suspect that will still not do what you expected, unless you expected it to: 然后,我怀疑除非您期望,否则它仍然不会实现您的期望:
nodeNumm
to empty string for duration of the following, nodeNumm
设置为空字符串, If you wanted to set nodeNumm
to the output of the command, correct syntax would be: 如果要将
nodeNumm
设置为命令的输出,则正确的语法为:
nodeNumm=$(bash -c "${nodeSayi} | grep -c '\"\"'")
There is however no point in running the grep
in the subshell, which can get us rid of the outer quotes and the whole nesting problem. 但是,在子外壳程序中运行
grep
毫无意义,这可以使我们摆脱外部引号和整个嵌套问题。 Just 只是
nodeNumm=$(bash -c "${nodeSayi}" | grep -c '""')
Note, that I changed the process substitution from backquotes to $()
. 注意,我将流程替换从反引号更改为
$()
。 It's exactly because that nests correctly with respect to other process substitutions and to quotes. 正是因为这样才能相对于其他流程替换和引号正确嵌套。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.