从linux shell中的'ftp'命令获取退出状态代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4899316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:43:34  来源:igfitidea点击:

Getting exit status code from 'ftp' command in linux shell

linuxbashshellftpexit-code

提问by Possa

I need to retrive the exit status code from a command line program. No worries, I used $?. But for ftp, even if it doesn't connect, it opens the ftp shell, so I'm not able to understand that the connection haven't take place.

我需要从命令行程序中检索退出状态代码。不用担心,我用了 $?。但是对于ftp,即使它没有连接,它也会打开ftp shell,所以我无法理解连接没有发生。

Try this code for understand:

试试这个代码来理解:

#!/bin/sh

ftp 1234567
OUT=$?
if [ $OUT -eq 0 ];then
   echo "ftp OK"
else
   echo "ftp Error: "$OUT
fi

exit 0

Any help? Thanks Filippo

有什么帮助吗?谢谢菲利波

采纳答案by Ruchi

You should be looking for success message from ftp command rather than looking for a status. It's "226 Transfer complete". You can confirm it with ftp manual on your system.

您应该寻找来自 ftp 命令的成功消息,而不是寻找状态。它是“226 传输完成”。您可以使用系统上的 ftp 手册进行确认。

200 PORT command successful.
150 Opening ASCII mode data connection for filename.
226 Transfer complete.
189 bytes sent in 0.145 seconds (0.8078 Kbytes/s)

Here's a sample script.

这是一个示例脚本。

FTPLOG=/temp/ftplogfile
ftp -inv <<! > $FTPLOG
open server
user ftp pwd
put filename
close
quit
!

FTP_SUCCESS_MSG="226 Transfer complete"
if fgrep "$FTP_SUCCESS_MSG" $FTPLOG ;then
   echo "ftp OK"
else
   echo "ftp Error: "$OUT
fi
exit 0

回答by ayush

some scripts do -

一些脚本做 -

ftp -n $HOST > /tmp/ftp.worked 2> /tmp/ftp.failed <<END_SCRIPT
blah blah
END_SCRIPT

EXITSTATUS=$?

if [ $EXITSTATUS != "0" ]
then
    # handle the error...
fi 

Except that the above doesn't always work - most FTP clients always exit with a status of 0. This leads to ugly "false negatives": the file transfer fails, but the script doesn't detect the problem.

除了上述并不总是有效 - 大多数 FTP 客户端总是以 0 状态退出。这会导致丑陋的“误报”:文件传输失败,但脚本没有检测到问题。

One way to verify that a file transfer took place - transfer it back:

验证文件传输发生的一种方法 - 将其传回:

#!/bin/sh

ftp -n << END_SCRIPT
open 
user  
put 
get  retrieval.$$
bye
END_SCRIPT

if [ -f retrieval.$$ ]
then
    echo "FTP of  to  worked"
    rm -f retrieval.$$
else
    echo "FTP of  did not work"
fi

回答by Andrea Spadaccini

If you need to download something and see if the download succeeded, why don't you use wget? It supports the FTP protocol.

如果您需要下载一些东西并查看下载是否成功,为什么不使用wget?它支持FTP协议。

It will report the status of the download with several return codes (quote from man page):

它将用几个返回码报告下载状态(引用自手册页):

EXIT STATUS
   Wget may return one of several error codes if it encounters problems.
   0   No problems occurred.
   1   Generic error code.
   2   Parse error---for instance, when parsing command-line options, the .wgetrc or .netrc...
   3   File I/O error.
   4   Network failure.
   5   SSL verification failure.
   6   Username/password authentication failure.
   7   Protocol errors.
   8   Server issued an error response.

回答by Chris Stratton

The last time I needed to use ftp in a script, I got so frustrated with it that I finally found a BSD-licensed ftp client source and simply modified it to give it the behavior I needed, and used that instead of the version provided with the OS.

上次我需要在脚本中使用 ftp 时,我对它感到非常沮丧,以至于我终于找到了一个 BSD 许可的 ftp 客户端源,并简单地修改了它以赋予它我需要的行为,并使用它而不是提供的版本操作系统。

Ugly, but the depth of head-level dents in the cube wall was starting to get ridiculous

丑陋,但立方体墙壁上头平凹痕的深度开始变得可笑

回答by AndresVia

Try the following scripts.

尝试以下脚本。

To copy:

复印:

