#!
/bin/bash
IFS – GetOpts – Source - Functions
1
IFS
Internal Field Separator
IFS VARIABLE
The IFS (Internal Field Separator) environment
variable defines a list of characters that bash shell
uses as field separators (for word splitting
operations). By default, the bash shell considers the
following characters as field separators:
space ( )
tab (\t)
newline (\n)
3
IFS VARIABLE
• As we see in this example “new”
and “york” are split up in our for
loop. To fix this we need to
change the IFS variable to only
separate on the new line
character.
4
IFS VARIABLE
• As we can see this resolved the separation issue.
5
IFS EXAMPLE: SPLITTING PASSWD
sve@hp-lnx-sve:~/scripts$ ./[Link] | more
#!/bin/bash
fields in root:x:0:0:root:/root:/bin/bash
root
IFSOLD=$IFS x
IFS=$'\n' 0
0
root
for line in $(cat /etc/passwd) /root
do /bin/bash
printf "\nfields in $line\n"
fields in daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
IFS=$':' daemon
for field in $line x
do 1
printf "\t$field\n" 1
daemon
done /usr/sbin
done /usr/sbin/nologin
fields in bin:x:2:2:bin:/bin:/usr/sbin/nologin
bin
x
--More--
6
RED='\033[0;31m' NC='\033[0m' # No Color echo -e "I ${RED}love${NC} Stack Overflow"
Getopts
GETOPTS EXAMPLE
No error message generated for unknown options
Option b is expecting an argument (ex. filenaam)
8
GETOPTS EXAMPLE
#!/bin/bash sve@hp-lnx-sve:~/scripts$ ./[Link] -a AA -b BB -v CC DD
# [Link] demo script ALPHA: AA
BETA: BB
ALPHA="" VERBOSE: 1
BETA="" ARGS: CC DD
VERBOSE=0 sve@hp-lnx-sve:~/scripts$
# Process options with getopts
while getopts ":a:b:v" opt; do
case "$opt" in
a) ALPHA="$OPTARG";;
b) BETA="$OPTARG";;
v) VERBOSE=1;;
:) echo "Error: Option -$OPTARG requires an argument." >&2
exit 1;;
\?) echo "Error: Invalid option -$OPTARG" >&2
exit 1;;
esac
done
# Remove parsed options from $@
shift $((OPTIND - 1))
echo "ALPHA: $ALPHA"
echo "BETA: $BETA"
echo "VERBOSE: $VERBOSE"
echo "ARGS: $*" 9
RED='\033[0;31m' NC='\033[0m' # No Color echo -e "I ${RED}love${NC} Stack Overflow"
BASH_SOURCE & SOURCE
BASH_SOURCE
11
SOURCE – INCLUDE EXTERNAL FILE
12
Functions
CREATING FUNCTIONS
• Syntax:
14
SAMPLE FUNCTION
15
FUNCTION REMARKS
• Functions can be defined anywhere in the script.
But the definition needs to be done before you call
the function.
• Make sure to use unique names for your functions.
Otherwise you can overwrite another function, with
the same name, within your script.
• Function names are case sensitive
16
FUNCTION EXIT STATUS
• The default exit status of a function is the exit
status of the last called command in that function.
• You can use the return command to change the exit
status. The return command needs to be the last
command in your function.
• You need to capture the exit status directly after
you called the function using $?
• Exit status can only contain values from 0-255
17
FUNCTION EXIT STATUS EXAMPLE
18
FUNCTION EXIT STATUS EXAMPLE 2
19
FUNCTION OUTPUT
• Just as with any other bash command you can
capture the output of a function
21
FUNCTION PARAMETERS
• Just as with any other bash command you can pass
parameters to a function
22
FUNCTION VARIABLES
There are 2 different types of variables that can be
used in functions:
Global variables
Local variables
23
GLOBAL VARIABLES
• Can be used within and outside the function. This is
the default behavior.
24
LOCAL VARIABLES
• Local variables in functions are safer to use. If you
have a long script and by accident use the same
variable name you won’t overwrite the value of the
variable that was used elsewhere in the script.
• Syntax:
• local varname
25
LOCAL VARIABLES EXAMPLE
26
Thanks!
Tom Van Den Broeck
tomvdb@[Link]
Translation and updates
[Link]@[Link]
27