LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Setting function default in a bash script - Bash (http://www.linuxquestions.org/questions/linux-general-1/setting-function-default-in-a-bash-script-bash-854813/)

sixtensixone 01-07-2011 06:10 AM

Setting function default in a bash script - Bash
 
Hi,

Let's suppose I'd like to initiate the Linux in runlevel 2 using a script file. So, then I put this file in the /etc/rc2.d directory. However, let's say I have four functions into this file: funcA, funcB, funcC and fubcD. And I'd like to start in runlevel 2 using the funcB().

What do I need to do to set this: Start a script file in a specific function?

Or the OS looking for the function's name "start()" all the times?

Regards,

EricTRA 01-07-2011 06:37 AM

Hello,

I don't get completely what you want.

- Do you want to start your Linux in runlevel 2 by default?
- Or do you want to execute your scripts only when started in runlevel 2?
- Or do you want to write a script to change runlevel when started and execute your commands/functions?

Kind regards,

Eric

sixtensixone 01-07-2011 07:47 AM

Hi Eric!

You don't need to worry about what I want to do! If you know the answer to my question just answer it! I thought I was clear but, let's try againg:

Question:

If I have a script file in (any) initialization level and this file have two (just for instance) functions (or more) inside it, which one is the function which will run first when the file start running?

Letīs try put this question in other words:

How do I tell to the Operating System (Linux in this case ), that I want to start a script file running the function xpto ? ( - xpto is just an example - ).

The question is not about runlevels. The question is not about if I want to do this or that, absolutely! The question is about how the Operating System works on this situation!

If it is not clear enought, please let me know.

EricTRA 01-07-2011 08:01 AM

Hello,

I'm not at all worried about what you want to do, that's not my place nor my system :)

I was / still am confused about what you mean with your question. If it's not about runlevels then I'm assuming it's about the startup order of your scripts. You say you have 'a' script with let's say 4 functions/commands in it in all your rc?.d directories? That for as far as I know, taking into account using SysV startup scripts, your script should be in /etc/init.d with links in the rc?.d directories representative for the runlevel you want to start it in. Depending on what system you use (chkconfig, update-rc.d, ...) you can choose the order in which your scripts will get started. Within your script however you'll have something like this:
Code:

start)
        command1
        command2
        etc
        ;;

Those commands will get executed in that order as you put them. If you want to execute only one command, depending on the runlevel you're in, you'll have to include that check within the script with a conditional check and execute the commands accordingly.

Is that what you're looking for?

Kind regards,

Eric

crts 01-07-2011 08:46 AM

Hi,

I am also a bit confused about the question and am not 100% sure if I understand what your requirement is.
Quote:

Originally Posted by sixtensixone (Post 4216439)
If I have a script file in (any) initialization level and this file have two (just for instance) functions (or more) inside it, which one is the function which will run first when the file start running?

Well, the answer to this question would be neither one. Or whichever one is called first. Functions do not run by themselves. They need to be explicitly called.
Quote:

How do I tell to the Operating System (Linux in this case ), that I want to start a script file running the function xpto ? ( - xpto is just an example - ).
Well, assuming you have a collection of functions that you want to call from several point from inside another script, you would first 'source' the script in one of the other init-scripts - as Eric pointed out - and the you can call the functions from inside the init-script.
Code:

source /path/to/xpto
xpto_function_1
# ... some other commands
xpto_function_2

Hope this helps.

sixtensixone 01-07-2011 10:16 AM

Hi again Eric!

So, always the operating system will look for the string "start" or "stop" and will do it acording the runlevel in which it is working. Is that right Eric? Is that what the SO does when it works with a script file? Please, let me know if I am in the right path. If yes, how many strings like that is there for it and which are those? For instance: The "restart" is something that you could call by yorself, isn't?

case "$1" in
start )
funcA
;;
stop )
funcB
;;

restart )
funcC
;;


* )
echo "Invalid option"
;;
esac

sixtensixone 01-07-2011 11:22 AM

Hi crts! Thank you for your replay!

Letīs see:

Quote:

Well, the answer to this question would be neither one. Or whichever one is called first. Functions do not run by themselves. They need to be explicitly called.
Ok, this is an answer! Thank you! So, I cannot use functions in a script file which will be used in a initialization process. That's it. Are you sure about it? Because, I thought I could use functions in a script like that.

Thank you again!
Regards,

crts 01-07-2011 11:47 AM

Quote:

Originally Posted by sixtensixone (Post 4216576)
So, I cannot use functions in a script file which will be used in a initialization process.

No, you misunderstood. You can call them. That is what I was trying to point with my example. You will have to manually modify the init-script in which you want the functions from your script to be accessible. You add the following line at the beginning of the init-script
Code:

source /path/to/your/script
Then you can call functions from your script.

EricTRA 01-07-2011 11:50 AM

Hi,

Start, Stop, Restart, and others are passed to script when executing at boot time. For example if you have your script started in runlevel 2, you'll have a link in /etc/rc2.d/ that starts with SXX where XX is a number. This number indicates when the script is started (before others that have a higher number, after those with a lower number). The options that are available depend on what you have in the script, but the main ones, used by the system, if I'm not mistaking, are start and stop. The other ones are for manual restarting, reloading, and other stuff. On various distros you have a file /etc/init.d/skeleton which is an empty set you can use to put in your commands to execute depending on what option passed as argument.

Kind regards,

Eric

catkin 01-07-2011 12:21 PM

A description of the init script mechanism: http://www.debian.org/doc/debian-pol...tml#s-sysvinit

sixtensixone 01-07-2011 04:38 PM

Solved
 
Hi Catkin, Thank you!

Quote:

Originally Posted by catkin (Post 4216627)
A description of the init script mechanism: http://www.debian.org/doc/debian-pol...tml#s-sysvinit

This is it! This is exactly what I was talking about! Thank you very much!

Best Regards,

EricTRA 01-08-2011 01:49 AM

Hello,

Keep in mind that Debian as from version 6 (Squeeze) is moving towards insserv (http://wiki.debian.org/LSBInitScript...dencyBasedBoot) for dependency based boot. Other distros are migrating to upstart (Ubuntu, RHEL6). Just to make the picture a bit more complete.

Kind regards,

Eric

sixtensixone 01-08-2011 01:36 PM

Solved but this worth.
 
Quote:

Originally Posted by EricTRA (Post 4217116)
Hello,

Keep in mind that Debian as from version 6 (Squeeze) is moving towards insserv (http://wiki.debian.org/LSBInitScript...dencyBasedBoot) for dependency based boot. Other distros are migrating to upstart (Ubuntu, RHEL6). Just to make the picture a bit more complete.

Kind regards,

Eric

Hi Eric,
I really appreciated your comments. Thank you, and yes we have the whole picture now.
Thank you again,

EricTRA 01-08-2011 01:44 PM

Hi,

You're welcome! Have a nice weekend.

Kind regards,

Eric


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