从ftp服务器下载文件时出现错误

时间:2019-06-27 17:56:57

标签: shell

语法不正确。我想在ftp脚本失败时添加一条错误消息。虽然下载了文件,但在正确下载文件后获得成功消息的最后几行有错误

. $HOME/env
. $LIB_PATH/miip_functions.shl
dataset="'$1'"
filename=$2
file=basename $2
HOST=ftp.test
USER=test
 PASSWORD=test1

logMessage "Starting FTP download of $file."

ftp -inv $HOST <<!
user $USER $PASSWORD
get $dataset $filename
 bye
  !
  rtn=$?
   if [ $rtn -eq 1 ] ; then 
  logMessage DLOAD "Completed FTP download of $file."
 else
  logMessage ERROR "Download of $file failed."

当ftp加载失败时,我需要添加消息。

1 个答案:

答案 0 :(得分:0)

次要问题:dataset="'$1'"被过度引用;写dataset="$1"

主要问题:ftp的出口状态没有按照您需要的方式设置,因此您必须检查其输出。例如:

logMessage "Starting FTP download of $dataset."
complete=0
(echo user $USER $PASSWORD
 echo get $dataset $filename
) | ftp -inv $HOST | while read code rest
                     do echo $code $rest
                        if [ "$code" = 226 ]; then complete=1; fi
                     done
if [ $complete -eq 1 ]; then 
  logMessage DLOAD "Completed FTP download of $dataset."
else
  logMessage ERROR "Download of $dataset failed."
fi

(226是Transfer complete消息的标识代码。)

  

文件已下载,但由于数据集失败而出现logMessage

我注意到有些外壳程序在子外壳程序中执行最后一个管道命令,因此complete=1在父外壳程序中无效。以下版本返回complete值作为退出状态,即使在这些shell中也可以使用。

logMessage "Starting FTP download of $dataset."
complete=0
(echo user $USER $PASSWORD
 echo get $dataset $filename
) | ftp -inv $HOST | (while read code rest
                      do    echo $code $rest
                        if [ "$code" = 226 ]; then complete=1; fi
                      done && exit $complete)
if [ $? -eq 1 ]; then 
  logMessage DLOAD "Completed FTP download of $dataset."
else
  logMessage ERROR "Download of $dataset failed."
fi