Bash Scripting Tutorial - LinuxConfig
Bash Scripting Tutorial - LinuxConfig
(/)
(/)
Contents
1.Global vs. Local variables(https://linuxcon g.org/bash-
scripting-tutorial#h1-global-vs-local-variables)
2.Declare simple bash array(https://linuxcon g.org/bash-
scripting-tutorial#h2-declare-simple-bash-array)
3.Read le into bash array(https://linuxcon g.org/bash-
scripting-tutorial#h3-read- le-into-bash-array)
4.Simple Bash if/else statement
(https://linuxcon g.org/bash-scripting-tutorial#h4-simple-
bash-if-else-statement)
5.Nested if/else (https://linuxcon g.org/bash-scripting-
tutorial#h5-nested-if-else)
6.Arithmetic Comparisons(https://linuxcon g.org/bash-
scripting-tutorial#h6-arithmetic-comparisons)
7.String Comparisons(https://linuxcon g.org/bash-scripting-
tutorial#h7-string-comparisons)
8.Bash for loop (https://linuxcon g.org/bash-scripting-
tutorial#h8-bash-for-loop)
9.Bash while loop(https://linuxcon g.org/bash-scripting-
tutorial#h9-bash-while-loop)
10.Bash until loop(https://linuxcon g.org/bash-scripting-
tutorial#h10-bash-until-loop)
11.Control bash loop with(https://linuxcon g.org/bash-
scripting-tutorial#h11-control-bash-loop-with)
12.Escaping Meta characters(https://linuxcon g.org/bash-
scripting-tutorial#h12-escaping-meta-characters)
13.Single quotes (https://linuxcon g.org/bash-scripting-
tutorial#h13-single-quotes)
14.Double Quotes(https://linuxcon g.org/bash-scripting-
tutorial#h14-double-quotes)
15.Bash quoting with ANSI-C style
(https://linuxcon g.org/bash-scripting-tutorial#h15-bash-
quoting-with-ansi-c-style)
16.Bash Addition Calculator Example
(/)
(https://linuxcon g.org/bash-scripting-tutorial#h16-bash-
addition-calculator-example)
17.Bash Arithmetics(https://linuxcon g.org/bash-scripting-
tutorial#h17-bash-arithmetics)
18.Round oating point number
(https://linuxcon g.org/bash-scripting-tutorial#h18-round-
oating-point-number)
19.Bash oating point calculations
(https://linuxcon g.org/bash-scripting-tutorial#h19-bash-
oating-point-calculations)
20.STDOUT from bash script to STDERR
(https://linuxcon g.org/bash-scripting-tutorial#h20-stdout-
from-bash-script-to-stderr)
21.STDERR from bash script to STDOUT
(https://linuxcon g.org/bash-scripting-tutorial#h21-stderr-
from-bash-script-to-stdout)
22.stdout to screen(https://linuxcon g.org/bash-scripting-
tutorial#h22-stdout-to-screen)
23.stdout to le (https://linuxcon g.org/bash-scripting-
tutorial#h23-stdout-to- le)
24.stderr to le (https://linuxcon g.org/bash-scripting-
tutorial#h24-stderr-to- le)
25.stdout to stderr(https://linuxcon g.org/bash-scripting-
tutorial#h25-stdout-to-stderr)
26.stderr to stdout(https://linuxcon g.org/bash-scripting-
tutorial#h26-stderr-to-stdout)
27.stderr and stdout to le(https://linuxcon g.org/bash-
scripting-tutorial#h27-stderr-and-stdout-to- le)
This bash script tutorial assumes no (/)
previous knowledge of bash scripting.As
you will soon discover in this quick
comprehensive bash scriptingguide,
learning the bash shell scripting is very
easy task. However, if you do not nd an
(http://linuxcon g- answer to your questions by reading this
bash tutorial or you need extra help, feel
free to ask us on our newLinux Forum
(http://forum.linuxcareer.com). We will
org.tradepub.com/free/w_mach02#linux- be more than happy to help you with
o ers) your bash questions there.
>PDF DOWNLOAD<(http://linuxcon g-
org.tradepub.com/free/w_mach02#linux- Lets begin this bash scripting tutorial
o ers) with a simple "Hello World" script.Let's
start with Learning the bash Shell: Unix
Shell Programming
$ which bash
(/)
#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING
$ chmod +x hello_world.sh
./hello_world.sh
#!/bin/bash
tar -czf myhome_directory.tar.gz /home/linuxcon
fig
Variables (/)
#!/bin/bash
STRING="HELLO WORLD!!!"
echo $STRING
#!/bin/bash
OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig
Arrays
ARRAY[$count]=$LINE
((count++))
done
Bash if / else /
statements
Simple Bash if/else statement (/)
Please note the spacing inside the [ and ] brackets! Without the
spaces, it won't work!
#!/bin/bash
directory="./BashScripting"
Nested if/else
#!/bin/bash
else
Bash Comparisons
Arithmetic Comparisons
-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=
#!/bin/bash
# declare integers
NUM1=2
NUM2=2
if [ $NUM1 -eq $NUM2 ]; then
echo "Both Values are equal"
else
echo "Values are NOT equal"
fi
#!/bin/bash
# declare integers
NUM1=2
NUM2=1
if [ $NUM1 -eq $NUM2 ]; then
echo "Both Values are equal"
else
echo "Values are NOT equal"
fi
#!/bin/bash
# declare integers (/)
NUM1=2
NUM2=1
if [ $NUM1 -eq $NUM2 ]; then
echo "Both Values are equal"
elif [ $NUM1 -gt $NUM2 ]; then
echo "NUM1 is greater then NUM2"
else
echo "NUM2 is greater then NUM1"
fi
String Comparisons
= equal
!= not equal
-z s1 string s1 is empty
#!/bin/bash
#Declare string S1
S1="Bash"
#Declare string S2
S2="Scripting"
if [ $S1 = $S2 ]; then
echo "Both Strings are equal"
else
echo "Strings are NOT equal"
fi
#!/bin/bash
#Declare string S1
S1="Bash"
#Declare string S2
S2="Bash"
if [ $S1 = $S2 ]; then
echo "Both Strings are equal"
else
echo "Strings are NOT equal"
fi
Bash File Testing (/)
-d
Check for directory existence
directoryname
#!/bin/bash
file="./file"
if [ -e $file ]; then
echo "File exists"
else
echo "File does not exists"
fi
Similarly for example we can use while loop to check if le does
(/)
not exists. This script will sleep until le does exists. Note bash
negator "!" which negates the -e option.
#!/bin/bash
while [ ! -e myfile ]; do
# Sleep until file does exists/is created
sleep 1
done
Loops
#!/bin/bash
# This bash script will locate and replace spac
es
# in the filenames
DIR="."
# Controlling a loop with bash read command by
redirecting STDOUT as
# a STDIN to while loop
# find will not truncate filenames containing s
paces
find $DIR -type f | while read file; do
# using POSIX class [:space:] to find space in
the filename
if [[ "$file" = *[[:space:]]* ]]; then
# substitute space with "_" character and conse
quently rename the file
mv "$file" `echo $file | tr ' ' '_'`
fi;
# end of while loop
done
Bash Functions
!/bin/bash
# BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER (/)
function function_B {
echo Function B.
}
function function_A {
echo $1
}
function function_D {
echo Function D.
}
function function_C {
echo $1
}
# FUNCTION CALLS
# Pass parameter to function A
function_A "Function A."
function_B
# Pass parameter to function C
function_C "Function C."
function_D
Bash Select
#!/bin/bash
# bash select
select word in "linux" "bash" "scripting" "tuto
rial"
do
echo "The word you have selected is: $word"
# Break, otherwise endless loop
break
done
exit 0
Case statement
conditional
#!/bin/bash
echo "What is your preferred programming / scri (/)
pting language"
echo "1) bash"
echo "2) perl"
echo "3) phyton"
echo "4) c++"
echo "5) I do not know !"
read case;
#simple case bash structure
# note in this case $case is variable and does
not have to
# be named case this is just an example
case $case in
1) echo "You selected bash";;
2) echo "You selected perl";;
3) echo "You selected phyton";;
4) echo "You selected c++";;
5) exit
esac
#!/bin/bash
Single quotes
Single quotes in bash will suppress special meaning of every meta
characters. Therefore meta characters will be read literally. It is
not possible to use another single quote within two single quotes
not even if the single quote is escaped by backslash.
#!/bin/bash
Double Quotes
Double quotes in bash will suppress special meaning of every
meta characters except "$", "\" and "`". Any other meta
characters will be read literally. It is also possible to use single
quote within double quotes. If we need to use double quotes
within double quotes bash can read them literally when escaping
them with "\". Example:
#!/bin/bash
#!/bin/bash
Arithmetic Operations
let RESULT1=$1+$2
echo $1+$2=$RESULT1 ' -> # let RESULT1=$1+$2'
declare -i RESULT2
RESULT2=$1+$2
echo $1+$2=$RESULT2 ' -> # declare -i RESULT2;
RESULT2=$1+$2'
echo $1+$2=$(($1 + $2)) ' -> # $(($1 + $2))'
(/)
Bash Arithmetics
#!/bin/bash
# bash subtraction
let SUBTRACTION=7-8
echo "7 - 8 =" $SUBTRACTION
# bash multiplication
let MULTIPLICATION=5*8
echo "5 * 8 =" $MULTIPLICATION
# bash division
let DIVISION=4/2
echo "4 / 2 =" $DIVISION
# bash modulus
let MODULUS=9%4
echo "9 % 4 =" $MODULUS
cat $1 2>&1
stdout to screen
The simple way to redirect a standard output ( stdout ) is to
simply use any command, because by default stdout is
automatically redirected to screen. First create a le " le1":
$ touch file1
$ ls file1
file1
As you can see from the example above execution of ls command
(/)
produces STDOUT which by default is redirected to screen.
stdout to le
The override the default behavior of STDOUT we can use ">" to
redirect this output to le:
stderr to le
By default STDERR is displayed on the screen:
$ ls
file1 STDOUT
$ ls file2
ls: cannot access file2: No such file or direct
ory
$ ls
file1 STDOUT
$ ls file1 file2 2> STDERR
file1
$ cat STDERR
ls: cannot access file2: No such file or direct
ory
stdout to stderr
It is also possible to redirect STDOUT and STDERR to the same
le. In the next example we will redirect STDOUT to the same
descriptor as STDERR. Both STDOUT and STDERR will be
redirected to le "STDERR_STDOUT".
$ ls
file1 STDERR STDOUT
$ ls file1 file2 2> STDERR_STDOUT 1>&2
$ cat STDERR_STDOUT
ls: cannot access file2: No such file or direct
ory
file1