× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



hi Phil,

Okay, I see the problem. You're not running your sftp as a batch file transfer, and therefore sftp isn't doing it's normal 'batch mode' error handling.

Here's what you have:

#!/bin/sh
fromfile=$1
tofile=$2
user=$3
tgtsys=$4
totmpfile=$5
errlvl=$6
sftp $errlvl $user@$tgtsys<< EOT
put $fromfile $totmpfile
rename $totmpfile $tofile
quit
EOT

sftp has a "batch mode" feature, wherein you pass the -b switch, followed by an IFS path name to a file containing the script to run. But you aren't using that mode... you're simply using a here doc that is subtituted for keyboard input. The result is that sftp thinks its operating interactively, and therefore doesn't do it's batch-mode error handling.

The two possible solutions are to

1) Switch to using a batch script.
2) Use an Expect script, verify the correct responses, etc, manually.

Since #1 is the easier solution, here's how I'd code your script:

#!/bin/sh
fromfile=$1
tofile=$2
user=$3
tgtsys=$4
totmpfile=$5
errlvl=$6
eval tmpf=/tmp/script$$
cat - > $tmpf << EOT
put $fromfile $totmpfile
rename $totmpfile $tofile
quit
EOT
success=0
sftp -b $tmpf $errlvl $user@$tgtsys || success=$?
rm $tmpf
exit $success


As you can see, I'm still using a here doc (since I wanted to change your code as little as possible), but I'm writing it to a file on disk instead of redirecting the input to sftp. (Note the $$ in the filename will be replaced with the process ID of the script, that way multiple copies of this can run without a clash on the filename)

Then I run sftp, and I specify -b, followed by the temp file I just created, and it runs sftp in batch mode.

The "|| success=$?" causes a variable named 'success' to be set to the exit status of the sftp command if it fails. After deleting the temp file, I return that exit status, so my caller can check whether it failed. (This is how I propagate the error)

HTH

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.