unexpected EOF while looking for matching `"'
I am writing a script which has command to execute as below:
cat /abc | grep -v ^# | grep -i root | sed -e '\''s/"//g'\'' | awk开发者_如何学运维 '\''{print $2}'\''
When running the script on SunOS, i am getting below error:
test: line 1: unexpected EOF while looking for matching `"'
test: line 3: syntax error: unexpected end of file
Tried with different option.. but no luck.
Need somebody help me identify what is missing in the above command.
what are those escapes ?!
cat /abc | grep -v '^#' | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
^ ^ ^ ^
Your problem is there:
sed -e '\''s/"//g'\''
^ unmatched
The quoting is all wrong. Why do you use single quote, backslash, single quote, single quote,and always in that order? Regardless, you have an unquoted double quote, so the shell expects you to add a closing quote for the quoted string which starts with that opening double quote.
As a matter of style, you should also lose the Useless Use of Cat, and think about how to simplify your script. At least:
grep -v ^# /abc | grep -i root | sed -e 's/"//g' | awk '{print $2}'
... but in practice
awk '/^#/ { next } /[Rr][Oo][Oo][Tt]/ { gsub ("\"",""); print $2 }' /abc
Because some of the characters in the awk
and sed
scripts have a special meaning to the shell, we put them in single quotes. If you need to have single quotes in a script, you need to double quote them; a frequent pattern is to have a string in single quotes adjacent to a string in double quotes, like this: echo '"'"'"
. This echos "
(quoted in single quotes) immediately followed by '
(quoted in double quotes).
Edit Updated analysis of quoting problem; added code example; corrected code example. Final edit corrects quoting of gsub
in awk script, and adds a small discussion of quoting.
精彩评论