Bash Scripting Notes
Bash Scripting Notes
BY / ISLAM SALAH
COURSE AGENDA
• What is Shell ?
• Types of Shell
• What is Shell Scripting?
• Benefits of Shell Scripting
• Basic Linux Commands
• Basic Bash Scripting
• Useful Shell commands
• Conditionals
• Loops
• Functions
WHAT IS SHELL ?
• A shell accepts and interprets commands.
• It offers an efficient environment for interacting with the operating system and scripting.
• Bash is a programming language for running commands. Bash is the default shell in Linux
operating systems. It is widely used, so some familiarity with Bash is expected in many
systems or development roles.
BASIC BASH SCRIPTING
Type of Variables:
• Local Variables
• Environment Variables
• Predefined Variables
BASIC BASH SCRIPTING
Local Variables:
• When defining a variable, the variable name must be prefixed with the dollar ($)
symbol.
• The variable must contain no spaces or special characters within the variable name. A
variable name can contain only letters (a to z or A to Z), numbers (0 to 9), or the
underscore character ( _), and they are usually capitalized (e.g. VARIABLE).
name=“Salah“
Age = “28”
BASIC BASH SCRIPTING
Display shell Variables:
name="John"
echo ${name}
BASIC BASH SCRIPTING
Environment Variables:
BASIC BASH SCRIPTING
Predefined Variables:
Predefined variables are variables known to the shell and their values are assigned by the shell.
$# Number of arguments
$* List of all arguments
$0 Script name
$1, $2,.. First argument, second argument,..
$? Return code of the last command
HANDS ON AND DEMONSTRATION
1. Open the Bash shell in a Linux environment.
2. At the command prompt, enter the echo command and an environment variable from
the following list:
• echo $HOME
• echo $SHELL
• echo $USER
• echo $PATH
3. env command is a shell command for Linux. You use this command to print a list of
environment variables or run another utility in an altered environment.
Aliases can be set temporarily in the current shell, but it is more common to set them in
the user's .bashrc file so that they are permanent.
alias alias_name=‘command’
$ unalias ll
WHAT ARE SCRIPTS?
• Scripts are text files of commands and related data.
• Automation allows scripts to run more quickly than if they are run manually.
WHAT IS SHELL SCRIPTING?
• A shell script is a list of commands
in a computer program that is run
by the Unix shell to execute
commands.
• Automating tasks
CHARACTERISTIC OF SHELL SCRIPTING
• Shell scripts don’t have to be boring
• Reusing
• Always available
• Readability
• Run/Executing script
EXAMPLE BASH SCRIPTING
$ touch backup_script.sh
$ chmod +x backup_script.sh
$ vi backup_script.sh
$ ./backup_script.sh
• #! is referred to as a shebang.
• Example: #!/bin/bash
SCRIPT DOCUMENTATION
• Some administrators create a script
template, which contains all the relevant
information and sections.
#!/bin/bash
name="Islam Salah“
echo ${name}
echo ${name/J/j} (substitution)
echo ${name:0:2} (slicing)
echo ${name::2} (slicing)
echo ${name::-1} echo ${name:(-1)} (slicing from right)
length=2 echo ${name:0:length}
BASIC BASH SCRIPTING
Reading User Input:
• Create script to get name and print message to welcome him, in interactive style
HANDS ON ACTIVITY
• grep
• cut
• sed
• awk
• sort
USING | GREP
• grep is commonly used after another command, along with a pipe (|).
• Examples:
• ps -ef | grep sshd
• cat /var/log/secure | grep fail
USING | CUT
The cut command requires the user to specify bytes, fields, or characters to extract.
When using a field, you must specify the delimiter of the file.
To substitute text
$sed ‘s/salah/fargany/g’ myfile
$sed –n ‘s/salah/fargany/2’ myfile
USING | SED | EXAMPLE
To delete lines from 1 to 3 (including 3rd line)
$sed ‘1,3d’ myfile
• awk scans a file line by line, searching for lines that match a specified pattern performing
selected actions
• awk stands for the first initials in the last names of each authors of the language, Alfred Aho,
Peter Weinberger, and Brian Kernighan
Record separators are by default carriage return, stored in a built-in variables ORS and RS.
Each record consists of words called fields which by default separated by white spaces.
NF variables contains the number of fields in a record
FS variable holds the input field separator, space/tab is the default.
The field separator is @ so the first field becomes everything that is before that @ instead of the
first name
awk –F @ ‘{print $1}’ customers.txt
This program prints Start Processing, then displays the first field of each record, and finally
displays Done!:
awk 'BEGIN { print "Start Processing." }; { print $1 }; END { print “Done ! " }' names.csv
USING | SORT
Sorts file contents in a specified order: alphabetical, reverse order, number, or month
• Examples:
-o outputs the result to a file
(sort file.txt –o sortedfile.txt is like sort file.txt > sortedfile.txt)
-r sorts in reverser order
-n sorts numerically if the file contains numbers
-k sorts according to the kth column (if the file is formatted as a table )
-u removes duplicates
-t for delimiter
USING | SORT| EXAMPLE
sort names.csv
sort -r names.csv
sort -k 2n salary.txt
sort -u names.csv
• Integer Testing
int1 –eq int2 equal to
int1 –ne int2 not equal to
int1 –gt int2 greater than
int1 –ge int2 greater than or equal
int1 –lt int2 less than
int1 –le int2 less than or equal
TESTING AND LOGICAL OPERATIONS
-a and operator
-o or operator
-f filename file existence
-h filename symbolic link
-r filename readable
-w filename writable
-x filename executable
CONDITIONAL STATEMENTS | IF
if command
then
… commands …
fi
if [ expression ]
then
… commands …
fi
Nested condition
if command
Then
… commands …
if command
Then
… commands …
fi
fi
CONDITIONAL STATEMENTS | IF
if [ expression ]
then
... commands ...
elif [ expression ]
then
... commands ...
else
... commands ...
fi
Or inline:
if <condition>; then <command> fi
Note:
- The semicolon (;) is not mandatory if not writing inline.
- Indentation is used for better readability but is not required.
CONDITIONAL STATEMENTS | EXAMPLE
#!/usr/bin/bash
if test -f file1
then
cat file1
fi
if [ -f file1 ];
then
cat file1 > new_file.txt
fi
CONDITIONAL STATEMENTS | EXAMPLE
if [ cp file1 /tmp ];
then
rm file1
fi
HANDS ON IF STATEMENT
INTEGER COMPARISON OPERATORS
INTEGER COMPARISON OPERATORS| EXAMPLE
#!/bin/bash
#Compares integer $1 and $2
if (( $1 == $2 ));
then
echo ".."
fi
INTEGER COMPARISON OPERATORS| EXAMPLE
if [ $1 -gt $2 ]
then
echo ".."
fi
if [[ $1 -gt $2 ]]
then
echo ".."
fi
CONDITIONAL STATEMENTS | EXAMPLE
#!/bin/bash
#Compares $1 and $2
if [ $1 -gt $2 ];
then
echo "the first number is greater then the second number"
elif [ $1 -lt $2 ];
then
echo "the second number is greater then the first number"
else
echo " the numbers are equal"
fi
STRING COMPARISON OPERATORS
STRING COMPARISON OPERATORS | EXAMPLE
#!/bin/bash
#Compares letters $1 and $2
if [ "$1" == "salah" ];
then
echo "words are the same"
else
echo "$1 is not equal salah"
fi
STRING COMPARISON OPERATORS | EXAMPLE
#!/bin/bash
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
*)
Default condition to be executed
;;
esac
CONDITIONAL STATEMENTS | CASE
case variable in
value1)
Command(s)
;;
value2)
Command(s)
;;
*)
Command(s)
;;
esac
CONDITIONAL STATEMENTS | EXAMPLE
#!/bin/bash
case "$fruit" in
"apple")
echo "Apple pie is quite tasty."
;;
"banana")
echo "I like banana nut bread."
;;
"kiwi")
echo "New Zealand is famous for kiwi."
;;
*)
echo “your answer is out of scope”
esac
CONDITIONAL STATEMENTS | EXAMPLE
#!/bin/bash
case $month in
February)
echo "There are 28/29 days in $month.";;
April | June | September | November)
echo "There are 30 days in $month.";;
January | March | May | July | August | October | December)
echo "There are 31 days in $month.";;
*)
echo "Unknown month. Please check if you entered the correct month name: $month";;
esac
CONDITIONAL SELECT| EXAMPLE
select choice in Ahmed Adel Tamer Quit
do
case $choice in
Ahmed)
echo Ahmed is good boy
;;
Adel)
echo Adel is the best
;;
Tamer)
echo Tamer is a bad boy
;;
*)
echo $REPLY is not one of the choices.
;;
esac
done
CONDITIONAL SELECT| EXAMPLE
Output:
1)Ahmed
2)Adel
3)Tamer
#? 1
Ahmed is a good boy
1)Ahmed
2)Adel
3)Tamer
#?5
5 is not one of the choices
1)Ahmed
2)Adel
3)Tamer
LOOP STATEMENTS
• While
• Until
• For
LOOP STATEMENTS | WHILE
while command
do
… command …
done
Inline:
while command; do command; done
Examples:
num=0
while [ $num –lt 10 ]
do
echo “hello it’s number: $num”
num=$(($num+1))
done
LOOP STATEMENTS | WHILE
echo "please enter your name:"
read name
Write a bash script game that asks the executer to guess the number and exit only when he gets the
correctly number
LOOP STATEMENTS | UNTIL
until command
do
command(s)
done
Example:
hour=1
until [ $hour –lt 24 ]
do
case $hour in
[6-11] ) echo good morning ;;
12) echo lunch time ;;
[1-4]) echo work time ;;
*) echo Good Night ;;
esac
hour=$hour+1
done
LOOP STATEMENTS | FOR
• It is used to execute commands a finite number of times on a list of items (files/usernames)
Example:
for ((a=1; a <= 5 ; a++))
do
echo "Welcome $a times."
done
Example:
for i in $(seq 5)
do
echo "Welcome $i times"
done
LOOP STATEMENTS | FOR | EXAMPLE
Example:
for i in {1..5}
do
echo "Welcome $i times"
done
Example:
for i in {0..10..2}
do
echo "Welcome $i times"
done
Example:
BOOKS=('Python' 'Java' 'Bash' 'Go' 'Rubyonrails')
for book in "${BOOKS[@]}";
do
echo "Book is: $book"
done
LOOP CONTROL
Example:
#!/bin/bash
• Format:
• function function-name {commands; commands;}
• function_name
• Return value $?
FUNCTION | EXAMOLE
function F1()
{
retval='I like programming‘
echo $retval
F1
echo $retval
FUNCTION | EXAMPE
#! /bin/bash
function increment {
(( sum = $1 + 1 ))
return $sum
}
www.linkedin.com/in/islam-salah-25b4a4a4
010 68-280-165