LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2007, 01:37 PM   #1
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Rep: Reputation: 15
Bash code design


Hi!

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.

Thanks!
 
Old 08-14-2007, 01:38 PM   #2
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
Put each function in a file of it's own...
 
Old 08-14-2007, 01:43 PM   #3
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by rubadub View Post
Put each function in a file of it's own...
Hmm... That seems a little to much?

Can you say why I would do it like this?

And how would I make the functions visible for the main script file?
 
Old 08-14-2007, 01:52 PM   #4
thebouv
Member
 
Registered: Aug 2007
Distribution: RHEL, Fedora, Ubuntu
Posts: 64

Rep: Reputation: 16
Quote:
Originally Posted by rejeep View Post
Hmm... That seems a little to much?

Can you say why I would do it like this?

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.
 
Old 08-14-2007, 02:02 PM   #5
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by thebouv View Post
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 View Post
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.

How would I solve this best?
 
Old 08-14-2007, 02:25 PM   #6
thebouv
Member
 
Registered: Aug 2007
Distribution: RHEL, Fedora, Ubuntu
Posts: 64

Rep: Reputation: 16
Quote:
Originally Posted by rejeep View Post
"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.

Sound good?
 
Old 08-15-2007, 04:13 AM   #7
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by thebouv View Post
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 View Post
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! =)

Thanks a lot!
 
Old 08-15-2007, 05:31 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you can put all the functions in a separate file, say funcs.sh
then in the script:
Code:
#!/bin/bash -u
. funcs.sh

# they are now defined
edit: oops sorry didn't read all posts properly
 
Old 08-15-2007, 09:36 AM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

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
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux for Graphic Design, web design, and publishing maelstrom209 Linux - Software 8 07-17-2011 11:35 AM
Doubt with a bash code linuxlover1234 Programming 10 03-30-2004 12:48 PM
where is the bash source code?... gearoid Programming 5 11-25-2003 04:24 AM
BASH Scripting Design Problem... amaze Linux - General 1 08-19-2003 08:30 AM
Bug in c code calling bash code Linh Programming 11 08-12-2003 04:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration