LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2008, 03:43 PM   #1
grande25
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Rep: Reputation: 15
Any help with this linux script?


Hi! I'm running this script and it seems to "almost" work. It pings the IP address but when it gets a "1" condition it won't reboot like I want it to. Script:

ping -c 5 123.45.678.910
echo $?
if [ $? -ne 0 ] ; then shutdown -r -t 10 "now"
fi


That's it... I can run "shutdown -r -t 10 "now"" from the root and it works just fine.

Thanks!
 
Old 10-20-2008, 03:52 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
The problem is that "echo" is treated as a command. It will print the exit code of the ping command, and then the exit code of the echo command is zero (0).

Something like this might work:
Code:
ping -c5 123.45.678.910

PING_EXIT_CODE=$?

if [ $PING_EXIT_CODE -ne 0 ]
then
  echo $PING_EXIT_CODE
  shutdown -r -t 10 now
fi

------ OR ------

ping -c4 123.45.678.910
if [ $? -ne 0 ]
then 
  echo $?
  shutdown -r -t 10 now
fi
I do not know, for sure if the second example will give you the desired results, but the first one will, for sure.

PS. Do not post your question in more than one forum - the most appropriate place for this thread would be in the Programming forum.
 
Old 10-20-2008, 04:22 PM   #3
grande25
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks!

That worked great, indienick! Thanks a bunch!
 
Old 10-20-2008, 08:38 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
No problem. Did the second example work as expected?
 
Old 10-21-2008, 12:55 PM   #5
grande25
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Yes, the second part worked too

Hi indienick,

Yes, the second example worked. I just tested it and will probably leave it like that.

Can you tell me what "echo $?" does in the 4th line?

Also, in the first example, is PING_EXIT_CODE=$? a standard command that's listed in some compendium of Linux commnands?

I've been going nuts trying to find insight on how all this comes together in some simple way but it's been really slow going.

This isn't a regular task for me at work but it would be nice to be able to get something together if I need to.

Thanks again for basically writing the code for me. When I started looking into this, I thought it would be a pretty easy task but it was a serious PITA until you saved the day.

Iz
 
Old 10-21-2008, 02:23 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
hah! *blushes* Well, thank you - all in a day's work.

The "echo $?" in the fourth line means to print the value of the special variable "?", which is the exit code of the last command, to screen. Keep in mind, all references to variables in Bash are prefixed with a dollar-sign "$". It works in my example, because there is no command issued between the "ping" command and the "echo $?".

In the first example, "PING_EXIT_CODE=$?" is just a variable assignment - assign the exit code of the previous command (ping) to PING_EXIT_CODE. That way, if you wanted to issue a few more commands before testing the exit code of the ping command, you do not need to worry about the exit code of the ping command being "tainted".
 
Old 10-21-2008, 02:56 PM   #7
grande25
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Cool

Well now when I look at this stuff I see $? as the bash variable syntax. I had assumed that since a "1" was displayed from the root when pings failed that an echo had taken place and that the logical thing to happen would be for the script to see it and execute a shutdown. But really, I need to tell it to "echo" the $? (1 if ping fails) when -ne 0 and now that it sees a -ne 0 condition it will go ahead and do the shutdown according to the if/then lines.

it still makes my brain hurt. thanks again for the insight.
 
Old 10-21-2008, 03:08 PM   #8
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
No problem - just note that the "echo $?" in the second example is completely extraneous! It's meant for "debugging" purposes.

If you wanted to log it, you could substitute "echo $?" for "echo $? >> /path/to/file.log".
 
  


Reply



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
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
where to get linux administrator script, ex: script to let's user change user info. hocheetiong Linux - Server 1 03-17-2008 12:06 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:14 AM.

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