2 Replies Latest reply: Aug 20, 2012 5:48 AM by Billy~Verreynne RSS

    IF condition in UNIX

    891060
      Hi

      I am running a shell script

      echo "Inside the shell script for product Loading"
      tmp=`wc -l product.csv| sed 's/[^0-9]//g'`
      echo $tmp
      if [ "$tmp" -eq "0" ]
      then
      echo "Nodata file exists"
      fi


      its throwing error as
      sample1[4]: syntax error at line 4 : `if' unmatched

      Can any one suggest on this
        • 1. Re: IF condition in UNIX
          Catch-22
          "If unmatched" indicates a problem with your if-then construct. Did your create the script under Linux or did you copy it from a MS Windows system? In the later case your script may not have proper line endings.

          What happens after you process your script with the "dos2unix" command (yum install dos2unix) or for use perl, for intance

          <pre>
          dos2unix script.sh
          perl -pi -e 's/\r\n/\n/g' script.sh
          </pre>
          • 2. Re: IF condition in UNIX
            Billy~Verreynne
            user6149203 wrote:

            if [ "$tmp" -eq "0" ]
            Behaviour may differ between shells - but for bash, this operator expects numeric input. So if +$tmp+ is null/nothing, the condition will fail. E.g.
            /home/billy> LINES=""
            
            // -eq operator results in a numeric evaluation that fails
            /home/billy> if [ "$LINES" -eq "0" ]; then echo "Lines is zero"; fi;
            bash: [: : integer expression expected
            
            // the = operator supports string checking and works
            /home/billy> if [ "$LINES" = "0" ]; then echo "Lines is zero"; fi;