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 |
Fns certainly can take params: http://www.tldp.org/LDP/abs/html/complexfunct.html
|
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.
|
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 Code:
myfunc $1 jlinkels |
Hi Guys
Thanks for all the help :) That was exactly what I was looking for. Cheers wvw |
All times are GMT -5. The time now is 12:56 PM. |