[SOLVED] Setting function default in a bash script - Bash
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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,
Last edited by sixtensixone; 01-07-2011 at 06:14 AM.
Reason: an icon for the question.
- 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?
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!
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.
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
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
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?
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.
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
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.
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.
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,
Last edited by sixtensixone; 01-08-2011 at 01:39 PM.
Reason: complete the information
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.