LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 05-12-2009, 10:50 PM   #1
wvw
Member
 
Registered: Apr 2008
Posts: 30

Rep: Reputation: 15
functions in bash scripting


Hello

I would like to know if bash can only handle a function by itself or whether a fucntion can be used where it accepts a variable from the main program? From my reading on bash scripting sites it looks like the former is possible. below is an e.g. of what I mean:

#!/bin/bash
#
#Script to clarify my above question about functions used in bash
#test function
#
testintvalue(x)
{
if [$x == 5]; then
y=x+3 else
y=3
echo $y "is the total value"
fi
}
#
#Main program lines here
#
clear
echo "Welcome! Please type in a number between 1 and 9 inclusive"
read num
#Call the above function with the value of num passed to it
testintvalue(num)
#end of script

I know the above might have some syntax errors regarding the if statement etc, but what's important to me is if I can pass variables to the function like I did above.

Thanks
wvw
 
Old 05-13-2009, 12:10 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Fns certainly can take params: http://www.tldp.org/LDP/abs/html/complexfunct.html
 
Old 05-13-2009, 01:36 AM   #3
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
When I was reading abs I found it rather buggy and plenty of text was not related to bash directly. Maybe is it me only being overly captious.
 
Old 05-13-2009, 09:39 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,062

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
When writing a function in Bash you do NOT specify parameters in the function declaration. Inside the function you refer to parameters positionally, $1, $2 etc. These parameters are local to a function, hence not the command line parameters.

You pass parameters to a function by appending the call with parameters:
Code:
myfunc parm1 parm2
So if you have on the command line one parameter, and you want to pass that to you function:
Code:
myfunc $1
Run you program with sh -x youprogram while developing, it helps understand where the values go.

jlinkels
 
Old 05-14-2009, 05:22 PM   #5
wvw
Member
 
Registered: Apr 2008
Posts: 30

Original Poster
Rep: Reputation: 15
Hi Guys

Thanks for all the help That was exactly what I was looking for.

Cheers
wvw
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Scripting Functions ishcoleobo Programming 8 03-26-2008 08:51 PM
Functions in bash script som_kurian Programming 3 01-01-2008 09:12 AM
Order of functions in a bash script essdeeay Programming 1 05-02-2006 10:52 PM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 01:48 AM


All times are GMT -5. The time now is 01:10 PM.

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