Linux Bash Scripting Tutorial
Functions
----------------
A function is a subroutine, code block that implements a set of operations, a "black box" that performs a specified task.
or you can call it as a group of commands.
shell functions let you to modularize your program by dividing it up into seperate tasks.
functions are used wherever there is repetetive code.
functions should be declared at the begining of the script.
A functions is essentially a code block, which means stdin can be redirected
function may have local variables, and may be recursive.
Bash uses dynamic scoping model, whereby variables declared with local are visible inside that function and in other functions it calls.
A function may use RETURN commands to return an exit status to the caller/calling shell program.
it is also possible to have nested functions, i.e functions within functions
A Bash function is a block of reusable code designed to perform a particular operation. Once defined, the function can be called multiple times within a script.
or you can say group of commands called for later execution. functions are used mainly to avoid repetetive code.
syntax: function funtionname{ command... } [redirections] or function_name() { command .... } [redirections]
defining function sayHello using syntax 1, it has single command called echo
#!/usr/bin/bash function sayHello { echo "Hello World!"; }
Calling function within srcipt
sayHello;
defining function sayHello using syntax 2,
#!/usr/bin/bash function sayHello() { echo "Hello World!"; }
sayHello;space should be given after function name and before curly braces.
is there a way to pass arguments to functions, answer yes,unlike 'C' programming where function accepts parameters,But in Bash functions accepts paramaters using positional arguments($1,$2,$3 etc.,)
passing numbers to bash function
#!/usr/bin/bash function sum() { s=$(( $1+$2+$3 )) echo "sum of numbers :${s}"; } sum 10 20 30
output
sum of numbers :60
find a filename which has lengthy name in a given directory
In this example maxLength function dir variable has directory name,$HOME (shell variable expansion occurs)+Downloads. files variable has only files in a given directory. loop through all files using for loop. extract only file name using basename shell built-in function. If stament checks for is length of the file is greater than the max,if true, sets max value with current file name length,and records filename in name variable.
#!/usr/bin/bash function maxLength() { dir="$HOME/Downloads" files=`find $dir -maxdepth 1 -type f`; name=''; for file in ${files} do file=`basename "${file}"` if [[ ${#file} -gt "${max}" ]]; then max=${#file}; name=${file} fi done echo -e "file in a dir=${dir} \n filename:${name} \n length:${max}"; } maxLength;
Variables declared outside the function are called as global variables. Variables declared within the function are called as local variables.
scope of Global variables is from beginning of the script to end of the script. whereas scope of local variables within the function only.
Accessing Global variable name in function test
name="jhon" function test() { echo $name; } test;
output: jhon will be printed
Local variables superceds Global variables,in order to avoid class between local & global variables, different names should be followed.
name="jhon" function test() { name="peter"; echo $name; } test;
output: "peter" will be printed
Local variables declared within function can be accessed the functions it is calling
function f1() { name="jhon"; echo "name before calling f2 function $name"; f2; echo "name After calling f2 function $name"; } function f2() { name=${name}" peter" } f1;output:
name before calling f2 function "jhon" name After calling f2 function "jhon peter"
Functions can read input from a file, command or from a Here Document using IO redirection operators
function reading data from a file
$cat country.txt Country Capital ISD_CODE USA Washington 1 China Beijing 86 Japan Tokyo 81 India Delhi 91
file="country.txt" function readFile() { while read line do echo $line done }<${file} readFile;
function reading data from a Here Document
function redir() { read line; echo $line; } <<< $(echo 'one two three')output:
one two threeadv bash bash ref manual linux shell scripting -
ADS