LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2008, 04:59 AM   #1
soujiro777
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
Question subprograms for bash shell-script


I have a question about subprograms but I have very recently started Linux, so please bare with me.

hardware: Sharp Zaurus SL-C1000(sold in Japan)
software: bash shell-script

The role of subprograms is to divide the entire software into pieces, correct? So I am trying to write a program consisting of two files for start. Like...

------------------------------------------------
#!/bin/bash
function confirm
{
if [ $# != 2 ]
then
echo "OK"
else
echo "Do it again!"
fi
}
export confirm
subprogram1
#(<-I am not sure what to write here either)
-------------------------------------------------

then I am to write a subprogram that calls this function "confirm", but I am not sure how. Is it...?

-------------------------------------------------
#!/bin/bash
confirm "Bob" "123"

-------------------------------------------------


Also I am little confused about how to execute those files because there are two.

Is it

./subprogram1

?

please point out the parts that are not clear to you.
thanks!
 
Old 02-22-2008, 06:05 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Hm. I think what you want are functions which, I believe, are more portably written with the 'function() { list; }' syntax. And those would be in the same shell script. You invoke them simply by name and any arguments given to them are separate from the arguments passed into the script. So

function() {
blah blah $1
}

more cmds
function foo # invokes 'function' and passes 'foo' to it.

A thing to note is that variables coming out of functions, so to speak, are global unless specifically restricted with 'local'.

If you really want a library of functions (because they can be used in more than one script) you can keep them in another file, perhaps called 'lib.sh', and then you can source that file '. lib.sh' and then all functions in that file will be made available to your script.

Hope that helps.

-- Oh, and welcome to LQ!
 
Old 02-22-2008, 06:21 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
A script can call a function which is declared within the same file, or which is in a file of its own. In the latter case, the function being called could be something already in bash--or something that you wrote. If you want the function to be available to other scripts, then put it in a separate file.

To call a function (subprogram, subroutine, etc.) which is declared in the same file, you would simply call it by name. Otherwise the standard rules apply (the path to the function file has to be in $PATH, or you have to specify the path---eg "./filename" means "<current directory>/filename")
 
Old 02-22-2008, 07:41 PM   #4
soujiro777
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
subprograms

thank you for replying.

but sorry, didiot, could be more specific about

>declared within the same file, or which is in a file of its own

? I cannot distinguish between "within the same file" and "a file of its own".

You know, I am still a grew-up-with-MSWindows-person...
 
Old 02-22-2008, 08:53 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
That was actually pixellany - quoting another post is easy with the 'Quote' button in the bottom right corner of each post. But he was saying what I said about the "same shell script" vs. "in another file". Functions are just reusable bits of code. You can have a file called 'foo' with the functions it uses in the same file with the main body of code (that which calls the functions) or you can have a file 'bar' with no functions in it at all, but which 'source's a function (or library of several functions) called 'baz' by using the 'source' aka '.' (dot) builtin.

Code:
$ cat foo
greeting(){
  printf "hello "
}

greeting
echo world

$ cat bar
. baz

greeting
echo world

$ cat baz
greeting(){
  printf "hello "
}
I'd really recommend reading a tutorial on shell scripting and functions and/or the manual for more details.

Last edited by slakmagik; 02-22-2008 at 08:54 PM.
 
  


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
Bash Shell script bd1308 Linux - Hardware 9 08-22-2005 05:38 PM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
bash/shell script mikis Programming 2 10-14-2003 11:39 AM
BASH shell script help ewarmour Programming 8 05-24-2002 07:57 AM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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