1

I have an issue with the if statement, basically I want to write the values of $tag9700 and $tag9701 to the 9700l.log and 9701l.log respectively if they exist. If they don't then write 9700=, to 9700l.log and 9701=, to 9700l.log.

tag9700=`egrep -Eo '9700=[0-2]{0,9}' $filename-PDK-AP-LXFXMR-01*.log`

if [ $tag9700 -eq 0 ]
then
    echo $tag9700 >> /home/user/9700l.log
else
    echo '9700=,' >> /home/user/9700l.log
fi

tag9701=`egrep -Eo '9701=[A-Z]{0,9}' $filename-PDK-AP-LXFXMR-01*.log`

if [ $tag9701 -eq 0 ]
then
    echo $tag9701 >> /home/user/9701l.log
else
    echo '9701=,' >> /home/user/9701l.log
fi

The outcome is that 9700l.log file should look like:

9700=1
9700=0
9700=,(only if it isn't a value grepped in the intial $tag9700)

The same scenario applies to $tag9701.

Marco
  • 893
DJOlu
  • 95
  • So, what's the issue? Except that you're missing the quotes on if [ "$tag9700" -eq 0 ], and what you probably want is if [ -n "$tag9700" ] anyway. I don't think that grep will ever print a zero. – ilkkachu Mar 15 '17 at 10:27
  • See e.g. http://mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D and http://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters (though the latter doesn't touch the test directly) – ilkkachu Mar 15 '17 at 10:29

1 Answers1

0
tag9700=`egrep -Eo '9700=[0-2]{1,9}' $filename-PDK-AP-LXFXMR-01*.log`
echo "${tag9700:-,}" > 9700.logfile

You need a minimum of 1 match. Then on an unsuccessful match, you make use of the bash substitution property ${var:-...}

  • if [ -z "${tag9700+xxx}" ] then echo '9700=,' >> /home/user/9700l.log else echo "$tag9700" >> /home/user/9700l.log fi – DJOlu Mar 21 '17 at 15:53
  • I got it working with that partially but the problem I have is if a 9700 tag isn't present in the logs it doesn't write 9700=, to 9700l.log – DJOlu Mar 21 '17 at 15:57