LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 01-07-2011, 06:10 AM   #1
sixtensixone
LQ Newbie
 
Registered: Apr 2005
Location: Rio de Janeiro
Distribution: Fedora 3
Posts: 13

Rep: Reputation: 0
Question 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,

Last edited by sixtensixone; 01-07-2011 at 06:14 AM. Reason: an icon for the question.
 
Old 01-07-2011, 06:37 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 01-07-2011, 07:47 AM   #3
sixtensixone
LQ Newbie
 
Registered: Apr 2005
Location: Rio de Janeiro
Distribution: Fedora 3
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
Old 01-07-2011, 08:01 AM   #4
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 01-07-2011, 08:46 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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 View Post
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.
 
Old 01-07-2011, 10:16 AM   #6
sixtensixone
LQ Newbie
 
Registered: Apr 2005
Location: Rio de Janeiro
Distribution: Fedora 3
Posts: 13

Original Poster
Rep: Reputation: 0
Lightbulb

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
 
Old 01-07-2011, 11:22 AM   #7
sixtensixone
LQ Newbie
 
Registered: Apr 2005
Location: Rio de Janeiro
Distribution: Fedora 3
Posts: 13

Original Poster
Rep: Reputation: 0
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,
 
Old 01-07-2011, 11:47 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by sixtensixone View Post
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.
 
Old 01-07-2011, 11:50 AM   #9
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 01-07-2011, 12:21 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
A description of the init script mechanism: http://www.debian.org/doc/debian-pol...tml#s-sysvinit
 
1 members found this post helpful.
Old 01-07-2011, 04:38 PM   #11
sixtensixone
LQ Newbie
 
Registered: Apr 2005
Location: Rio de Janeiro
Distribution: Fedora 3
Posts: 13

Original Poster
Rep: Reputation: 0
Smile Solved

Hi Catkin, Thank you!

Quote:
Originally Posted by catkin View Post
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,
 
Old 01-08-2011, 01:49 AM   #12
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
1 members found this post helpful.
Old 01-08-2011, 01:36 PM   #13
sixtensixone
LQ Newbie
 
Registered: Apr 2005
Location: Rio de Janeiro
Distribution: Fedora 3
Posts: 13

Original Poster
Rep: Reputation: 0
Wink Solved but this worth.

Quote:
Originally Posted by EricTRA View Post
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,

Last edited by sixtensixone; 01-08-2011 at 01:39 PM. Reason: complete the information
 
Old 01-08-2011, 01:44 PM   #14
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Hi,

You're welcome! Have a nice weekend.

Kind regards,

Eric
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split function (bash script) NiM Programming 11 03-10-2012 06:53 AM
bash script with ftp in a function -- please help! kpj104 Linux - General 1 05-19-2008 10:24 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
search function (bash script) LYK Programming 2 05-27-2004 10:51 AM
C built-in function for a Bash script Linh Programming 3 04-23-2004 09:23 AM


All times are GMT -5. The time now is 06:02 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