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. |
This should do it.
Code:
#!/bin/bash |
Quote:
|
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 |
You don't need the grep at all, since ping exits with exitcode 1 if it gets no answer to its echo requests.
|
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 |
to me you dont even need the
Quote:
Quote:
|
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 . |
okay . i think . This will help --> http://hacktux.com/bash/colors
Also I will take some time to understand all useful suggestions . |
@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. |
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 |
Quote:
|
Quote:
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 |
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:
|
All times are GMT -5. The time now is 11:11 PM. |