If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > while loop on remote server

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-09-04, 13:54
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
while loop on remote server

How would I go about making a while loop execute on a remote sever and keep doing so until a specific # is met? I've tried this several different ways, using until, adding the dir after the "while". I'm probably missing something here.

blah="echo * | wc -w"
m=/tmp/read

ssh -f -t <hostname>

cd $m

while [ "$blah" = 40 ]
do
scp *.ask <hostname>:/tmp
sleep 40
done

TIA!!

Last edited by kasparaitis; 03-09-04 at 15:16.
Reply With Quote
  #2 (permalink)  
Old 03-09-04, 15:50
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try :

while [ $blah = 40 ]

or

while [ "$($blzh)" = 40 ]
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 03-09-04, 16:40
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
Quote:
Originally posted by aigles
Try :

while [ $blah = 40 ]

or

while [ "$($blzh)" = 40 ]

Thanks! The second while command worked.

Last edited by kasparaitis; 03-09-04 at 17:36.
Reply With Quote
  #4 (permalink)  
Old 03-09-04, 18:16
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
Another question. Now this script should in theory scp all the files and exit the loop if $blah is = to 40 correct? Otherwise the script will continue in the loop scp'ing files if $blah is less than 40. I'm asking this because my script re-runs even if $blah is = to 40. Did I write this wrong?

TIA!

code:

blah="echo * | wc -w"
m=/tmp/read

ssh -f -t <hostname>

cd $m

while [ "$($blah)" = 40 ]
do
scp *.ask <hostname>:/tmp
sleep 40
done

exit 0

TIA!!
Reply With Quote
  #5 (permalink)  
Old 03-11-04, 12:47
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
bump
Reply With Quote
  #6 (permalink)  
Old 03-11-04, 16:20
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
The result of the 'echo *|wc -w' containts leading spaces before the count of words. If there are 40 files the resuly is ' 40' and not '40'.

Modify your test to make numic comparison :
Code:
while [ "$(blah)" -eq 40 ]
__________________
Jean-Pierre.
Reply With Quote
  #7 (permalink)  
Old 03-11-04, 16:26
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
Quote:
Originally posted by aigles
The result of the 'echo *|wc -w' containts leading spaces before the count of words. If there are 40 files the resuly is ' 40' and not '40'.

Modify your test to make numic comparison :
Code:
while [ "$(blah)" -eq 40 ]
Thanks! I made the changes you requested and got this:

<hostname> code
Begin code processing on\n Thu Mar 11 13:22:55 PST 2004

Authentication successful.
Copying files from <hostname>

code: blah: command not found
code: [: : integer expression expected
Copy Complete

end code processing on\n Thu Mar 11 13:38:28 PST 2004

-if I use 'until' instead of 'while' it gives the same errors, except it ends up scp'ing the files through and doesn't exit the loop.

Last edited by kasparaitis; 03-11-04 at 16:42.
Reply With Quote
  #8 (permalink)  
Old 03-14-04, 21:37
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
bump
Reply With Quote
  #9 (permalink)  
Old 03-15-04, 04:46
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Code:
while [ "$($blah)" -eq 40 ]
__________________
Jean-Pierre.
Reply With Quote
  #10 (permalink)  
Old 03-16-04, 12:58
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
I've tried every one of the following: along with a few others.

while [ "$($blah) -eq 40 ]
while [ "$blah -eq 40" ]
while [ "$(blah) -eq 40 ]

and so on. With the while [ "$($blah) -eq 40 ] statement I received this error:

code: [: * | wc -w: integer expression expected
Copy Complete
Reply With Quote
  #11 (permalink)  
Old 03-16-04, 16:38
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Code:
while [ "$(eval $blah)" -eq 40 ]
__________________
Jean-Pierre.
Reply With Quote
  #12 (permalink)  
Old 03-16-04, 16:54
kasparaitis kasparaitis is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
Quote:
Originally posted by aigles
Code:
while [ "$(eval $blah)" -eq 40 ]
It gives no error when I use 'eval' but it also does not copy anything over.
Reply With Quote
  #13 (permalink)  
Old 03-17-04, 03:18
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Replace '"$(eval $blah)" by $(echo * | wc -c) in your test.

Another solution is to create a function instead of using a variable:

FileCount() {
echo * | wc -c
}

while [ $(FileCount) -eq 40 ]

Execute your script with the -x option and analyze the result to determine where is the problem.
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On