LinuxQuestions.org
Help answer threads with 0 replies.
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 04-21-2009, 12:45 PM   #1
mravljica
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Rep: Reputation: 0
Bash shell scripting - read choose next


Hi!

I am new to bash programing but the learning is in progress.
I have made a script which at scheduled times update local files from remote servers. I have created a file which contains server names eg.:
server1
server2
server3
server4
...

How do I make my script to choose the first server and if the first one is unreachable to select the next one, etc. and when it comes to the server which is reachable then start making update and then exit.
I think I need to use some sort of loop (for, while)???

I have only the part that check for reachability of the server:
-----------
case `ping -qnc 1 $server 2>&1` in
*'100% packet loss'*)
echo "Server unreachable."
exit 1
*'0% packet loss'*)
echo "Server ready"
$update
;;
esac
-----------

Thank you for your help in advance!

Regards,
Sasa

Last edited by mravljica; 04-21-2009 at 12:53 PM.
 
Old 04-21-2009, 02:21 PM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I usually just use the:

Code:
while read line
do

done < file
to read files line by line for processing, especially when the file is short. So you could put your code inside that loop. I'm not sure about using ping for this, I suppose you could, but what if the server doesn't accept pings ?
 
Old 04-22-2009, 04:24 AM   #3
mravljica
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Ping will be accepted. So if I understood wright the script should look like the following:

Code:
servers = servers.txt

read $servers

while read line
do
  case `ping -qnc 1 $line 2>&1` in
       *'100% packet loss'*)
         echo "Server unreachable."
         exit 1
       *'0% packet loss'*)
         echo "Server ready"
         $update
         exit 0
       ;;
  esac
done < $servers
exit 0

Is this OK?

Regards,
Sasa

Last edited by mravljica; 04-22-2009 at 04:33 AM.
 
Old 04-22-2009, 05:16 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
What is the point of 'read $servers' ?

The other line should be:

Code:
servers=servers.txt
 
Old 04-22-2009, 06:18 AM   #5
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
Instead of
Quote:
case `ping -qnc 1 $line 2>&1` in
*'100% packet loss'*)
echo "Server unreachable."
exit 1
*'0% packet loss'*)
echo "Server ready"
$update
exit 0
;;
esac
I would suggest
Quote:
[[ `ping -qnc 1 $line 2>&1` ]] && ( echo "Server Ready";exit 0 ) || ( echo "Server Unreachable"; exit 1 )
 
Old 04-22-2009, 06:37 AM   #6
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
Hmm...i guess you would not want to exit if the server is unreachable, rather just echo and at the end, instead of "exit 0" user "exit 1" as control would go to the end only if all servers are unreachable.
 
Old 04-22-2009, 07:05 AM   #7
mravljica
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Libu View Post
Hmm...i guess you would not want to exit if the server is unreachable, rather just echo and at the end, instead of "exit 0" user "exit 1" as control would go to the end only if all servers are unreachable.
You are wright. If the server is unreachable then I want to check the next in the line till the one that is reachable.
 
Old 04-22-2009, 08:17 AM   #8
mravljica
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
The script is partially working.

Code:
while read line
do
  case `ping -qnc 1 $line 2>&1` in
       *'100% packet loss'* )
         echo "Server unreachable"
       ;;
       *'0% packet loss'* )
         echo "Server ready"
         # run function update
         update
         exit 1
       ;;
  esac
done < $servers
exit 0
partially I mean because it just run the loop but no echo is displayed if server is ready or unreachable. I ahve ran bash -x and only the loop is working then exit 0.

What am I missing here?

Regards,
Sasa
 
Old 04-22-2009, 09:14 AM   #9
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
This is what I would do:
Code:
#!/bin/sh

servers=servers.txt

while read line
do
  if test $(ping -nc 1 $line | grep packet | awk '{ print $6 }') == '0%'
  then
    echo "Server ready"
    # run function update
    update
	 
    # success
    exit 0
  else
    echo "Server unreachable"
  fi
done < $servers

# fail
exit 1

Last edited by H_TeXMeX_H; 04-22-2009 at 09:16 AM.
 
Old 04-22-2009, 09:26 AM   #10
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
Quote:
servers="servers.txt"

while read line
do
echo "For Server: $line "
[[ `ping -qnc 1 $line 2>/dev/null` ]] && ( echo "Server Ready";update;exit 0 ) || ( echo "Server Unreachable")

done < $servers

exit 1
I would still prefer the one line solution . But you should use H_TeXMeX_H's way for it to be portable. My way would only work if you are using bash (or ksh ??).
 
Old 04-22-2009, 10:21 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
@Libu: You need to use "{" instead of "("
Code:
ping -qnc 1 $line >/dev/null 2>&1 && { echo "Server Ready";update;exit 0; } || { echo "Server Unreachable"; }
"(" will open a subshell so the exit will not end the top level script. And getting rid of the [[ makes it portable to most shells, I think.
 
Old 04-22-2009, 12:45 PM   #12
mravljica
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Got my script working.

Thank you all for the help.

Regards,
Sasa
 
Old 04-22-2009, 02:11 PM   #13
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
ntubski:
you are right about it spawning a new shell. thanks for pointing it out.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 05:41 PM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 02:10 PM
BASH Shell scripting help ewarmour Linux - General 8 05-25-2002 07:10 AM

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

All times are GMT -5. The time now is 04:21 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