AloneInTheDark
AloneInTheDark

Reputation: 938

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

olsnodes variable is a command which is run from a directory like:

   /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 :

ax1
ax2
ax3
ax4

Also, i can grep the line count with this command:

/u01/app/11.2.0.4/grid/bin/olsnodes |grep -c ""

Upvotes: 1

Views: 3149

Answers (1)

Jan Hudec
Jan Hudec

Reputation: 76376

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,
  • 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=$(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

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.

Upvotes: 1

Related Questions