LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can someone explain how this script works? (https://www.linuxquestions.org/questions/programming-9/can-someone-explain-how-this-script-works-561613/)

Sanborn 06-13-2007 06:46 PM

Can someone explain how this script works?
 
I am by no means a programmer. Using SUSE I found a script that creates the equivalent to having a rc.local file, found here: http://linux.derkeiler.com/Mailing-L...5-07/1773.html. I was wondering if anyone could explain how this script works too me.

This is what the mail-list says to do:

Steps to simulate rc.local in Suse:

1) Create a "/etc/rc.d/rclocal" script, with this content:

----------------------------
#! /bin/sh

## This script simulates redhat's rc.local (Add commands at the end)

### BEGIN INIT INFO
# Provides: rclocal
# Required-Start: $local_fs $remote_fs $network
# X-UnitedLinux-Should-Start: $ALL
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Simulates rc.local
# Description: Simulates redhat's rc.local: contains
# commands to execute after system has booted (all services are already
# available)
### END INIT INFO

## Execute ony when service is started
case "$1" in
start)
## commands will be executed
;;
*)
exit 0
;;
esac

# vvvvv Add your commands bellow this line vvvvv
----------------------------

2) Add executable permision: chmod +x rclocal

3) Create symlink to make it easy to find: ln -s rclocal rc.local

4) Activate the service by using yast2:
yast2 > System > Runlevel editor > rclocal > Enable

You can add/remove commands to /etc/rc.d/rc.local anytime you wish.

So what I did and what my problem is:

I opened up Kate editor and followed step 1 exactly character for character. Where it says "add your commands bellow this line vvvv" I typed ' route add 192.168.0.1 gw 30.38.72.50 ' (without ''). This part confused me, because doesn't the command need to be where it says ## commands will be executed?

Step 2 and 3 worked no problem

on step 4, when I tried to activate the script it gave me the error message:

/etc/init.d/relocal start returned 7 (program is not running)

Did I implement that line of code incorrectly?

dxqcanada 06-13-2007 07:42 PM

case "$1" in

this means ... read the first parameter passed to this shell script and look for a match below.

start)

If $1 equals "start" then run all commands after this section up to the ";;" that terminates this section ... then goto esac.
In this script there are no commands to run.

*)

If $1 equals anything then run all commands after this section up to the ";;"
In this script it says to exit.

esac

ends the case

Commands after this point will only be started if the $1 parameter equals "start".

Sanborn 06-13-2007 07:52 PM

Thanks, this is going to sound stupid. But does that mean I should put the route command inside the first part and at the end ?

so like:

## Execute ony when service is started
case "$1" in
start)
route 192.168.0.1 gw 30.38.72.50
;;
*)
exit 0
;;
esac

route 192.168.0.1 gw 30.38.72.50

or just in one? Also, is that the right way to use route? If you do whereis route it says that route is /sbin/route. Does that mean I need to use it as /sbin/route add ..blah blah when inside the script ?

Thanks for the help, I'm a bit overwhelmed at even the more simple stuff.

dxqcanada 06-13-2007 07:57 PM

If the rclocal script was passed a parameter of "start" then it would pass through the case statement ... exit the case statement without doing anything ... and then attempt to execute your route command.

So, I think it should be OK in the "vvvvv" section.

Yes, I would specify the full path of the command.

Make sure the command runs properly on the command line ... cause I think you do not have the command correctly written.

Sanborn 06-13-2007 08:50 PM

Oh yes, I forgot to add the "add" portion right after route. I have it done correctly on the code.

So thats odd, I have the script typed exactly as described, any other reason why when I try to enable the script Yast gives me that error?

dxqcanada 06-13-2007 08:58 PM

First ... if you do not add the route command to the rclocal script ... does it generate an error ?

Sanborn 06-13-2007 10:11 PM

It's actually a work computer I've been doing some side stuff on. I'll check it tomorrow morning. If the script is supposed to load while the system is booting, where would I see if there was an error? Or do you mean actually running the script by itself after everything is loaded?

I believe that I ran it and nothing seemed to happen, but it was a quick try before I left for the day.

