Page 1 of 1

Bash Scripting - Functions how to write and use functions in bash scripts Rate Topic: -----

#1 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 311
  • View blog
  • Posts: 1,515
  • Joined: 16-May 09

Posted 28 November 2009 - 02:24 AM

In this tutorial we are going to learn how to write functions in our scripts to do our desired tasks. Writing functions may not be useful for you at first, but as you write more advanced scripts and they get longer, you need a way to maintain them easily and fast... writing functions give you this ease, you have to break the script into different functions (where needed and possible), and then recall those when you want to use them.

OK, now to the tutorial itself, the syntax for defining a function in Bash Scripts is quite easy:
...
function simple_function {
	#some stuff here to do when calling simple_function later
}
#or this for us C/C++ addicts!!!
another_function () {
	#some stuff here
}
...
Wasn't it easy ? :D

Here's a very simple script to show a function in use:
#!/bin/bash
function test_func {
		echo "Inside test_func function"
		echo "User id: $EUID"
}
#in the following line we call the test_func function
test_func
For me, the output is:
Inside test_func function
User id: 1000

however, your user id may be different.

Now, what if we want to pass arguments to the functions we created before? It's really easy, just look at this example script:
#!/bin/bash
#set default values -> nothing!
option1=
option2=
#function responsible for checking if number of parameters are valid
#they must not be higher than 2
check_params () {
	if [ "$1" -gt "2" ]; then
		echo "This script only uses 2 parameters(max), but you provided $1 parameters..."
		exit 1
	fi
}
#this function loops through the passed parameters while they are not empty
#the trick is the shift, it shifts the parameters 1 down, so $1 = $2 after shifting
parse () {
	while [ "$1" != "" ]; do
		if [ "$1" = "-a" ]; then
			option1="on"
			shift
		fi
		if [ "$1" = "-b" ]; then
			option2="on"
			shift
		else
			shift
		fi
	done
}
#now, we have the option1 and option2 set to on if the parameters were provided
task () {
		#print the user id, just for fun !
	echo "User id: $EUID"
		#just writing some text to be cool and show if the options are set to on
	if [ "$option1" = "on" ]; then
		echo "Option1 is set... Congratulations YO!!!"
	fi

	if [ "$option2" = "on" ]; then
		echo "Option2 is set... Congratulations YO!!!"
	fi
}
#we pass the parameters to functions like this
#note that $# is the number of parameters passed to the script, or functions
#here it is the number of parameters passed to script
check_params $#
#pass the first and second parameters passed to script to the function
#it doesn't matter if they are not set, in this case they wil be empty
parse $1 $2
#this function takes no parameters
task
#exit with success
exit 0

In the above example, you can see the way we can pass parameters to both scripts and functions, just use "$1", "$2", etc for them. And not that, the parameters we send to the script cannot be used in any functions with a "$1" or "$2", because in this case, the script interpreter thinks we are referring to the parameters passed to the function itself, while we didn't pass anything to the function.

When you want to run the above script, give it the parameter "-a" and it should output:
User id: 1000
Option1 is set... Congratulations YO!!!
However, your user id may be different.

Hope this tutorial was useful for you to understand the use of functions in Bash Scripts :D Have a nice day Yo!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1