user1726550
user1726550

Reputation: 81

Shell script nested if

What is wrong with the following code in shell script :

Below code throwing unexpected else error:

if [ $result -eq 0 ];
then
  echo "SFTP completed successfully to Prod Remote Server" >> $LOG_FILE
else
    errorConnectToProd=1
   if [[ $result -eq 4 || $result -eq 5 ]];
  echo "FAILED to connect to Server. " >> $LOG_FILE

   else
 echo "FAILED to SFTP to  Remote Server. " >> $LOG_FILE
   fi
fi

Below line giving /usr/bin/sftp not found error:

/usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID@$REMOTE_SERVER_PROD >> $LOG_FILE 2 >> $LOG_FILE

Regards,

Chai

Upvotes: 0

Views: 4302

Answers (3)

William Pursell
William Pursell

Reputation: 212238

There are two errors. The syntax error is the missing then. The other error is that this should be a case statement:

exec >> $LOG_FILE
case "$result" in
0)   echo "SFTP completed successfully to Prod Remote Server";;
4|5) errorConnectToProd=1
     echo "FAILED to connect to Server. ";;
*)   echo "FAILED to SFTP to  Remote Server. ";;
esac

Upvotes: 0

Appleman1234
Appleman1234

Reputation: 16076

You are missing the then after the second if statement.

It should be

 if [[ $result -eq 4 || $result -eq 5 ]];
 then
   echo "FAILED to connect to Server. " >> $LOG_FILE

As for the second command, either sftp isn't installed or it isn't in /usr/bin.

Run which sftp to find out where it is.

Upvotes: 2

Jasonw
Jasonw

Reputation: 5064

if [[ $result -eq 4 || $result -eq 5 ]];

You forget the 'then' after the if construct.

When it is not found, check if sftp is found in the path.

Upvotes: 0

Related Questions