simple while loop script to check a connection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple while loop script to check a connection
# 1  
Old 10-08-2012
Bug simple while loop script to check a connection

Hi,

I am completely new to shell scripting. Basically I am wanting to create a simple while loop script to check if a network connection is available from 'netstat -a' and if its present then output a character to the serial port. I think i am nearly there.. but need some assistance.. this is what i have so far..(ip address an example)..
Code:
#!/bin/bash
CONNECTED=$(netstat -a | grep -c 1.1.1.1)
while [echo $CONNECTED -gt 0]; do
  echo -n p >/dev/ttyS1;
  sleep 20;
done

Many thanks

Zippy

Last edited by Franklin52; 10-09-2012 at 03:09 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-08-2012
You don't need to echo CONNECTED

Now check..

Code:
#!/bin/bash
CONNECTED=$(netstat -a | grep -c 1.1.1.1)
while [ "$CONNECTED" -gt 0 ]; do
echo -n p >/dev/ttyS1;
sleep 20;
CONNECTED=$(netstat -a | grep -c 1.1.1.1)#I think you need this also inside the while loop. so it can change the value and reflects it..
done

This User Gave Thanks to pamu For This Post:
# 3  
Old 10-09-2012
thanks for the quick reply.. on the road right now but will check later

---------- Post updated 10-09-12 at 02:53 AM ---------- Previous update was 10-08-12 at 11:18 AM ----------

works a treat pamu thanks

---------- Post updated at 02:54 AM ---------- Previous update was at 02:53 AM ----------

works a treat thanks...
# 4  
Old 10-09-2012
You don't even need the variable:
Code:
while netstat -antu|grep  -q 10.1.1.1; do something; done

The -tu options to netstat will restrict its output to tcp/udp sockets only.
# 5  
Old 10-09-2012
Hi I have kind of modified things to change for an application..
Code:
#!/bin/bash
CONNECTED=$(ps -ef | grep -c APP1)
while [ "$CONNECTED" -gt 1 ]; do
  echo -n p >/dev/ttyS1;
  echo $CONNECTED
  sleep 10;
  CONNECTED=$(ps -ef | grep -c APP1)
done

one thing I don't know how to get right is exit status.. so when $CONNECTED is less than or equal to 1 I want the loop to continue but not send the output to the serial port. Right now if $CONNECTED is = 1 the script stops..

any help appreciated

Last edited by Franklin52; 10-10-2012 at 04:24 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 10-09-2012
Quote:
Originally Posted by zippyzip
I want the loop to continue but not send the output to the serial port.
Is there any specific requirement for this..?

Please provide details what is purpose behind this so we can try to provide better sollution to you..Smilie
# 7  
Old 10-09-2012
Hi pamu

Thanks for your help so far. Essentially if program A is running i want to continually send serial keepalives to the serial port..(something is scooping this output up fine) if the program fails i want the output to stop.. if the program resumes i want it to continue

Thanks

Zippy
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Script to check the FTP connection

Hi, I need a script which check the FTP connections, if its working fine then no issue. Incase not working fine then Trigger the mail. Please help on this. Thanks (4 Replies)
Discussion started by: Keshav Kalra
4 Replies

2. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

3. Shell Programming and Scripting

Simple check login script

Hello, I just started with shell and i am doin simple script to check if certain user is logged in. Script takes 3 args. name sleep time and quit value if there is q for quit then date and user name is printed to file, not to screen. so far i have got this... login() { while :... (3 Replies)
Discussion started by: shelllo
3 Replies

4. Shell Programming and Scripting

Check the connectivity of the DB through script, exit if no connection

check the connectivity of the DBs through script, script should exit if no connection and display the output as below. connectivity for DB1 is OK connectivity for DB2 is OK connectivity for DB3 is FAILED for DB in 1 2 3 do (sqlplus -s... (5 Replies)
Discussion started by: only4satish
5 Replies

5. Shell Programming and Scripting

Simple Script to Check running Process

#!/bin/sh CHECK='ps aux | grep start.jar | grep -v grep | wc -l' if then /usr/local/jre-1.7.0/bin/java - jar start.jar & else fi Could anybody advise whats up with this code im trying to put this in as a cron job to check that solr search engine is running every 10secs and if... (10 Replies)
Discussion started by: will_123
10 Replies

6. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

7. Shell Programming and Scripting

Simple LOOP script help

I am trying to create a simple ksh loop script. What I've pasted below is not working. Any help is appreciated. typeset -i count typeset -i tcount count=0 tcount=0 while $tcount >= 11 do print "\$count is $count" pwd ls -l sleep 30 $(( $tcount = $count + 1 )) ... (5 Replies)
Discussion started by: musikman65
5 Replies

8. Shell Programming and Scripting

how to check DB connection in unix shell script

In the following script I need to find whether DB is established successfully or not. I need to find and display it. If <DB is connected successfully> then echo "DB connection success" else echo "DB connection failure" fi In the same way I need to do it for DB terminate. I don't... (1 Reply)
Discussion started by: kmanivan82
1 Replies

9. UNIX for Dummies Questions & Answers

Bash Script to check Remote Host Connection

Hi all, Can anyone tell/guide me how to check remote host is up/running using bash script? Thanks. Zulfiqar (5 Replies)
Discussion started by: zulfikarmd
5 Replies

10. Shell Programming and Scripting

Simple script loop question

Hey all. Thanks in advance for any help you can give, hopefully this is an easy one. I want to create a loop to run a simple performance monitor like vmstat and record it to a file, but have very limited scripting skills (obviously). Starting with this... date >> /var/log/perfmon.log vmstat... (2 Replies)
Discussion started by: mattlock73
2 Replies
Login or Register to Ask a Question