Have you ever written a huge Unix script and thought you would have divided this script into pieces of reusable code? Well don’t worry. You can create functions in unix shell script and make reusable code and call the function from your unix code.
Hello World Function in Unix shell script
First let us see a simple code snippet for creating hello world function in Unix. Create a shell script file helloworld.sh and paste following code in it and execute it.#function hello world defined
fnHelloWorld()
{
#print hello world
echo "Hello World";
}
#calling hello world function
fnHelloWorld
Code language: Bash (bash)
OutputThe above shell script will print Hello World on command prompt. All we have done is we have defined a function in shell script byCode language: Bash (bash)$./helloworld.sh Hello World
fnHelloWorld() {}
and called this by just fnHelloWorld.Passing argument to function in shell scripts
Passing argument to the functions in shell script is very easy. Just use $1, $2, .. $n variables that represent arguments in the function. Following is the example of function that takes two argument and prints them. Note that we have called the function fnArgumentFunction and passed two arguments “Hello” and “World” by fnArgumentFunction “Hello” “World”.fnArgumentFunction()
{
echo "Argument 1:" . $1
echo "Argument 2:" . $2
}
fnArgumentFunction "Hello" "World"
Code language: Bash (bash)
Thus if your function will use three arguments you can just use $1, $2 and $3 in your code. You may want to use $# which gives you number of arguments passed to the function if you want to implement a function with variable arguments.Returning values from Function in Shell Script
Like passing arguments in function in shell script, returning values are also easy. Following is the example of a function that take two input string and returns the concatenated values. We store the return parameter in a variable and print the variable on screen.fnReturnFunction()
{
echo $1$2
}
value=$(fnReturnFunction "holly" "wood")
Code language: Bash (bash)
Note that in above example we have called the function by $(fnReturnFunction “holly” “wood”).
Capturing echoed output is fairly useful, but then obtaining the success or failure of the function can get tricky. I might write it like this:
myfunc() {
local var=$1
eval $var="$2$3"
}
This makes it handy to test the return value of a function that might fail:
if myfunc value "holly" "wood"; then
echo "$value beckons!"
else
echo woe is me
fi
Damn, it backslashed all the quotes and backslashes :\
Hi spacebat.. I resolved the backslash issue. Thanks for the code.
My method suffers from increased need for quoting (say the parameters have spaces, you might need to quote them and escape the quotes before passing to eval), also it won’t work if the variable name you pass in is the same name as the local variable used to store the name, ie this is broken:
myfunc var “holly” “wood”
I’ve never run into that problem in real life, but I’ve been reading about it in the context of lisp macros where its known as variable capture. A workaround would be to prefix local vars with underscores.
Anyway at this point I tend to think heck this needs porting to perl or python :)
Neat!
could you please cover some basic like this on awk and sed. wil so thankful
nice 1……