sftp 返回码 [英] sftp return code
问题描述
我在网站上搜索了这个问题,但找不到解决方案.问题与 sftp 有关.我正在运行一个脚本,它接受 7 个参数,执行 SSH 并在 sftp 服务器上上传文件.我提供的参数是服务器,用户,port,source_directory,target_directory,source_file 和 tager_file.如果一切顺利,文件上传没有任何错误,返回码为 0.问题是如果任何参数错误,例如 target_directory,即使脚本返回 0 作为返回值.这是脚本的外观-
I seacrhed the site for this problem but could not find solution.Problem is related to sftp.I am running a script which accepets 7 parameters,does SSH and uploads the file on sftp server.Parameters i supply are-server,user,port,source_directory,target_directory,source_file and tager_file. if everything goes fine, file is uploaded without any error and return code is 0. Problem is if any parameter is wrong, like target_directory, even then script returns 0 as return value.Here is how script looks-
typeset targetUsername=$1
typeset targetHostname=$2
typeset sftpPort=$3
typeset sourceDir=$4
typeset targetDir=$5
typeset sourceFilename=$6
typeset targetFilename=$7
typeset cmdPut="put ${sourceDir}/${sourceFilename} ${targetTempDir}/${tmpFileNam
e}"
typeset cmdRen="rename ${targetTempDir}/${tmpFileName} ${targetDir}/${targetFile
name}"
sftp ${sftpOption} ${targetUsername}@${targetHostname} <<EOF
${cmdPut}
${cmdRen}
bye
EOF
sftpStatus=$?
sftpStatus 应该返回状态.但我的状态总是为 0.任何想法,如何解决这个问题?非常感谢.
sftpStatus is supposed to return the status.But i am getting status as 0 always. Any idea, how to resove this? Thanks alot in advance.
推荐答案
你需要在批处理模式下运行 sftp
,将你的命令放在一个文件中并将它传递给 sftp
代码>.如果失败,您将获得退出代码 1,成功则获得 0.
You need to run sftp
in batch mode, by putting your commands in a file and passing that to sftp
. You will then get an exit code of 1 if there is a failure and 0 for success.
这是一些示例代码:
# create a temporary file containing sftp commands
BATCH_FILE="/tmp/$(basename $0).$$.tmp"
echo ${cmdPut} > ${BATCH_FILE}
echo ${cmdRen} >> ${BATCH_FILE}
# run sftp in batch mode
sftp -b ${BATCH_FILE} ${sftpOption} ${targetUsername}@${targetHostname}
sftpStatus=$?
rm ${BATCH_FILE}
这篇关于sftp 返回码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!