Shell脚本嵌套if

时间:2012-10-27 12:38:15

标签: linux shell unix

shell脚本中的以下代码有什么问题:

以下代码抛出其他意外错误:

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

找不到/ usr / bin / sftp的行下面错误:

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

此致

3 个答案:

答案 0 :(得分:2)

您在第二个if语句后缺少然后

应该是

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

至于第二个命令,sftp未安装或不在/usr/bin

运行which sftp以找出它的位置。

答案 1 :(得分:0)

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

你忘记了if构造之后的'then'。

如果找不到,请检查路径中是否找到sftp。

答案 2 :(得分:0)

有两个错误。语法错误是缺少then。另一个错误是这应该是case语句:

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