ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I am creating a Bash script. In the script I have about 7-10 functions.
How would you make the design? I don't like that the functions have to be defined before you call it. So I want to avoid having all functions in the beginning of the code.
Is there any way you can declare them like in C and maybe even put the functions in a different file?
How would you do it? The total amount of rows will be when then script is done about 200-300.
And how would I make the functions visible for the main script file?
Putting each function in its own file is bad advice in my opinion since you're trying to design a full bash app with X functionality.
Create a source file, say called myfunctions.sh, and in your main program you mention above, simple do this first:
source myfunctions.sh
Basically that 'source' command embeds the code in myfunctions.sh into your main script first, as if you had typed them there. Therefore, your functions are defined before they are called, just as you requested.
Putting each function in its own file is bad advice in my opinion since you're trying to design a full bash app with X functionality.
"with X functionality". From where did you get that? =)
Quote:
Originally Posted by thebouv
Create a source file, say called myfunctions.sh, and in your main program you mention above, simple do this first:
source myfunctions.sh
Basically that 'source' command embeds the code in myfunctions.sh into your main script first, as if you had typed them there. Therefore, your functions are defined before they are called, just as you requested.
Having all functions in one file makes more sense to me.
How then would you recommend me organize my files?
I must put the main file in a folder defined in $PATH. But I guess I don't want to make the "myfunctions.sh" runnable as it gets.
"with X functionality". From where did you get that? =)
My bad, I didn't really mean X functionality, as in X windows system. I mean X as in .. a variable .. as in I should have said that different. Ignore that part.
I could have just said "with blahblahblah functionality". Nevermind.
Quote:
Having all functions in one file makes more sense to me.
How then would you recommend me organize my files?
I must put the main file in a folder defined in $PATH. But I guess I don't want to make the "myfunctions.sh" runnable as it gets.
How would I solve this best?
Well your source command can be an absolute path such as:
source /path/to/myfunctions.sh
Or perhaps you want to make a folder such as /home/yourname/library and put your file in there, and whenever you want to use those functions, you can use something like:
source $HOME/library/myfunctions.sh
This way you build yourself a little library of bash source code for reuse.
My bad, I didn't really mean X functionality, as in X windows system. I mean X as in .. a variable .. as in I should have said that different. Ignore that part.
I could have just said "with blahblahblah functionality". Nevermind.
Haha
Quote:
Originally Posted by thebouv
Well your source command can be an absolute path such as:
source /path/to/myfunctions.sh
Or perhaps you want to make a folder such as /home/yourname/library and put your file in there, and whenever you want to use those functions, you can use something like:
source $HOME/library/myfunctions.sh
This way you build yourself a little library of bash source code for reuse.
Sound good?
Sounds like of course! Why didn't I think of that! =)
There is an argument for placing the functions in separate files. The Korn shell has a feature, autoload, which allows functions to be loaded from files when they are invoked (provided they can be found in FPATH). This is also available in pdksh.
There are also demonstration functions of an autoload feature in the bash documentation files (not the man page).
I have not used either extensively, but it might be useful for situations where portability is not an important consideration ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.