Visit Our UNIX and Linux User Community


while loop to copy on failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while loop to copy on failure
# 1  
Old 12-10-2007
while loop to copy on failure

I'm trying to do an automated SCP (passwordless auth is already set up) from a bash script...

My problem is that on the receiving end, the computer will sometimes NOT actually get my files. BUT if I loop enough times (by hand at the moment) calling an 'ls' on those files, I can see if the files got there, otherwise resend.

This works, believe it or not... maybe somethings screwed up with my SCP?

Either way, I want to now put this functionality into a script, so I can just get the 'ls' of my files, see if there are 4 lines (pre-grepped for just files, no headers) -- aka 4 files -- and if there are, continue... else loop.

Pseudo-code:
Code:
do {
  scp veryOriginalName.1 root@myComp:/tmp
  scp veryOriginalName.2 root@myComp:/tmp
  scp veryOriginalName.3 root@myComp:/tmp
  scp veryOriginalName.4 root@myComp:/tmp

  VAR = ssh root@myComp "ls -l /tmp/veryOriginalName.*" | grep "-" | wc -l
} while {
  VAR != 4
}

# 2  
Old 12-10-2007
Power

Quote:
Originally Posted by jjinno
I'm trying to do an automated SCP (passwordless auth is already set up) from a bash script...

My problem is that on the receiving end, the computer will sometimes NOT actually get my files. BUT if I loop enough times (by hand at the moment) calling an 'ls' on those files, I can see if the files got there, otherwise resend.

This works, believe it or not... maybe somethings screwed up with my SCP?

Either way, I want to now put this functionality into a script, so I can just get the 'ls' of my files, see if there are 4 lines (pre-grepped for just files, no headers) -- aka 4 files -- and if there are, continue... else loop.

Pseudo-code:
Code:
do {
  scp veryOriginalName.1 root@myComp:/tmp
  scp veryOriginalName.2 root@myComp:/tmp
  scp veryOriginalName.3 root@myComp:/tmp
  scp veryOriginalName.4 root@myComp:/tmp

  VAR = ssh root@myComp "ls -l /tmp/veryOriginalName.*" | grep "-" | wc -l
} while {
  VAR != 4
}


If I understand the problem correctly Smilie, for kourne-shell, can do something like below:

#!/bin/ksh

file_cntr=0
while [[ $file_cntr != 4 ]]; do
# do copy file
scp ...

# do get file_cntr value
file_cntr=`ssh root@myComp "ls -l ...`
done
# 3  
Old 12-10-2007
Quote:
Originally Posted by jjinno
Code:
do {
  scp veryOriginalName.1 root@myComp:/tmp
  scp veryOriginalName.2 root@myComp:/tmp
  scp veryOriginalName.3 root@myComp:/tmp
  scp veryOriginalName.4 root@myComp:/tmp

  VAR = ssh root@myComp "ls -l /tmp/veryOriginalName.*" | grep "-" | wc -l
} while {
  VAR != 4
}

Why not use the error code returned by scp to indicate the success or failure of the operation?

Also, you can transfer multiple at the same time which would save time on connection and negotiation....

Code:
scp veryOriginalName.1 \
       veryOriginalName.2 \
       veryOriginalName.3 \
       veryOriginalName.4 root@myComp:/tmp/
if test "$?" = "0"
then
     echo success
     VAR = `ssh root@myComp "ls -l /tmp/veryOriginalName.*" | grep "-" | wc -l`
else
     echo failure
fi


Previous Thread | Next Thread
Test Your Knowledge in Computers #940
Difficulty: Medium
Ping was not included in 4.3BSD.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Failure using regex with awk in 'while read file' loop

I have a file1.txt with several 100k lines, each of which has a column 9 containing one of 60 "label" identifiers. Using an labels.txt file containing a list of labels, I'd like to extract 200 random lines from file1.txt for each of the labels in index.txt. Using a contrived mini-example: $ cat... (8 Replies)
Discussion started by: pathunkathunk
8 Replies

3. 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

4. Shell Programming and Scripting

KSH Script -- loop and data copy question

I am trying to write a script that will allow me to train others with commands that I run manually by only allowing the exact command before continuing onto the next set of commands. Here is where I come into an issue. I have changed the directories for this post. Software we run creates files... (2 Replies)
Discussion started by: hurtzdonut
2 Replies

5. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

6. UNIX Desktop Questions & Answers

Copy all files in 1 directory to another usinge for-in loop

I was looking to get some help with copying files in one directory to another using a for-in loop. My script file is called copyfile and here is what I have: for file in $(ls -a $1) do cp $file ~/dir-2 done When I run copyfile dir-1 this is what I get cp: omitting directory `.'... (1 Reply)
Discussion started by: Trinimini
1 Replies

7. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

8. Shell Programming and Scripting

Loop to copy like files

Hi, I need to write a script that copies all .zip files in the subdirectories of ~100 folders. No clue how to write a loop that goes into each folder, searches for a .zip file, and copies it and extracts it to a unique location. I imagine something like cp -f /home/folder1/*.zip... (6 Replies)
Discussion started by: nez
6 Replies

9. Shell Programming and Scripting

copy command failure

A cp command failure occured on our production system. A shell script copies a source file to a temporary directory - cp <source file on mount point 1> <dest path on mount point 2> The same script ran successfully for another file, about 3mins after the failure. The script did not have a... (1 Reply)
Discussion started by: mynix
1 Replies

10. Shell Programming and Scripting

Unzip, copy, and delete loop

Hey there, I am trying to move zipped text files from a remote server to a remote tape storage facility, through my home directory. What I want to do is get the zip file (using scp), unzip it, copy the output text file which was inside (using rfcp) to the tape storage server, and then delete... (3 Replies)
Discussion started by: spyne
3 Replies

Featured Tech Videos