ssh stops a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh stops a while loop
# 1  
Old 03-21-2008
ssh stops a while loop

The while loop exits (early) when a simple ssh command is run.
#!/bin/ksh
#set -x
#-------------------------------------------------------------------------
# Functions Section
#-------------------------------------------------------------------------
while :
do
cat list.txt|while read TypeB; do
if [ $TypeB = 1 ]; then
echo "#1"
fi
if [ $TypeB = 2 ]; then
echo "#2"
ssh ftadmin@XXX.81.204.54 "date"
fi
if [ $TypeB = 3 ]; then
echo "#3"
fi
done
sleep 5
done
exit

cat list.txt
1
2
3

Results when the ssh statement is commented out is:
#1
#2
#3
but when I do the ssh statement:
#1
#2

Then it sleeps. Can anyone explain this and tell me how I get around it.
# 2  
Old 03-21-2008
The "cat list.txt" is providing stdin for the entire "while" loop. It is not constrained to only the read statement. ssh is eating all of the input and feeding it to "date" as stdin. And date just ignores the input. Use the -n option to ssh to stop this from happening.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to loop with ssh

I read a file (iplist.txt) ine-by-line in a loop which has the list of all the server hostnames. With each hostname read; I do ssh and fire multiple commands to gather information about that systemas shown below. #!/bin/bash while IFS='' read -r line || ]; do echo "Text read from file:... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. AIX

Need help on for loop inside ssh

Hi, I am having a file like, #cat file Jun 19 13:08 Jun 19 13:08 Jun 19 13:08 Jun 19 13:14 when I run the below comamnd locally it will work fine, IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done And the output is, HI Jun 19 13:08 HI Jun 19 13:08 HI Jun 19 13:08 HI... (1 Reply)
Discussion started by: sumanthupar
1 Replies

3. Shell Programming and Scripting

while loop stops after first iteration - remote ssh exit command problem?

I have written the following script to update some Debian boxes. #!/bin/bash mxg_hosts_file="/etc/mxg/ssh-hosts" while read line ; do mxg_host="$(echo ${line} | awk -F":" '{print $1}')" mxg_port="$(echo ${line} | awk -F":" '{print $2}')" echo "Connecting and Upgrading... (3 Replies)
Discussion started by: jelloir
3 Replies

4. UNIX for Dummies Questions & Answers

while loop stops after a function...

my ksh script is not working... i wanna remove lines in file2.txt from file1.txt # cat file1.txt this is line one this is line two this is line three this is line four this is line five # cat file2.txt this is line two this is line three # cat my_script.ksh #!/bin/ksh i=1 y=1... (8 Replies)
Discussion started by: curtis911
8 Replies

5. UNIX for Dummies Questions & Answers

ssh and for loop

Hi all, I am facing an issue while trying to access a for loop variable inside ssh. Can anyone please help me with what this issue is, the following is the code that I have -> IMPL_LOG_FOLDERS=(transaction_logs invalid_transaction_logs) sshg3 ftp_id@boxname << EOS for log_folder in... (5 Replies)
Discussion started by: anindyabecs
5 Replies

6. Shell Programming and Scripting

Script with infinite loop stops after sometime

Hi I am working on a server that is set up and maintained by a third party. It seems whenever I run bash scripts in the background (with a &) with while loops in them they seem to me killed in around 2.5 hours. ( I am running them as a normal user with no special privileges ) . Is there a... (3 Replies)
Discussion started by: pkabali
3 Replies

7. Shell Programming and Scripting

ssh inside a for loop

Hi, The requirement is to ssh to unix servers and oracle databases, to perform some monitoring activity. I'm using shell script to perfom this. I pass the server details and database to a variable ... SERVERS="SERVER1 SERVER2 SERVER3" DATABASE="DB1 DB2 DB3" for i in $SERVERS do ssh... (2 Replies)
Discussion started by: senthil3d
2 Replies

8. UNIX for Advanced & Expert Users

ssh-keygen stops working

Hey guys, I was using ssh-keygen settings for a long time to login on remote machines without password. 2 days back it suddenly stops working, i tried by reset all ssh-keygen setting but it not works. what could be the reason of this issue and how can i resolve this? (2 Replies)
Discussion started by: RohitKJ
2 Replies

9. Shell Programming and Scripting

using ssh with a for loop

How do you do an ssh on a for statement. I have done ssh on individula lines of code before with no problems. #!/usr/bin/ksh cat server_list.txt | while read line do ssh $line "for i in `lslpp -l |grep tsm` do lslpp -Lc $i |grep -v State |tr ':' ',' |awk -F, '{print $2, "," ,... (4 Replies)
Discussion started by: din
4 Replies

10. Shell Programming and Scripting

ssh and executing a for loop

Hi all, I am trying to run a script which is expected to do: on the remote machine, There are two directories /export/home/abc1,/export/home/abc2 i am trying to do, ssh SERVERNAME "for i in `ls -l /export/home/abc*|awk '{print $9}'`; do cd $i; ls -l; done" But its not working ,iam... (11 Replies)
Discussion started by: Jartan
11 Replies
Login or Register to Ask a Question