shell脚本中双引号内的双引号

时间:2022-09-15 16:21:24

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

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 ""

1 个解决方案

#1


1  

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:

然后我怀疑仍然没有达到你的预期,除非你期望它:

  • set variable nodeNumm to empty string for duration of the following,
  • 将变量nodeNumm设置为空字符串,持续时间如下,

  • run the command,
  • 运行命令,

  • execute the output as a command.
  • 将输出作为命令执行。

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. Just

然而,在子shell中运行grep是没有意义的,这可以让我们摆脱外部引号和整个嵌套问题。只是

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.

请注意,我将进程替换从反引号更改为$()。这正是因为它在其他过程替换和引用方面正确嵌套。

#1


1  

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:

然后我怀疑仍然没有达到你的预期,除非你期望它:

  • set variable nodeNumm to empty string for duration of the following,
  • 将变量nodeNumm设置为空字符串,持续时间如下,

  • run the command,
  • 运行命令,

  • execute the output as a command.
  • 将输出作为命令执行。

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. Just

然而,在子shell中运行grep是没有意义的,这可以让我们摆脱外部引号和整个嵌套问题。只是

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.

请注意,我将进程替换从反引号更改为$()。这正是因为它在其他过程替换和引用方面正确嵌套。