dxq, do you know how I can add some kind of echo command to write to a text file? Just to see if the script goes thru on boot?

acid_kewpie 06-14-2007 03:15 AM

nothing to do with networking. please choose your forums more carefully in future. moved to Programming.

Sanborn 06-14-2007 06:24 AM

Opps, I should sleep more.

Sanborn 06-14-2007 08:54 AM

If I enable the script in Yast it will run the route command as it should (becuase I placed the route command in 3 spots). In the yast-runlevel service I told it to start/stop in level 2/3/5/S (what is S?) and yet nothing happens on boot.

Any ideas?

This is the script exactly as I have it:

"#! /bin/sh

## This script simulates redhat's rc.local (Add commands at the end)

### BEGIN INIT INFO
# Provides: rclocal
# Required-Start: $local_fs $remote_fs $network
# X-UnitedLinux-Should-Start: $ALL
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Simulates rc.local
# Description: Simulates redhat's rc.local: contains
# commands to execute after system has booted (all services are already
# available)
### END INIT INFO

## Execute ony when service is started
/sbin/route add 192.168.0.1 gw 30.38.72.50

case "$1" in
start)
## commands will be executed
/sbin/route add 192.168.0.1 gw 30.38.72.50
;;
*)
exit 0
;;
esac

# vvvvv Add your commands bellow this line vvvvv ----------------------------
/sbin/route add 192.168.0.1 gw 30.38.72.50


-I added it to multiple lines to see if anything would happen. Again, if I enable it from yast it will run thru the script and route add will work correctly. On boot, nothing.

Sanborn 06-14-2007 09:08 AM

Also, can someone explain to me how anything after esac matters? Is that where the program goes to if $1 = Start and it hits ;; ?

dxqcanada 06-14-2007 01:04 PM

The script runs through the case routine ... then the sript runs through any other commands after esac (which ends the case routine).

The only time the script will exit is either there are no more commands to process or an exit command it encountered.

So in your case ... if the case routine matches "start" then it will process all commands up to the ";;" then the case routine jumps to esac ... then the script continues on to process any other commands that follow it.
If the case routine matches "*" (anything else) then it encounters the exit command that tells the script to end.

There should not be a " in the first line ... I am going to assume it is a typo.


You could add some debug type statements into the script to see what it is doing.
Example ... add
Code:

echo "script start" > /debug
in the begining of the script.

After boot see if the file /de bug was created.
If not then the script did not even run.

Sanborn 06-14-2007 01:45 PM

Which line do you think is a typo??

the "$1" portion? That code is exactly how I have it (unless I am not seeing something from what i typed previously)

edit: Yes the "#!sbin part is a typo.

I assume the "$1" is correct tho?

Another question I have is, with SUSE, where should this script be running?

Just to cover bases I told it to start/stop on 2,3,5,S (what does S even do?) and I edited the rc5.d and rcS.d folder so that it was S99 position.

I feel I am close...but just missing one little detail.

Sanborn 06-14-2007 02:07 PM

Alright little update:

I put an echo command to write to a text file and ran the script from command line. It worked as intended, so I know that the code is ok. Now the real problem: Why is it not being ran on boot up? It is enabled on multiple levels and set to S99. I must be making some sort of novice mistake.

Sanborn 06-14-2007 04:38 PM

Quite interesting, back in 05 someone was trying to do the exact same thing I am, so I must be close.

http://www.linuxforums.org/forum/sus...m-startup.html

RobeCote's last reply:

okay I got it to work using a work around with a rc.local file
i had to create a file that calls the scripts (rc.local) and i put it in /etc/init.d/
then you have to create a symlink in rcX.d that calls the rc.local script,
since my problem was it was executing before network drivers and such were ready, i had to name the link S99rclocal (S99 being the order of the scripts run on startup. i put this file in rc3.d so that its the last thing executed on a runlevel3 bootup.

Funny enough, I think the last thing I was about to try before I left was change the order to S99 in rc3. Maybe I already did it, I'll find out tomorrow!


All times are GMT -5. The time now is 08:41 PM.