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

后端 未结 6 1172
半阙折子戏
半阙折子戏 2020-12-28 23:08

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

6条回答
  •  悲&欢浪女
    2020-12-28 23:09

    some scripts do -

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

    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.

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

    #!/bin/sh
    
    ftp -n << END_SCRIPT
    open $1
    user $2 $3
    put $4
    get $4 retrieval.$$
    bye
    END_SCRIPT
    
    if [ -f retrieval.$$ ]
    then
        echo "FTP of $4 to $1 worked"
        rm -f retrieval.$$
    else
        echo "FTP of $4 did not work"
    fi
    

提交回复
热议问题