LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2007, 10:08 AM   #1
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Strange behaviour in bash - while loop


Hi

I have a textfile with servers that is being read by a backup script in a while loop. Here is it, simplified a bit:

servers.txt:
Quote:
server1 database1
server2 database2
and so on...

This works fine:
Code:
cat servers.txt |
while read line
do
  server=$(echo $line |awk '{print $1}')
  database=$(echo $line |awk '{print $2}')
  echo "Server: $server - Database: $database"
done
Then I add code that actually do something:
Code:
cat servers.txt |
while read line
do
  server=$(echo $line |awk '{print $1}')
  database=$(echo $line |awk '{print $2}')
  echo "Server: $server - Database: $database"
  ssh $server "mysqldump -u root --password=mysecret $database" >$database.sql
  echo "Ok"
done
The problem is, when I add the ssh command, the script stops after the first line. The database dump is made, "Ok" is printed, but the loop stops. Somehow the ssh command messes up the reading of the textfile it seems.

Is there perhaps a different way to read thru a textfile, without using pipes?
 
Old 06-08-2007, 10:23 AM   #2
avatarez
LQ Newbie
 
Registered: Jun 2007
Location: Buenos Aires, Argentina
Distribution: Ubuntu 7.04 / Fedora Core 6
Posts: 2

Rep: Reputation: 0
I dont know if this is the best way, but this is how i do it and works for me

(
read LINE;

while [ -n "$LINE" ]
do

# do the same things you were doing before

read LINE;
done
) < servers.txt
 
Old 06-08-2007, 10:33 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Original Poster
Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Thank you. I tried it, but it behaves the same. Without the ssh command, it works fine, but with it, it stops after the first line.

I guess I have to read the textfile into an array and then loop thru it, as it seems the piping gets messed up by the ssh. So I'll try to change it to an array then.
 
Old 06-08-2007, 12:15 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

ssh inherits stdin, which makes the pipe (cat ... | while ...) useless. Use ssh's -n option to get around this behavior. I.e:

Code:
cat servers.txt |
while read line
do
  server=$(echo $line |awk '{print $1}')
  database=$(echo $line |awk '{print $2}')
  echo "Server: $server - Database: $database"
  ssh -n $server "mysqldump -u root --password=mysecret $database" >$database.sql
  echo "Ok"
done
Hope this helps.
 
Old 06-08-2007, 02:11 PM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Original Poster
Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Thank you!

I had already changed the script to read into an array. It was a bit of work since variables read in a pipe would lose their value when done. So I changed it to ksh and it worked.

Your solution is a lot simpler, and also, now I understand why it behaved the way it did.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
strange behaviour marsques Slackware 11 02-15-2006 06:05 PM
strange behaviour when bash starts Y0jiMb0 Linux - General 8 07-09-2004 06:29 AM
Strange Behaviour mikeyt_3333 Linux - General 4 08-06-2001 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration