LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2003, 12:59 PM   #1
munkeh
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Rep: Reputation: 0
bash script for network restart


I am only 3 days into learning bash scripts, so bear
with me. I have encountered a problem I cannot solve
by reading tutorials.

This script produces a syntax error in the case statement
inside the function named "direction"

Code:
./nrs: line 37: syntax error near unexpected token `;;'
./nrs: line 37: `;;'
nrs is the filename of the script.

Well, here it is. Very q&d

Code:
#!/bin/bash
#
# v1.0
# This script is designed as a temporary fix for
# the internet connection dropout problem.
#
# Unitil such time as the fault can be properly
# diagnosed, this will be the fix.
#
# The network and iptables firewall should be
# reset when the connection loss is detected by
# the ping_eval() function in the script.
#
# The final script will be executed by the
# system (as a service?)as it will reside in
# /etc/rc.d/init.d/
#


				## Functions ##
function ping_eval()
{
	until [ $? != "0" ] # till ping returns a fail value for its exit status
		do
		sleep 30
		ping -c 1 194.168.8.100 -w 3 # ping again
		done
}



function direction()
{
case "$?" in
"0")
		ping_eval()
;;

"1")
	/etc/rc.d/init.d/network restart 	#restarts the network
	/etc/rc.d/stronger_firewall-2.4 restart #restarts the firewall
;;

"2")
	## 	/home/ian/logs/nrs/fail < cat <<- _EOF_		# will eventually print
	##	echo "ping has generated an error at"  		# the time of the exit status 2
	##	date						# from ping when I figure out how.
	##	echo "the nrs has exited due to this error"
	##	_EOF_
	exit 1 #if the ping returns an unknown error
;;

esac
}



				## Commands ##

case "$1" in
start)
	ping -c 1 194.168.8.100 -w 3 # ping again


	until [ $? = "3" ]
		do
	                direction()
	        done

	exit 0
;;

*)
        echo "Usage: nrs {start}"
	exit 1
;;
esac
# end of script
I'm sure its something glaringly obvious but I've
gone over case statement syntax again and again,
and I just can't fathom it.

Any help would be greatly appreciated
 
Old 11-09-2003, 05:47 PM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
When you are calling the function remove the parenthesis, e.g.

ping_eval() change to ping_eval @ line 37
direction() change to direction @ line 68
 
Old 11-10-2003, 09:51 AM   #3
munkeh
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks indeed

Thank you so much. I thought it would be something o so simple.
I do like forums such as these.
 
Old 11-10-2003, 12:24 PM   #4
munkeh
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Code:
ping -c 1 194.168.8.100 -w 3 # ping again
	echo "\"?\" is $?"

	until [ $? = "127" ]
	        do
	                echo "\"?\" is $?"
		sleep 5
		direction
	        done
now it changes the ? variable to 1 between
Code:
until
and
Code:
echo "\"?\" is $?"
I don't see how
Argh, coding scripts is harder than anticipated

Last edited by munkeh; 11-10-2003 at 12:55 PM.
 
Old 11-10-2003, 01:29 PM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
I'm not sure what you're asking, so I'm guessing - and
changing your code which may not be the best, but it's how you'll learn.

Code:
#! /usr/bin/sh
        ping -c 1 194.168.8.100 -w 3 # ping again
        retval=$?
        echo " retval=$retval"

        while [ true ]   # change this to test retval instead, if needed
	        do
	           echo "retval =$retval"
	           if [ $retval -eq 127 ]; then 
	                break;
	           fi
	           sleep 5
	           direction
	           retval=$?
	        done
 
Old 11-10-2003, 03:47 PM   #6
munkeh
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Original Poster
Rep: Reputation: 0
i'll try it but...

Thank you.

I still don't understand what command returns an exit status of 1
whithin that small part of code.

I'll try the code you suggest.

Thanks again

munkeh
 
Old 11-10-2003, 04:36 PM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
You need to look at the flow of your original code. I thought it had problems but I only answered the original question and I left the debugging to you.

Your program starts here:
case "$1" in
start)
ping -c 1 194.168.8.100 -w 3 # ping again
until [ $? = "3" ]
do
direction
done
exit 0

That first ping does nothing. It returns a $? but that is never checked before you call direction(). So the until loop is only checking the return value of direction(). However; you do not have a return from direction() only an exit 1, so direction() will never return to the original until loop (an exit statement terminates the script even if it is within a function.)

You don't have proper flow. You need to write down on paper what you want to do and try to make the code flow a little better.

Here is a rough draft:

Check input to see if how program was called.
You could also put an option to stop the script since it needs to be put in an endless loop.

Start -> MainLoop
Stop -> killall -9 $0
Unknown -> Exit with message

MainLoop() {
Start a while or untill loop
do
ping router
ping OK do nothing
ping Not OK do something
ping Unknown exit with error
done
}
 
Old 11-10-2003, 04:43 PM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
Here's an example.

Code:
#!/bin/bash
#
# v1.0
# This script is designed as a temporary fix for
# the internet connection dropout problem.
#
# Unitil such time as the fault can be properly
# diagnosed, this will be the fix.
#
# The network and iptables firewall should be
# reset when the connection loss is detected by
# the ping_eval() function in the script.
#
# The final script will be executed by the
# system (as a service?)as it will reside in
# /etc/rc.d/init.d/
#

main_loop() { 
while true
do
sleep 30
ping -c 1 194.168.8.100 -w 3 # ping again
case "$?" in
	0)
	echo "Ping OK, do nothing..."
	;;

	1)
	echo "Error 1"
	echo "Ping Not OK..." 	#restarts the network
	;;

	2)
	echo "Error 2"
	echo "the nrs has exited due to error #2"
	##	_EOF_
	return 2 #if the ping returns an error2
	;;

	*)
	echo "Error $?"
	echo "Unknown error..."
	return "$?"
	;;
esac
done
}

				## Commands ##

case "$1" in
    start)
	main_loop
        exit "$?"
	;;
    stop)
	killall -9 $0
	;;
        *)
        echo "Usage: nrs {start || stop}"
	exit 1
	;;
esac
# end of script

Last edited by /bin/bash; 11-10-2003 at 04:57 PM.
 
Old 11-10-2003, 05:00 PM   #9
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
By default or design -

when a unix command fails it returns non-zero, almost always one ( 1 ).
when a unix command succeeds it returns a zero.

For every command the shell processes, it sets the internal variable $? to a 1 or a zero. The only things that return errors other than one are usually specialty code, not the standard GNU stuff.

Since commands that fail may still produce some output, or do some parts of what they are supposed to do - eg., tset does this, but if and when it fails it bombs with a one.

So, the only way to be positive that your command did not bomb is to test the return value.

If you are having trouble determining where in the script the error occurs -
make your first line
set -x

Be sure to turn this off as it produces way too much clutter to be run in production.
 
  


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
Script to check connection (and restart network if down) mac_phil Linux - Networking 10 01-06-2010 05:19 AM
MySQL restart script? Fuwex Debian 2 08-17-2005 02:23 PM
run script when restart ust Linux - Software 4 08-25-2004 03:13 PM
Restart script blablaman Linux - Newbie 1 08-20-2004 06:09 AM
Bash?? System Restart (shutdown -r now) woranl Linux - Newbie 4 04-13-2004 09:33 PM


All times are GMT -5. The time now is 09:46 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration