LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-09-2012, 06:42 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
writing script in bash


Hi ,
I don't know much programming .
I want to make following program

for variable k in list 58 , 62 ,67 ,... (this is a peculiar list of 30 numbers)
do
{
l="$(ping -c 1 192.168.8.$k | grep -i ttl)"
if [ $? -eq 0 ]
then
echo "found\n"
else
echo "\nNot found 192.168.8.$k"
fi
}

I got some pieces from hither and thither .
My aim is to ping 192.168.8.something & report which where not found .
Also somebody told me dollar is better than back ticks.
 
Old 05-09-2012, 07:17 AM   #2
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
This should do it.

Code:
#!/bin/bash
for k in 58 62 67 87 123; do
   l = $(/bin/ping -c 1 192.168.8.${k} | /bin/grep ttl)
   if [ "X${l}" = "X" ]; then
      echo "Address 192.168.8.${k} does not answer."
   else
      echo "Address 192.168.8.${k} does answer."
   fi
done
 
Old 05-09-2012, 07:26 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by sumeet inani View Post
Also somebody told me dollar is better than back ticks.
Yes -- $( <command(s)> ) is preferred over ` <command(s)> ` for reasons explained here.
 
Old 05-09-2012, 12:00 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As long as your shell is bash or ksh, you really should be using [[..]] instead, or ((..)) for arithmetical comparisons. Then you don't need to use that old "[ "X${l}" = "X" ]" trick.


http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


However, I'm guessing we don't really want any of the output from grep, just whether it found anything. In which case you don't even need a test at all. Just silence its output and check its exit code directly.

Code:
#!/bin/bash

for k in 58 62 67 87 123 ; do

   if  ping -c1 "192.168.8.$k" | grep -q ttl ; then

	echo "Address 192.168.8.$k answered."

   else

	echo "Address 192.168.8.$k didn't answer."

   fi

done

exit

Last edited by David the H.; 05-09-2012 at 12:01 PM.
 
1 members found this post helpful.
Old 05-09-2012, 12:06 PM   #5
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You don't need the grep at all, since ping exits with exitcode 1 if it gets no answer to its echo requests.
 
Old 05-09-2012, 12:25 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
That's true, isn't it. It dawned on me after I posted.

But from what I can see, ping doesn't seem to have a truly silent mode (if it does, correct me please). You'd have to redirect the output away.

Code:
if  ping -c1 "192.168.8.$k" &>/dev/null ; then
 
Old 05-10-2012, 12:50 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
to me you dont even need the
Quote:
if ping
, I think what Tobi means is to if the exit status, so :
Quote:
ping -c1 "192.168.8.$k"
if [ $? = 0]
 
Old 05-10-2012, 01:43 AM   #8
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

Rep: Reputation: 49
Is it possible to write colorful text (only for some outputs) using echo command ?
In bash I used to set default prompt using variable PS1 .
 
Old 05-10-2012, 01:49 AM   #9
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

Rep: Reputation: 49
okay . i think . This will help --> http://hacktux.com/bash/colors

Also I will take some time to understand all useful suggestions .
 
Old 05-10-2012, 02:25 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197
@cbtshare - Running the command and then testing the return value would be two lines to do the same thing as let the "if" command test the return for you ... so no your second option would
not generally be preferred.
 
Old 05-10-2012, 05:14 AM   #11
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
David, thanks for the links. It's always great to learn new things. I've been using the bourne shell almost since it came out and am not totally familiar with all the new bash'isms.

sumeet inani, another thought that will probably make it easier to read the results: Define two variables that point to two files. Then just echo ${k} to the appropriate file. That way you will have a list of answered totally separate from the unanswered.
Code:
#!/bin/bash
ANS=/tmp/answered
NO=/tmp/noanswer
for k in 58 62 67 87 123; do
   if /bin/ping -c 1 192.168.8.${k} | /bin/grep ttl; then
      echo "${k}" >> ${ANS}
   else
      echo "${k}" >> ${NO}
   fi
done
 
Old 05-10-2012, 09:58 AM   #12
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
Quote:
Originally Posted by grail View Post
@cbtshare - Running the command and then testing the return value would be two lines to do the same thing as let the "if" command test the return for you ... so no your second option would
not generally be preferred.
Ah, ok ,well if its shorter code is the prefence, then the entire 'if' statement can be removed and subsituted with parameters.Your right efficiency is better,atleast for long code,but for stuff like this,I opt for the ABC's,nicer to read
 
Old 05-10-2012, 03:34 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197Reputation: 3197
Quote:
then the entire 'if' statement can be removed and subsituted with parameters.
Sorry, you lost me ... could you explain this method?

Also, my preference was not necessarily for shorter code, but at the end of the day we are doing the exact same process. "if" itself is a function and therefore tests the exit result
of other functions, like [ or [[, so the following are both the same:
Code:
if ping -c1 192.168.8.$k &>/dev/null; then

ping -c1 192.168.8.$k &>/dev/null
if [[ $? == 0 ]]; then # that is without going into semantics that we should use (()) here :)
 
Old 05-11-2012, 12:16 AM   #14
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
Yea I get what your saying as having the test for the exist code status makes it bulkier and some what redundant because you could have added into one line (the ping and the 'if')to achieve the same result.

I was referring to somtten like this, and no need for the if which makes it shorter
Quote:
for k in 8 8 67 87 123
do
ping=$(ping -c 1 192.168.1.$k | grep -E ttl=) >/dev/null

result=${ping:? didnt answer $(mail -s root "server .$k is down,please fix and run command again")}
echo ".$k answered fine"
done
exit 0

Last edited by cbtshare; 05-11-2012 at 12:18 AM.
 
  


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
Writing a bash script Leroy1990 Linux - Newbie 6 03-01-2012 03:40 PM
I need some help writing a bash script... trist007 Programming 8 04-15-2009 10:31 AM
Need help writing a bash script stympman Linux - Newbie 2 10-18-2008 07:55 PM
Help Me>> Need help in writing Bash script lamak_98 Programming 6 10-04-2007 10:44 AM
Bash (help writing script) lebabyg Linux - General 7 07-04-2006 05:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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