title: Unix shell script errors
body: <p>Below script is to push file to remote location through sftp,i faced lot of issues to write below code.But still i am facing some issue,Please guid me to resolve the issues.It's not working with sh.it is only working with ksh.</p>
  
  <pre><code>#test script 
  #-------------------------------------------------------------------
  #!/bin/sh
  #------------------------------------------------------------------------
  # sftp_file_uploads.sh 
  #------------------------------------------------------------------------
  export REMOTE_SERVER_PROD='192.168.0.1'
  export REMOTE_SERVER_FAILOVER='192.168.0.2'
  export SFTP_PORT='0001'
  export SOURCE_FUNCTIONAL_ID='testusr'
  export SOURCE_FILE_DIRECTORY='/var/temp/files/'
  export SOURCE_ARCHIVE_DIRECTORY='/var/temp/files/archive'
  export DATE_FORMAT=`date "+%Y%m%d"`
  export LOG_DIRECTORY='/var/temp/logs'
  export DESTINATION_FILE_DIRECTORY='/dest'
  export LOG_FILE='$LOG_DIRECTORY/test_$DATE_FORMAT.log'
  export SFTP_BATCH_FILE='/var/tmp/SFTP_BATCH_FILE'
  #------------------------------------------------------------------------
  # Find if the files are available at the source directory.
  #------------------------------------------------------------------------
  cd $SOURCE_FILE_DIRECTORY 
  export FILE_TO_UPLOAD_TESTD=`ls -lrt TESTD$DATE_FORMAT.csv  | awk '/TESTD/{ f=$NF };END{ print f }'`
  export FILE_TO_UPLOAD_TESTDF=`ls -lrt TESTDF$DATE_FORMAT.csv  | awk '/TESTDF/{ f=$NF };END{ print f }'`
  #------------------------------------------------------------------------
  # Try 2 times and Sleep for 5 mins if either of the files is not present
  #------------------------------------------------------------------------
  counter=0
  flag_file_found_TESTD=0
  flag_file_found_TESTDF=0
  while [ $counter –lt 2 ]
  do
  #---------------------------
  # Check TESTD file arrived
  #---------------------------
  if [ -z $FILE_TO_UPLOAD_TESTD ] then
        echo “No TESTD file to transfer. Sleeping for 5 mins” &gt;&gt; $LOG_FILE
   sleep 300
  else
   echo “TESTD file found to transfer.” &gt;&gt; $LOG_FILE
   flag_file_found_TESTD=1
  fi
  #---------------------------
  # Check TESTDF file arrived
  #---------------------------
  if [ -z $FILE_TO_UPLOAD_TESTDF ] then
        echo “No TESTDF file to transfer. Sleeping for 5 mins” &gt;&gt; $LOG_FILE
   sleep 300
  else
   echo “TESTDF file found to transfer.” &gt;&gt; $LOG_FILE
  flag_file_found_TESTDF =1
  fi
  if [[ flag_file_found_TESTD == 1 &amp;&amp;
        flag_file_found_TESTDF == 1 ]] then
   echo “Both files are found.” &gt;&gt; $LOG_FILE
   break
  else
  
   echo “At least one of the files is not found. Retrying now.” &gt;&gt; $LOG_FILE
  fi
  counter=`expr $counter + 1`
  done
  if [[ flag_file_found_TESTD == 1 &amp;&amp;
        flag_file_found_TESTDF == 1 ]] then
   echo “Both files are found.”
   break
  else
   if [ flag_file_found_TESTD == 0 ] then
      echo “test file is not found and two attempts completed. Cannot transfer the file for today.” &gt;&gt; $LOG_FILE
   fi
  
  if [flag_file_found_TESTDF == 0 ] then
   echo “test1 file is not found and two attempts completed. Cannot transfer the file for today.” &gt;&gt; $LOG_FILE
   fi
  fi
  #------------------------------------------------------------------------
  # Create sftp script
  #------------------------------------------------------------------------
  rm -f $SFTP_BATCH_FILE
  echo "lcd $SOURCE_FILE_DIRECTORY " &gt; $SFTP_BATCH_FILE
  echo "cd $DESTINATION_FILE_DIRECTORY " &gt;&gt; $SFTP_BATCH_FILE
  if [ -z $FILE_TO_UPLOAD_TESTD ] then
  echo "put $FILE_TO_UPLOAD_TESTD " &gt;&gt; $SFTP_BATCH_FILE
  fi
  if [ -z $FILE_TO_UPLOAD_TESTDF ] then
  echo "put $FILE_TO_UPLOAD_TESTDF " &gt;&gt; $SFTP_BATCH_FILE
  fi
  echo "bye" &gt;&gt; $SFTP_BATCH_FILE
  
  #------------------------------------------------------------------------
  # Do sftp
  #------------------------------------------------------------------------
  echo " Before SFTP " &gt;&gt; $LOG_FILE
  if [[ -z $ FILE_TO_UPLOAD &amp;&amp; -z $ FILE_TO_UPLOAD1 ]] then
   echo “No files to transfer” &gt;&gt; $LOG_FILE
         mv $LOG_FILE $LOG_DIRECTORY
   exit 1
  else 
  echo “Attempting to connect to  Remote Server $REMOTE_SERVER_PROD” &gt;&gt; $LOG_FILE
  /usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID@$REMOTE_SERVER_PROD &gt;&gt; $LOG_FILE 2 &gt;&gt; $LOG_FILE
  fi
  result=$?
  errorConnectToProd=0
  if [ $result -eq 0 ]
  then
    echo "SFTP completed successfully to Prod Remote Server" &gt;&gt; $LOG_FILE
  else
      errorConnectToProd=1
     if [[ $result -eq 4 || $result -eq 5 ]]
    echo "FAILED to connect to Server. " &gt;&gt; $LOG_FILE
  
     else
   echo "FAILED to SFTP to  Remote Server. " &gt;&gt; $LOG_FILE
     fi
  fi
  if [ errorConnectToProd == 1 ] then
  echo “Attempting to connect to FAILOVER Remote Server $REMOTE_SERVER_FAILOVER” &gt;&gt; $LOG_FILE
  /usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID@$REMOTE_SERVER_FAILOVER &gt;&gt; $LOG_FILE 2 &gt;&gt; $LOG_FILE
  fi
  result=$?
  if [ $result -eq 0 ]
  then
    echo "SFTP completed successfully to Failover Remote Server" &gt;&gt; $LOG_FILE
  else
     echo "FAILED to SFTP to Failover Remote Server. " &gt;&gt; $LOG_FILE
     mv $LOG_FILE $LOG_DIRECTORY
     exit 1
     fi
  fi
  cd $SOURCE_FILE_DIRECTORY
  mv $FILE_TO_UPLOAD_TESTD $SOURCE_ARCHIVE_DIRECTORY
  echo “Moved $FILE_TO_UPLOAD_TESTD to archive direcotry.” &gt;&gt; $LOG_FILE
  mv $FILE_TO_UPLOAD_TESTDF $SOURCE_ARCHIVE_DIRECTORY
  echo “Moved $FILE_TO_UPLOAD_TESTDF to archive direcotry.” &gt;&gt; $LOG_FILE
  
  rm -f $SFTP_BATCH_FILE
  echo “Deleted the SFTP Batch file.” &gt;&gt; $LOG_FILE
  echo “Upload completed.” &gt;&gt; $LOG_FILE
  mv $LOG_FILE $LOG_DIRECTORY
  exit 0
  </code></pre>
  
  <p>Getting below Errors:</p>
  
  <pre><code>test.ksh[41]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[55]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[56]: flag_file_found_TESTDF:  not found
  
  test.ksh[65]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[41]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[55]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[56]: flag_file_found_TESTNDF:  not found
  
  test.ksh[65]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[79]: [flag_file_found_TESTDF:  not found
  
  rm: /var/tmp/SFTP_BATCH_FILE is a directory
  
  test.ksh[89]: /var/tmp/SFTP_BATCH_FILE: cannot create
  
  test.ksh[90]: /var/tmp/SFTP_BATCH_FILE: cannot create
  
  test.ksh[97]: B:  not found
  
  test.ksh[98]: B:  not found
  
  test.ksh[99]: B:  not found
  
  test.ksh[100]: B:  not found
  
  test.ksh[101]: B:  not found
  
  test.ksh[102]: B:  not found
  
  test.ksh[106]: /var/tmp/SFTP_BATCH_FILE: cannot create
  
  test.ksh[113]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
  
  test.ksh[114]: syntax error at line 114 : `FILE_TO_UPLOAD' unexpected
  </code></pre>
  
  <p>Regards,</p>
  
  <p>Chai</p>