bash script - sftpbatchfile - stop on failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script - sftpbatchfile - stop on failure
# 1  
Old 04-04-2012
Question bash script - sftpbatchfile - stop on failure

Hello all,

I am currently writing a script to send files to a server over sftp. When the sftp put command succeeds it wil preform a local move from within the sftp shell to another folder (this is done so when the script is rerun no doubles will be sent).
therefore i had following sollution

Code:
for FILE in `ls -tr $tbtsftp`; do
    echo "put "$FILE
    echo "cd "$remotepath_VAN >> $commandlist
    echo "put "$FILE >> $commandlist
    echo "!echo STOP > "$SFTPCHECK_PUT_RM >> $commandlist
    echo "!rm -f "$FILE
    echo "!rm -f "$FILE >> $commandlist
    echo "!echo OK > "$SFTPCHECK_PUT_RM >> $commandlist
done

echo
echo "Executing commandlist over sftp"
echo    
sftp $sftplogon@$sftpserver -b $commandlist 2>&1

so basically first i put the file over sftp and then create a checkfile (for a following script) followed by the remove (again the checkfile will be overwritten here)
This would work perfectly if the sftpshell (or even this whole script for that matter) would stop and go in error on a failed !rm or put. This is not the case however. After the !rm fails (just tested) it just continues to put the next file.
the whole idea is that the following script would check the content of the generated checkfile and based on its content it will send us a message saying to check manually because the local remove failed.
Also i was ordered not to make a separate connection for each file (so the sftp batch is in fact nescessary)

I hope someone can help because this problem is driving me crazy.
Also i already tried the set -e option at the beginning of this script but this doesnt appear to work for an sftp batch.

thx in advance.


Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Kerberos; 04-04-2012 at 06:56 AM.. Reason: Code tags
# 2  
Old 04-04-2012
You could always have the tests and control external. If you have SFTP working, can you also SSH login to the same server as that account? Perhaps a controlling local script could put a single file with SFTP, then an audit and rename could be done by an SSH connection where you can then recognise errors.

Does that idea help?

Robin
Liverpool/Blackburn
UK
# 3  
Old 04-04-2012
sftp rm should abort on fail, according to the man page:
Quote:
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of - may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a - character (for example, -rm /tmp/blah*).
# 4  
Old 04-04-2012
Quote:
You could always have the tests and control external. If you have SFTP working, can you also SSH login to the same server as that account?
That would indeed work, except the only problem i have is that the server we send this to is a 3rth party server to which only sftp connection from our side is allowed.
On top of that the directory where we put the files (the only one we can put the files) has some service running on it that picks up the files and put them somewhere else. This pickup happens as soon as i put the file there. So i really need to be able to preform the check from my side only.

Another idea i have is to use

Code:
echo "!rm -f "$FILE >> $commandlist
echo "!echo "$?" > "$SFTPCHECK_PUT_RM >> $commandlist

but i am currently testing this :-)

thx for the response but no solution yet.

---------- Post updated at 03:05 PM ---------- Previous update was at 02:50 PM ----------

Quote:
sftp rm should abort on fail, according to the man page:
i know. It is what I would have expected, only it continues as you can see in the output after i changed the rights on the files
Executing commandlist over sftp

Code:
sftp> 
sftp> cd /home/JOBANT/VAN_SIM/
sftp> put 5414488000301.5414488000004.A00930185.CONTRL.edi
Uploading 5414488000301.5414488000004.A00930185.CONTRL.edi to  /home/JOBANT/VAN_SIM/5414488000301.5414488000004.A00930185.CONTRL.edi
sftp> !rm -f 5414488000301.5414488000004.A00930185.CONTRL.edi
rm: cannot remove `5414488000301.5414488000004.A00930185.CONTRL.edi': Permission denied
Shell exited with status 1

# 5  
Old 04-04-2012
!rm is excuting a shell command though, is it not? As opposed to sftp's builtin.

Theory only, haven't tried it myself Smilie.
# 6  
Old 04-04-2012
Quote:
Originally Posted by CarloM
!rm is excuting a shell command though, is it not? As opposed to sftp's builtin.

Theory only, haven't tried it myself Smilie.
Code:
!rm file

is what you do when trying to remove a local file while in an sftp shell. (hope i'm saying this right)
# 7  
Old 04-04-2012
Yes - I was confusing my local and remotes...

EDIT: Although it would work (abort) if you ran it the other way Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

3. Debian

Bash script to STOP installation 'if' a file exists...

Hey all, Here's my dilemma: 1. I'm a newbie at scripting! 2. I need to create a script that checks: If a file size is equal to zero, then stop the installation. Is there a way to do this or am I wasting my time??? Thanx in advance! :b: (2 Replies)
Discussion started by: thazsar
2 Replies

4. UNIX for Dummies Questions & Answers

boot up failure unix sco after power failure

hi power went out. next day unix sco wont boot up error code 303. any help appreciated as we are clueless. (11 Replies)
Discussion started by: fredthayer
11 Replies

5. Shell Programming and Scripting

Stop child script by stoping parent script

Hi everyone, I have this problem with a script I'm writting. I want to execute a code running in the background several times through a script. I am writting it like that parent_script for a in 1 2 3 4 5 do exec test -n $a done What I want to do is when parent_script is killed,... (0 Replies)
Discussion started by: geovas
0 Replies

6. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

7. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

8. Shell Programming and Scripting

script don't stop

Hello everybody! I am new to this and I am trying to change a script in an open source program that plots some offset vectors and then calls a postscript viewer. I have commented away the call for the postscript viewer but somehow the script doesn't return to the shell prompt. I cant figure out... (3 Replies)
Discussion started by: larne
3 Replies

9. UNIX for Dummies Questions & Answers

Stop a shell script

Hi, I am writing a bash shell script. How can I tell it to stop. For example, I would like to have something similar to the following: mike=1 if ; then STOP THE SCRIPT fi (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

Script does not stop when doing a read

Hi Folks, I have been trying to create a script wherein after it reads a certain number of data, it will pause and ask the user if he wants to continue or not. However, it seems that when it is supposed to read the user's answer, the script will go into a loop. What is wrong with my script here?... (7 Replies)
Discussion started by: rooseter
7 Replies
Login or Register to Ask a Question