bourne shell single-quote, doublequote & backquote question
Question
/!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.
Solution
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.
OTHER TIPS
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
It is much easier to write your test if you wrap it in a function:
mytest()
{
echo "$1 $2" \
| awk -F"," -v arg3="$3" '{print arg3}' \
| awk -F" " -v arg1="$1" '{print arg1}' \
| sed '/^$/d'
}
This way, you can verify that it works correctly. Once you gained this confidence
if [ "$(mytest "$desc" "$status")" != "OK" ]; then
echo "howdy doody"
fi
or
if mytest "$desc" "$status" | grep -q -v '^OK$'; then
echo "howdy doody"
fi
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
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.)