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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-09-2012, 06:42 AM
|
#1
|
Member
Registered: Oct 2008
Posts: 908
Rep:
|
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.
|
|
|
05-09-2012, 07:17 AM
|
#2
|
Member
Registered: Mar 2009
Posts: 367
Rep:
|
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
|
|
|
05-09-2012, 07:26 AM
|
#3
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by sumeet inani
Also somebody told me dollar is better than back ticks.
|
Yes -- $( <command(s)> ) is preferred over ` <command(s)> ` for reasons explained here.
|
|
|
05-09-2012, 12:00 PM
|
#4
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
05-09-2012, 12:06 PM
|
#5
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
You don't need the grep at all, since ping exits with exitcode 1 if it gets no answer to its echo requests.
|
|
|
05-09-2012, 12:25 PM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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
|
|
|
05-10-2012, 12:50 AM
|
#7
|
Member
Registered: Jul 2009
Posts: 645
Rep:
|
to me you dont even need the , I think what Tobi means is to if the exit status, so :
Quote:
ping -c1 "192.168.8.$k"
if [ $? = 0]
|
|
|
|
05-10-2012, 01:43 AM
|
#8
|
Member
Registered: Oct 2008
Posts: 908
Original Poster
Rep:
|
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 .
|
|
|
05-10-2012, 01:49 AM
|
#9
|
Member
Registered: Oct 2008
Posts: 908
Original Poster
Rep:
|
okay . i think . This will help --> http://hacktux.com/bash/colors
Also I will take some time to understand all useful suggestions .
|
|
|
05-10-2012, 02:25 AM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
@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.
|
|
|
05-10-2012, 05:14 AM
|
#11
|
Member
Registered: Mar 2009
Posts: 367
Rep:
|
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
|
|
|
05-10-2012, 09:58 AM
|
#12
|
Member
Registered: Jul 2009
Posts: 645
Rep:
|
Quote:
Originally Posted by grail
@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
|
|
|
05-10-2012, 03:34 PM
|
#13
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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 :)
|
|
|
05-11-2012, 12:16 AM
|
#14
|
Member
Registered: Jul 2009
Posts: 645
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 01:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|