I have code where awk is piped to a clearcase command where If else loop is not working.
code is below :
#!/bin/ksh
export dst_region=$1
cleartool lsview -l | gawk -F":" \ '{ if ($0 ~ /Global path:/) { if($dst_region == "ABC" || $dst_region -eq "ABC") { system("echo dest_region is ABC");}
else { system("echo dest_region is not ABC"); } }; }'
But when I execute the above script the I get incorrect output,
*$ ksh script.sh ABCD
dest_region is ABC
$ ksh script.sh ABC
dest_region is ABC*
Could anyone please help on this issue ?
It would be useful if you explained exactly what you are trying to do but your awk script can be cleaned up a lot:
gawk -F":" -vdst_region="$1" '/Global path:/ { if (dst_region == "ABC") print "dest_region is ABC"; else print "dest_region is not ABC" }'
General points:
-v
to create an awk variable from the value of $1
, the first argument to the script. This means that you can use it a lot more easily in the script.condition { action }
so you're using if
around the whole one-liner unnecessarily$0 ~ /Global path:/
can be changed to simply /Global path:/
||
looked like they were trying to both do the same thing, so I got rid of the one that doesn't work in awk. Strings are compared using ==
.system("echo ...")
is completely unnecessary. Use awk's built in print
You could go one step further and remove the if-else
entirely:
gawk -F":" -vdst_region="$1" '/Global path:/ { printf "dest region is%s ABC", (dst_region=="ABC"?"":" not") }'