Error after I added several lines

I have added several lines to my code

 let cnt=0
  if [ ${sftp_name} = "EOMMSRPT" ]
        then
                if [ ${cnt} -eq 0 ]
                then
                        cnt=`expr ${cnt} + 1`
                        sftp_name=EOMMSRPT1
                        echo "EOMsftpname = ${sftp_name}" >> ${LOGF}
                else
                        sftp_name=EOMMSRPT2
                        echo "EOMsftpname = ${sftp_name}" >> ${LOGF
                fi
  fi
 

Now when I run the script, it has an error

 /sftp_ondemand_monthly.sh[76]: syntax error at line 150 : `>' unexpected
 

What should wrong with it?

Thanks for contribution

---------- Post updated at 01:26 PM ---------- Previous update was at 01:17 PM ----------

Found the problem

 echo "EOMsftpname = ${sftp_name}" >> ${LOGF
 

must be the second

 ${LOGF} 
1 Like

Yep, you caught it I think.

FYI, in a modern shell, you can do CNT=$((CNT+1)) or even ((CNT++)) to add a number, the expr syntax is very old.

Yes, expr is an external program; the builtin $(( )) is much more efficient.
And in an if [ ${cnt} -eq 0 ]; then you can even do cnt=1 .
Further, you can redirect a whole code block like this

  if [ ${cnt} -eq 0 ]
  then
    cnt=1
    sftp_name=EOMMSRPT1
    echo "EOMsftpname = ${sftp_name}"
  else
    sftp_name=EOMMSRPT2
    echo "EOMsftpname = ${sftp_name}"
  fi >> $LOGF

And in this case of course

  if [ ${cnt} -eq 0 ]
  then
    cnt=1
    sftp_name=EOMMSRPT1
  else
    sftp_name=EOMMSRPT2
  fi
  echo "EOMsftpname = ${sftp_name}" >> $LOGF
1 Like