#!/bin/bash
# cftp.sh
# set -x

FTPSERVER=""
FTPPORT=""
REMOTEDIR=""

[[ "$REMOTEDIR" ]] || { echo -e "Usage: 
#!/bin/bash
# mftp.sh
# set -x

FTPSERVER=""
FTPPORT=""
REMOTEDIR=""

[[ "$REMOTEDIR" ]] || { echo -e "Usage: 
$ ./cftp.sh ; echo return code: $?
Usage: ./cftp.sh <ftpserver> <ftpport> <remotedir> [file1] [file2] ...
return code: 1
$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test ; echo return code: $?
return code: 0
$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.sh mftp.sh ; echo return code: $?
return code: 0
$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test *ftp.sh ; echo return code: $?
return code: 0
$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.s ; echo return code: $?
return code: 1
$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.s mftp.s ; echo return code: $?
return code: 2
$ ./cftp.sh ftpserver 21 /mnt/disk4/d0/tes cftp.sh ; echo return code: $?
return code: 1
<ftpserver> <ftpport> <remotedir> [file1] [file2] ..." > /dev/stderr ; exit 1 ; } L=$((BASH_ARGC-3)) LOCALFILES=("${BASH_ARGV[@]:0:$L}") RETCODE=0 for LOCALFILE in "${LOCALFILES[@]}" do THISRETCODE=0 [[ -f "$LOCALFILE" ]] || THISRETCODE=1 LOCALDIR="$(dirname "$LOCALFILE")" LOCALFILENAME="$(basename "$LOCALFILE")" [[ $THISRETCODE = 0 ]] && /usr/bin/ftp -iv "$FTPSERVER" << EOF | grep -q '226 Transfer complete' || THISRETCODE=1 lcd $LOCALDIR cd $REMOTEDIR put $LOCALFILENAME EOF [[ $THISRETCODE = 0 ]] && /bin/rm -f "$LOCALFILE" || THISRETCODE=1 RETCODE=$((RETCODE+THISRETCODE)) done exit $RETCODE
<ftpserver> <ftpport> <remotedir> [file1] [file2] ..." > /dev/stderr ; exit 1 ; } L=$((BASH_ARGC-3)) LOCALFILES=("${BASH_ARGV[@]:0:$L}") RETCODE=0 for LOCALFILE in "${LOCALFILES[@]}" do THISRETCODE=0 [[ -f "$LOCALFILE" ]] || THISRETCODE=1 LOCALDIR="$(dirname "$LOCALFILE")" LOCALFILENAME="$(basename "$LOCALFILE")" [[ $THISRETCODE = 0 ]] && /usr/bin/ftp -iv "$FTPSERVER" << EOF | grep -q '226 Transfer complete' || THISRETCODE=1 lcd $LOCALDIR cd $REMOTEDIR put $LOCALFILENAME EOF RETCODE=$((RETCODE+THISRETCODE)) done exit $RETCODE

To move:

移动:

$ ./mftp.sh ftpserver 21 /mnt/disk4/d0/test cftp.sh ; echo return code: $?
/bin/rm: cannot remove `cftp.sh': Permission denied
return code: 1
$ echo foo > /tmp/bar
$ ./mftp.sh ftpserver 21 /mnt/disk4/d0/test /tmp/bar ; echo return code: $?
return code: 0
$ ls -lha /tmp/bar
ls: cannot access /tmp/bar: No such file or directory

Here are some test cases:

下面是一些测试用例:

For copying.

用于复制。

if ![ -s "$INPUT_DIR/HOP_PSA_Transactions_$BATCH_ID.csv" ]
then
    ## No Transactions file
    FAIL_TIME=`date +"%d-%m-%Y %H:%M"`
 echo "ERROR: File HOP_PSA_Transactions_$BATCH_ID.csv not found @ $FAIL_TIME" >>$LOGFILE_DIR$LOGFILE_NAME
 exit $ERR_NO_TRANS_FILE    
fi

For moving.

为动。

##代码##

Update: Remember to read man 5 netrc

更新:记得阅读 man 5 netrc

回答by Ian

Another way around this is to check if you have the file on your server post transfer!

解决此问题的另一种方法是检查您的服务器上是否有文件传输后!

Something like...

就像是...

##代码##

If it's not there then it didn't transfer successfully!

如果它不存在,那么它没有成功转移!