sftp return code

烈酒焚心 提交于 2020-01-03 17:45:33

问题


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 is supposed to return the status.But i am getting status as 0 always. Any idea, how to resove this? Thanks alot in advance.


回答1:


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.

Here is some sample code:

# 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}


来源:https://stackoverflow.com/questions/5646349/sftp-return-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!