(CC - 23) Lab 3
(CC - 23) Lab 3
Lab 3
Bash Shell Scripting
Agenda
• The basic features of bash are copied from sh, and also
adds some of the interactivity features from csh.
test.sh
#!/bin/bash
test.sh
#!/bin/bash
date
d=$(date +%m-%d-%Y)
sysadmin@localhost:~$ ./
echo $d test.sh
d=$(date '+%A %d-%B, %Y') Fri 03 Mar 2023 04:32:04 PM
echo $d EET
d=$(date +%m-%Y) 03-03-2023
echo $d Friday 03-March, 2023
03-2023
sysadmin@localhost:~$
Date and Time Formatting Options
date +%a Weekday Name of weekday in short (like Sun, Mon, Tue, Wed, Thu, Fri, Sat) Mon
date +%A Weekday Name of weekday in full (like Sunday, Monday, Tuesday) Monday
date +%b Month Name of Month in short (like Jan, Feb, Mar ) Jan
date +%B Month Month name in full (like January, February) January
date +%d Day Day of month (e.g., 01) 04
date +%D MM/DD/YY Current Date; shown in MM/DD/YY 02/18/18
date +%F YYYY-MM-DD Date; shown in YYYY-MM-DD 2018-01-19
date +%H Hour Hour in 24-hour clock format 18
date +%I Hour Hour in 12-hour clock format 10
date +%j Day Day of year (001..366) 152
date +%m Month Number of month (01..12) (01 is January) 05
date +%M Minutes Minutes (00..59) 52
date +%S Seconds Seconds (00..59) 18
date +%N Nanoseconds Nanoseconds (000000000..999999999) 300231695
date +%T HH:MM:SS Time as HH:MM:SS (Hours in 24 Format) 18:55:42
date +%u Day of Week Day of week (1..7); 1 is Monday 7
date +%U Week Displays week number of year, with Sunday as first day of week (00..53) 23
date +%Y Year Displays full year i.e. YYYY 2018
date +%Z Timezone Time zone abbreviation (Ex: IST, GMT) IST
Bash Sleep Command
Sleep Command sleep NUMBER[SUFFIX]
• Used to insert a delay or pause the execution for a
specified period.
• SUFFIX options: s (seconds), m (minutes), h (hours), d
(days)
test.sh
#!/bin/bash sysadmin@localhost:~$ ./
test.sh
echo HH:MM:SS HH:MM:SS
17:13:14/03-03-2023
echo $(date +%H:%M:%S/%m-%d-%Y) 17:13:18/03-03-2023
sleep 4 sysadmin@localhost:~$
echo $(date +%H:%M:%S/%m-%d-%Y)
Bash Conditional Statements
Bash IF
if [ expression ]
# ^ ^ ^ please note these
spaces
then
statement(s)
fi
Bash Conditional Statements (Cont.)
Bash IF
• For compound expressions, Observe that the condition has
double square brackets.
• You can use this syntax with any number of expressions
# if condition is true
if [ "hello" == "hello" ]
then
sysadmin@localhost:~$ ./
echo "hello equals hello" test.sh
fi hello equals hello
sysadmin@localhost:~$
# if condition is false
if [[ "hello" == "bye" ]]; then
echo "hello equals bye"
fi
Bash Conditional Statements (Cont.)
Bash IF - Compare Numbers
• Relational Operators
if [ $a -eq $b ]; then
echo "$a -eq $b : a is equal to b"
sysadmin@localhost:~$ ./test.sh
else
echo "$a -eq $b: a is not equal to b" 10 -eq 20: a is not equal to b
fi
10 -gt 20: a is not greater
if [ $a -gt $b ]; then than b
echo "$a -gt $b: a is greater than b" sysadmin@localhost:~$
else
echo "$a -gt $b: a is not greater than
b"
Bash Conditional Statements (Cont.)
Operator Description
= or == Returns true if the value of two operands is equals
!= Returns true if the value of two operands is not equals
-z Returns true if length of the string is zero.
-n Returns true if length of the string non-zero.
-f Returns true if the file is existing.
if [ -f /etc/passwd ]
then
sysadmin@localhost:~$ ./
echo "passwd - File exists."
else test.sh
echo "passwd - File does not passwd - File exists.
exist." sysadmin@localhost:~$
fi
Bash Conditional Statements (Cont.)
Bash Else IF – elif
if <expression>; then
<commands>
elif <expression>; then
<commands>
else
<commands>
fi
Bash Conditional Statements (Cont.)
Bash IF - Compare Numbers
test.sh
#!/bin/bash
n=2
if [ $n -eq 1 ]; then
echo value of n is 1
elif [[ $n -eq 2 && $n -lt 5 ]]; then
echo value of n is less than threshold
fi
sysadmin@localhost:~$ ./test.sh
value of n is less than threshold
sysadmin@localhost:~$
Bash Conditional Statements (Cont.)
Bash Case
case EXPRESSION in
CASE1)
COMMAND-LIST
;;
CASE2)
COMMAND-LIST
;;
CASEN)
COMMAND-LIST
;;
*)
COMMAND-LIST
;;
esac
Bash Conditional Statements (Cont.)
Bash Case - Example
#!/bin/bash
time=15
case $time in
9)
echo Good Morning! sysadmin@localhost:~$ ./test.sh
;;
Good Day!
21)
echo Good Night!
sysadmin@localhost:~$
;;
*)
echo Good Day!
;;
esac
Bash Loops
Bash For Loop
• Allows iterating a specific set of statements over a
series of words in a string, elements in a sequence, or
elements in an array.
#!/bin/bash
# declare an array
arr=( "bash" "shell" "script" ) sysadmin@localhost:~$ ./test.sh
bash
# for loop that iterates over each shell
element in arr script
for i in "${arr[@]}" sysadmin@localhost:~$
do
echo $i
done
Bash Loops (Cont.)
Bash For Loop
• Bash For loop consider whitespaces in given string as word
separators.
#!/bin/bash
# declare a string
str="bash shell script" sysadmin@localhost:~$ ./test.sh
bash
# for loop that iterates over the shell
words script
for i in $str sysadmin@localhost:~$
do
echo $i
done
Bash Loops (Cont.)
Bash For Loop - Iterate Over a Sequence
• To iterate over a sequence of numbers.
sysadmin@localhost:~$ ./test.sh
#!/bin/bash 1
2
3
for i in $(seq 1 10) 4
5
do 6
echo $i 7
8
done 9
10
sysadmin@localhost:~$
Bash Loops (Cont.)
Bash For Loop - Use Counter to Iterate
Over Elements of Array
• To iterate over an array’s elements using a variable for counter
#!/bin/bash
arr=("bash" "shell" "script")
test.sh
#!/bin/bash