Results 1 to 3 of 3
I know these forums probably see a lot of this, but I'm trying to understand a little more about what's going on in this particular shell script.
It's mostly just ...
- 04-17-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 1
Need help understanding a script!
I know these forums probably see a lot of this, but I'm trying to understand a little more about what's going on in this particular shell script.
It's mostly just for the sake of my curiosity--I've never really touched anything in Linux before, but I stumbled across this script helping a friend find some resources to cite for a paper he's writing, and I was surprised at how much of it I understood, but my limited knowledge of shell scripting has left me scratching my head at some of this stuff. Anyway, if someone could help me break this down, I would appreciate that:
I pretty much understand that this is the host I want to ping. What are the parameters? Can the host include IP addresses? What all can I lump into the field?Code:# add ip / hostname separated by while space HOSTS="cyberciti.biz theos.in router"
This I don't really understand at all . . .Code:# no ping request COUNT=1
This much is pretty much obvious . . . obviously, it's the body of the script that I'm having trouble understanding the most.Code:# email report when SUBJECT="Ping failed" EMAILID="me@mydomain.com"
Is anyone willing to break this down for me line by line? I don't mean to be a pain here, but I really appreciate and respect the time you guys put into these forums.Code:for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 0 ]; then # 100% failed echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID fi done
- 04-17-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
All of these are just variables. In bash you set a variable this way:
They have no direct meaning, they could have any other different names like A, B, C or VAR. They only take true meaning when you use them on the script body. So, their contents must be modelled depending on what you are going to do with them. For example, the correct values for HOSTS depends on the use you are going to do of this variable.Code:VAR="foo bar etc whatever"
If you are going to use it in conjunction with the ping command (man ping for more info), this variable should contain only values that can be parsed and interpreted by ping (so, ip's would be possible as well).
This smells like someone's homework (and your nice story seems a bit odd to me), so I am not going to spoil the fun. I will just only tell you that you need to read the awk, ping and bash man pages.Is anyone willing to break this down for me line by line? I don't mean to be a pain here, but I really appreciate and respect the time you guys put into these forums.Code:for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 0 ]; then # 100% failed echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID fi done
- 04-18-2008 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458


Reply With Quote
