Sunday, March 20, 2011

bourne shell single-quote, doublequote & backquote question

Hi all,

#/!bin/sh

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'

First if-condition won't run, im guessing it's because of improper quotation, but i can't figure it out.

Thanks in advance for any help.

From stackoverflow
  • If you're using Bash, I'd recommend $(...) instead of back-quotes. What error messages do you get? My guess is that the -F"," option to awk is not being quoted properly. Trying inserting \ to escape the quotation marks.

    Anders : Sorry, can't use $(...) very very old shell. MMm, good suggestion, gonna try that.
    ghostdog74 : can't use $()?? then why the bash tag?
    Anders : Changed the topic.
    Anders : Was simply a matter of escaping. Thanks for the help.
    JesperE : Update the tags as well, please.
    Anders : Done, thanks for the help.
  • At first glance, you might want to try escaping some of the double quotes:

       if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
            echo "howdy dody"
       fi
    

    to

       if [ "`echo $desc $status | awk -F\",\" '{print $3}' | awk -F\" \" '{print $1}' | sed '/^$/d'`" != "OK" ]; then
            echo "howdy doody"
       fi
    
    Anders : Thanks for the help.
  • You can also use single quotes around the argument to the -F option as you have around other arguments:

    if [ "`echo $desc $status | awk -F',' '{print $3}' | awk -F' ' '{print $1}' | sed '/^$/d'`" != "OK" ]; then
    
  • Escaping the double quotes is certainly a good idea, but it looks like the $3 and the $1 are intended to be interpreted by awk. They are being interpreted by your shell instead. You probably want to escape the '$'s. (It is possible that you have meaningful values for $1 and $3 in the shell, but not likely.)

    Dennis Williamson : No, they're in single quotes so they don't need escaping.

0 comments:

Post a Comment