LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-29-2003, 08:45 AM   #1
Rex_chaos
Member
 
Registered: Aug 2001
Location: GD, GZ, China
Posts: 240

Rep: Reputation: 30
bash shell scirpt help


Hi all,
I have written so many shell scripts with bash. Most of them contain some identical functionality like reading password, check if input is numeric, etc. and some common constants (EXIT_CODE and ERROR_CODE). I wonder if it's possible to have a common script for organizing all this functions together. e.g.

#!/bin/bash

# Here is a common script for useful utilities and codes

EXIT_CODE=0
FILE_NOT_FOUND=-1
...

readpassword()
{
...
}

If it's ok, please tell me how to invoke the functions defined in the common script. Thanks in advance.
 
Old 07-29-2003, 08:53 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you look at a rc init script, e.g. /etc/init.d/cron you should see a line such as
Code:
. functions
which is effectively a #include in bash to the funcitons file in that directory, i.e. it will source that file, which (afaik) doesn't actually have to be an executable bash script, just a set of bash commands (i.e. no #!/bin/sh etc...)
 
Old 07-29-2003, 08:53 AM   #3
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Yes you can call the functions of another script with:

source script.sh
 
Old 07-29-2003, 09:52 AM   #4
Rex_chaos
Member
 
Registered: Aug 2001
Location: GD, GZ, China
Posts: 240

Original Poster
Rep: Reputation: 30
I don't understand. Suppose I define thousands of functions in a script(says script.sh). How can I just call one function in the script?
 
Old 07-29-2003, 10:03 AM   #5
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Lets consider that one of your functions inside script.sh it's called xfunc and you want to use in other script called script2.sh.

Edit script2.sh and add a line:
source script.sh

then in this script you will be able to call your function by its name:
xfunc foobar
 
Old 07-29-2003, 10:08 AM   #6
Rex_chaos
Member
 
Registered: Aug 2001
Location: GD, GZ, China
Posts: 240

Original Poster
Rep: Reputation: 30
great. thanks a lot
 
Old 07-29-2003, 11:17 AM   #7
Rex_chaos
Member
 
Registered: Aug 2001
Location: GD, GZ, China
Posts: 240

Original Poster
Rep: Reputation: 30
I wonder why the function can only return a numeric value. How can I return a string? Thanks
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is Bash shell? xailer Linux - Newbie 4 11-23-2003 09:29 AM
bash shell dawood shaheen Programming 9 08-01-2003 05:07 AM
Bash Shell triplem Linux - Software 4 04-17-2003 05:11 PM
Bash shell triplem Linux - Software 1 04-14-2003 08:01 AM
Help!!!!! Bash Shell???? DarkHorse Linux - Newbie 7 06-04-2001 12:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:52 PM.

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