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/)

gnashley 06-15-2007 03:28 AM

Having the command a second time means this is getting run twice.
If you just run rc.local without any argument the script exits without doing anything.
If you run the 'rc.local start' the command will be run correctly with the first line. But the script doesn't exit, it goes to the end of the case statement (esac) and continues. If there is a second line below the command will be run again.
You can debug this by inserting a line which echos to /var/log/messages just before each command.

The script you downloaded is a template for creating new rc script for any kind of 'service' Generally the scripts are called with an argument like 'start', 'stop' or 'restart' and running the script without any argument does nothing.
If you are simply starting a process that you don't want to manage as a service, or just a simple one-time command(your case) you don't need anything but this:
#!/bin/bash
/sbin/route add 192.168.0.1 gw 30.38.72.50

The way you last wrote the script(with 3 entries for the command) does the following:
If called without arguments the first command gets run, then the case statement is handled and the script exits because there are no arguments.
If called with the 'start' argument the command will be run all 3 times.
All you really need is the above two-liner to run this command. You probably don't really need to edit any other scripts to call, run or source this file(rc.local) as it will be automatically run, if present and executable, after init finishes running all other init scripts- rc.local is the last thing to run before login.

Sanborn 06-15-2007 06:20 AM

Aye, that was my next logical step. Thanks Gnashley.

I wanted to set it up so in the future anyone could come in and add their own command/scripts but maybe its not worth the hassle to get this single command running.

I also have a question, and this could have been part of the problem. What is the difference between using #!/bin/bash and #!/bin/sh

Sanborn 06-15-2007 07:31 AM

Still did not work after above changes made. It's just not being called, and I have no idea why.

Is runlevel 3 and 5 the correct placement? What about the order numbering? I set S and K both to 99. I have an echo command that writes hello to diditwork.txt and the file is not being written to.

So strange

gnashley 06-15-2007 08:53 AM

/bin/sh is the standard shell which the kernel looks for during bootup. In practice it is usually a link to /bin/bash. But scripts which have the #!/bin/sh shebang will be run using the syntax rules for sh and not bash. Most syntax is unaffected and you probably won't need to worry about it unles your scripts get longer and more complex.

Details about getting this thing to run during bootup should be addressed to a forum which is specific to your distro.


All times are GMT -5. The time now is 12:34 PM.