Visit Our UNIX and Linux User Community


[Solved] While read line and if statement not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] While read line and if statement not working
# 1  
Old 09-09-2013
[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list.

Code:
while read line ;do
        hostname=${line//\"}
        a=`ssh user@$hostname uptime;echo $?`
        if [[ "$a" = "255" ]];then
                dt=`date`
                touch /home/user/email.txt
                echo "To:user@userdomain.com" >> /home/user/email.txt
                echo "Subject: URGENT - $hostname is down" >> /home/user/email.txt
                echo "Content-Type: text/plain" >> /home/user/email.txt
                echo "X-Priority: 1 (Highest)" >> /home/user/email.txt
                echo "X-MSMail-Priority: High" >> /home/user/email.txt
                echo "Connection Failed at: $dt" >> /home/user/email.txt
                /usr/sbin/sendmail -t < /home/user/email.txt
                rm -f /home/user/email.txt
        fi
        rm -f /home/user/email.txt
done < "/home/user/servers.txt"

It connects to the first server in the list just fine, but will not try the 2nd or 3rd entry in the servers list.
# 2  
Old 09-09-2013
Hello,

This problem is link to ssh command that close stdin in standart mode when quit (here, your file "servers.txt").
To resolve this, you can use the option -o BatchMode

Regards.
# 3  
Old 09-09-2013
Try using the ssh -n option.
This User Gave Thanks to Scott For This Post:
# 4  
Old 09-09-2013
Please use this line.

Code:
a=`ssh -o BatchMode=yes user@hostname uptime;echo $?`

# 5  
Old 09-09-2013
Both the -n and -o BatchMode=Yes did the trick, thank you very much.
# 6  
Old 09-09-2013
Unless the while loop is fed by a pipe, there is another solution: use a file descriptor other than the default < == 1<
Code:
while read line <&3; do
  ...
done 3< "/home/user/servers.txt"

# 7  
Old 09-10-2013
Just adding my two cents here. I've been there myself, and solved it by closing the ssh connection after connecting to each server and doing what I had to do, so during the next run of the while loop there was no existing connection to "interfere" with the new one I was trying to establish. Just add the word exit before closing the loop, like so:
Code:
        rm -f /home/user/email.txt
exit
done < "/home/user/servers.txt"

Hope it helps.
If you have found the answer to your question and everything is working to your satisfaction, please mark this thread as solved for the future reference of other users. Thank you.
This User Gave Thanks to gacanepa For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #905
Difficulty: Easy
Generally speaking, Unix systems are commercial, closed source and propriety operating systems so it is expensive to customize for supercomputing projects. Linux, on the other hand, is free and easier to customize.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

While read -a line not working in ksh

while read -a line; this is not working in ksh. what is the equivalent of this in ksh. read: -a: unknown option (2 Replies)
Discussion started by: archana25
2 Replies

3. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

4. Shell Programming and Scripting

[Solved] Not able to read the next line from a file after doing ssh

Hi, I am trying to write a code, where it reads the jobnames from a file and checks for the logs in the server. If the log is not found in the first server, then it will ssh to the 2nd server and get the details from there. I will need to save the date in a variable. But the problem is that,... (0 Replies)
Discussion started by: ajayakunuri
0 Replies

5. Shell Programming and Scripting

While loop read line not working

Hi, I am trying to read a file line by line inside of a while loop. This while loop is part of a here document. while read line do ssh -t $2@$remotehost <<REMOTE ls path/to/dir > $path_to_dir while read line1 do echo "LINE --- $line" done... (4 Replies)
Discussion started by: mnanavati
4 Replies

6. Shell Programming and Scripting

[Solved] Read a .gz file line by line without using gzcat

Hi all Is there a way to read and process a gzip file line by line similar to a text file without using gzcat.. while processing a text file we will usually use the logic exec<sample.txt while read line do echo $line done Is there a similar way to process the gz file in the same... (4 Replies)
Discussion started by: aikhaman
4 Replies

7. Shell Programming and Scripting

[Solved] Read a line from one string till to another.... Unix scripting..

So i have a file which contains paths to JPG images separated by a space. I have to separate them each path to another file. So, I have to search all strings that start from /home/ and ends with .jpg or .png Then write each one to another file... Can you please help me on doing this???:cool: (11 Replies)
Discussion started by: hakermania
11 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

10. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

Featured Tech Videos