While loop correction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop correction
# 1  
Old 08-22-2013
While loop correction

I am checking if ssh is working fine on remote server proceed for next step until keep check ssh port, below is shell script.

Code:
#!/bin/bash

MON="SSH"

x=1
while [ "$MON" == "SSH" ]
do
  echo "SSH Checking Status"
  nc 192.168.1.20 -w 2
done

# 2  
Old 08-22-2013
The while loop you have written is infinite loop, you need to add conditions to validate whether the ssh check was successful or not and if successful, break from the loop and proceed otherwise continue the loop.
# 3  
Old 08-22-2013
i want to run this infinite. How?
# 4  
Old 08-23-2013
Code:
while true
do
        # if exit code not 0 then next cmd also. break = end the while loop
        nc 192.168.1.20 -w 2 >/dev/null 2>&1  || break
        
done

Is same as
Code:
ok=1
while ((ok==1))
do
        # test cmd exist status
        if nc 192.168.1.20 -w 2 >/dev/null 2>&1 
        then 
             # still ok
             ok=1
        else
             ok=0
        fi
done

Is same as
Code:
ok=1
while ((ok==1))
do
        nc 192.168.1.20 -w 2 >/dev/null 2>&1 
        # save last cmd exit status
        stat=$?
        # and test it
        if ((stat==0))
        then 
             # still ok
             ok=1
        else
             ok=0
        fi
done

Or if you like to save cmd output and test it, then something
Code:
ok=1
while ((ok==1))
do
        output=$( nc 192.168.1.20 -w 2 2>/dev/null )  # err msg redirect to the /dev/null
        stat=$?
        # stat=0 =ok, other values not ok
        case "$output" in
              "") # empty
                  ok=1
                  ;;
              XXX*) ok=1 ;;
              *) ok=0 ;;
        fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk correction

if awk 'BEGIN{if('$RSS'>='1000')exit 0;exit 1}' ; then dowhatever fi the above code works great when i'm comparing numerical figures. but when i try to do a string comparison, it does not work: if awk 'BEGIN{ /'$RSS'/ ~ /DB01/ exit 0;exit 1}' ; then dowhatever fi can this... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Perl Script date correction

Need assistance for the below script. Little details: based on the hours specified it calculates future or past date,time. This script works absolutely great . But at one point when i wante specify future date with hours at 1682 hours ...1 hours skips .Dont know why the calculation misses 1... (2 Replies)
Discussion started by: ajayram_arya
2 Replies

3. UNIX for Dummies Questions & Answers

Correction in Syllabus

Sir, I have the following modules to learn. The details of the modules in the syllabus published by the university are given below. There are some huge spelling mistakes in the syllabus. I have corrected few of them. Can u please quote the spelling mistakes in the syllabus if any and kindly... (1 Reply)
Discussion started by: achuthmama
1 Replies

4. Shell Programming and Scripting

small script correction

input cz1 87942437 87952030 M_001144992 0 + 87942537 87949664 0 3 710,114,2506, 0,2725,7087, script awk '{ n11 = split($11, t11, ",") n12 = split($12, t12, ",") for (i = 0; ++i < n11;) { s12 = $2 + t12 print $4"_xon"i, "\t",$4"_xon"i,"\t", $1,... (1 Reply)
Discussion started by: quincyjones
1 Replies

5. Shell Programming and Scripting

cron job - shell code correction

Hi I have a website that is having problem with cron jobs... I have a cron job set up to go to a page with this code... <? include('config.php'); if($_sys->bible_email_frequency == 'DAILY') { $u = new user(); $u->send_bible_email(); } ?> If i send my browser to this page... (2 Replies)
Discussion started by: whybelieve
2 Replies

6. UNIX for Dummies Questions & Answers

Extract data correction???

Hi I have file "test.txt" i need to extract the data as below from this file. /home/cvs/gkc ->awk -v RS= '{print $(NF-2),$(NF-1),$NF}' test.txt Buildfile: build.xml Buildfile: build.xml Created dir: /dist/advance/2.0.4/20060926-0413 awk: The field -1 must be in the range 0 to 199. ... (6 Replies)
Discussion started by: gkrishnag
6 Replies

7. UNIX for Dummies Questions & Answers

End of Error correction

I am being told by serveral different departmens that serveral of the products we use such as Oracle, IONA orbix 3.0, Veritas NetBackup and Tivoli Maestro are about to end support of Error Correction Support. Could some please help me understand what this actually means. Thanks Kristian (1 Reply)
Discussion started by: kristian
1 Replies
Login or Register to Ask a Question