Table of contents
Introduction
What is Shell?
The shell’s job, then, is to translate the user’s command lines into operating system instructions.
Components of Linux systems are
Hardware – It includes resources like CPU, RAM etc.
Kernel – It allocates hardware resources to software processes.
Shell - It is software that interprets command given by user.
Types of Shell – bash, sh, ksh , tsh, fish ,zsh.
How to check Shell type in Linux system?
echo $0
Definition of Bash scripting
A bash script is a file containing a sequence of commands that are executed by the bash program line by line. It allows you to perform a series of actions, such as navigating to a specific directory, creating a folder, and launching a process using the command line.
By saving these commands in a script, you can repeat the same sequence of steps multiple times and execute them by running the script.
What is Shebang?
This is first line script. It defines interpreter for script.
#!/bin/bash
How to run a script?
To run a Script, it must have execute permission. In Linux every file has read, write and execute permissions, for script file we require execute permissions. We can check permissions using below command. Here x
denotes execute permission. If file do not have execute permission, we can modify permissions using chmod
command
ls –l
chmod +x [file]
There are 3 ways to run a script
We can terminate script using Ctrl + C
and stop using Ctrl + z
.
Bash scripting basics
Comments in bash scripting
We can add comments in bash script using. This means these line are ignored by interpreter.
#
- Single line comment
<<
- Multi line comment
#!/bin/bash
#This is comments script
echo "Hello World"
<<comment
End of comments script
comment
Variables in bash scripting
Variables are used to store data. We can use variables to read , access and manipulate data.
We can set variables using multiple ways.
- Assign values directly
#!/bin/bash
#Assign values to variable directly
city="Pune"
echo "city name is $city"
- Assign value based on output from another command. We use
$
symbol for this.
#!/bin/bash
#Assign value based on output from another command
HOSTNAME=$(hostname)
echo "Hostname is $HOSTNAME"
Variable naming conventions
Variable names must start with letter or underscore.
Variables can contain letters, numbers and underscores.
Variable names are case sensitive.
Variable should not contain space or special characters
Avoid using reserved keywords.
Constant variable
Once defined we cannot change it in script.
#!/bin/bash
#Assign values to variable directly
readonly city="Pune"
echo "city name is $city"
city = "Mumbai"
Arrays in bash scripting
We can store multiple values in variable. We can different types of values. Values are separated by space.
#!/bin/bash
#How to define array?
myArray=(1 2 3 Hello "Hello World")
echo "All values in array are ${myArray[*]}"
#How to read array values?
echo "Value in first index is ${myArray[0]}"
#How to find length of array?
echo "Length of array is ${#myArray[*]}"
#How to get specific values?
echo "Values form 2-3 ${myArray[*]:2:3}"
#How to update an array?
myArray+=(5 6 8)
echo "Updated array is ${myArray[*]}"
#How to replace value in array?
myArray[1]=10
#Key-value array
#How to define key-value array?
declare -A myArray
myArray=([name] = Ashvini [city] = Pune)
echo "{$myArray[name]}"
String operations
In Bash scripting, a string is a sequence of characters enclosed within single quotes (')
or double quotes (")
.Single-quoted strings preserve the literal text, while double-quoted strings allow for variable substitution and interpretation of certain escape sequences.
#!/bin/bash
#String concatenation
string1 ="Hello, "
string2 = "world"
concatenated_string = $string1$string2
echo $ concatenated_string
#Length of string
text="Hello, world!"
length="Length of string is ${#text}"
#Substring extraction
full_string="Bash scripting is fun!"
substring="Extracted substring from index 5 to 9 is ${full_string:5:9}" # Extracts "scripting"
Substring operations
#!/bin/bash
string="Hello, world!"
start=7
length=5
substring=$(expr substr "$string" "$start" "$length")
echo "Substring: $substring"
#Get length of string using expr
length=$(expr length "$string")
echo "The length of the string is: $length"
#substring removal
path="/home/user/documents/file.txt"
base_name="${path##*/}" #Extracts file.txt
echo "Base name is $base_name"
In this example, the expr substr "$string" "$start" "$length"
command extracts a substring from the string starting at the 7th character and spanning 5 characters. The result is stored in the substring variable and then printed.
Pattern Matching
('${string%pattern}')
and '${string%pattern}')
: Remove text from the beginning of string that matches a specified pattern.
Example:
#!/bin/bash
file="document.docx"
extension_removed="${file%.*}" #Removes extension, leaves document
echo "Removed extension is $extension_removed"
Case Conversion ('${string,,}'
and '${string^^}')
: Convert the case of the characters in string.
Example:
#!/bin/bash
text="Hello World"
lowercase="${text,,}" #Converts to "hello world"
echo "Lower case is $lowercase"
uppercase="${text,,}" #Converts to "hello world"
echo "Uppper case is $uppercase"
User interaction
We can read user input using read
command
#!/bin/bash
#User intercation using read
read name
echo "Entered name is $name"
Arithmatic operations
#!/bin/bash
#Using let command
x=10
y=2
let mul=$x*$y
echo "Multiplication is $mul"
#using brackets
echo "multiplication is $(($x * $y))"
Conditional statement
Comparison operators - Read more on conditional operators on below link tldp.org/LDP/abs/html/comparison-ops.html
#!/bin/bash
#If-else conditions
read -p "Enter your marks: " marks
if [[ $marks -gt 40 ]]
then
echo "You are PASS"
else
echo "You are FAIL"
fi
elif condition
#!/bin/bash
#elif condition
read –p "Enter your marks: " marks
if [[$marks –g6 80]]
then
echo "First Division"
elif [[$marks –ge 60]]
then
echo "Second Division"
else
echo "You are FAIL"
fi
Case condition
#!/bin/bash
echo "hey choose an option"
echo "a = To see current date"
echo "b = list all files in current directory"
read choice
case $choice in
a)date;;
b)ls;;
*) echo "Please provide valid input"
esac
Logical operators
&&
- if both conditions are true
condition1 && condition2
#!/bin/bash
read -p "What is your age?" age
read -p "what is your country?" country
if [[ $age -ge 18 && $country -eq "India" ]]
then
echo "You can vote"
else
echo "You cannot vote"
fi
||
- if any one condition is true
condition1 || condition2
#!/bin/bash
read -p "What is your age?" age
read -p "what is your country?" country
if [[ $age -ge 18 || $country -eq "India" ]]
then
echo "You can vote"
else
echo "You cannot vote"
fi
Loops
Loops are used when we want to execute commands repeatedly, we use loops.
#!/bin/bash
#For loop
for i in 1 2 3 4 5
do
echo "1) Number is $i"
done
#other ways to write for loop
for i in {1..5}
do
echo "2) Number is $i"
done
#C-Style loop
for (( i=1 ; i<=5 ; i++ ));
do
echo "3) Number is $i"
done
#For loop for reading array
s=("football" "cricket" "hockey")
for n in ${s[@]};
do
echo $n
done
While loop
#!/bin/bash
count=0
num=10
while [[ $count -le $num ]]
do
echo "Numbers are $count"
let count++
done
#!/bin/bash
#while loop to read content from a file
while read myVar
do
echo "Value from file $myVar"
done < names.txt
#while loop to read content from csv file
while IFS="," read id name age
do
echo "Id is $id "
echo "name is $name"
echo "age is $age"
done < names.csv
Until loop
#!/bin/bash
num=10
until [[ $num -eq 1 ]]
do
echo "Value of num is $num"
num=$((num-1))
done
echo "num value is $num"
echo "loop terminated"
Functions
Block of code which perform some task when it is called. We can reuse it as many times. We can pass arguments to method.
#!/bin/bash
function welcomeNote {
echo "----------------------------------------"
echo "Welcome"
echo "----------------------------------------"
}
#To call a function
welcomeNote
welcomeNote
welcomeNote
welcomeNote
#function with braces
welcomeNote() {
echo "----------------------------------------"
echo "Welcome in brace function"
echo "----------------------------------------"
}
#To call a function
welcomeNote
welcomeNote
welcomeNote
welcomeNote
Arguments in function
#!/bin/bash
addition() {
let num1=$1
let num2=$2
let sum = $num1+$num2
echo "sum of $num1 and $num2 is $sum"
}
#to call a function
addition 10 20
#To get no. of arguments : $#
#To display all arguments : $@
#To use or display arguments : $1 ,$2
Other useful concepts
break
– to stop loop
#!/bin/bash
no=6
for i in 1 2 3 4 5 6 7 8 9
do
#break loop if no. not found
if [[ $no -eq $i ]]
then
echo "$no is found!!"
break
fi
echo "Number is $i"
done
continue
– stop current iteration and stop next iteration
#!/bin/bash
#find even/odd number
for i in 1 2 3 4 5 6 7 8 9 10
do
let r=$i%2
if [[ $r -eq 0 ]]
then
continue
fi
echo "odd no. is $i"
done
exit
– stop script at a point
#!/bin/bash
if [[ $# -eq 0 ]]
then
echo "Please provide atleast one argument"
exit 1
fi
addition() {
let num1=$1
let num2=$2
let sum=$num1 + $num2
echo "sum of $num1 and $num2 is $sum"
}
#to call a function
addition 10 20
exit
status $?
- gives status of previous command
#!/bin/bash
read -p "Which site you want to check? " site
ping -c 1 $site
sleep 5s
if [[ $? -eq 0 ]]
then
echo "Successfully connected to site"
else
echo "Unable to connect site"
fi
basename
– strip directory info and give file name
basename filepath
dirname
– strip file name and gives directory name
dirname filepath
realpath
– gives full file path
realpath filename
RANDOM – A random interger between 0 to 32767 is generated.
#!/bin/bash
NO=$(($RANDOM % 6 + 1))
echo "Number is $NO"
UID
– user id of user logged in
#!/bin/bash
if [[ $UID -eq 0 ]]
then
echo "User is root"
else
echo "User is not root"
fi
Redirection
/dev/null
– When we don’t want to redirect output of command to any file or command terminal
#!/bin/bash
read -p "Which site you want to check?" site
ping -c 1 $site &> /dev/null
sleep 5s
if [[ $? -eq 0 ]]
then
echo "Successfully connected to site"
else
echo "Unable to connect site"
fi
Logger
If we want to maintain the logging for script, we can use logger in script
We can find logs under
/var/log/messages
#!/bin/bash
#example of logging
logger "This is logger from ${0}"
Debugging script
provide all step ran in script
#!/bin/bash
set -x
if [[ $UID -eq 0 ]]
then
echo "User is root"
else
echo "User is not root"
fi
if we want to exit from script when it fails
#!/bin/bash
set -e
pwd
date
cd /root
hostname
Running script in background
nohup ./until.sh
Automate script
at
– for scheduling at one time
at 2:59 AM give script path
atq
– to check schedule job
atrm <id>
– to remove the schedule
crontab
– The cron utility runs based on commands specified in a cron table (crontab). Each user, including root, can have a cron file
crontab –l
– to list existing scheduled scripts
crontab –e
– to add/edit a cron
16 03 * cd /home/scripts && ./script name
#crontab -e
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# backup using the rsbu program to the internal 4TB HDD and then 4TB external
01 01 * * * /usr/local/bin/rsbu -vbd1 ; /usr/local/bin/rsbu -vbd2
# Set the hardware clock to keep it in sync with the more accurate system clock
03 05 * * * /sbin/hwclock --systohc
# Perform monthly updates on the first of the month
# 25 04 1 * * /usr/bin/dnf -y update
crontab -e
#type this in opened editor for crontab
19 17 * * * cd /home/ec2-user && ./uid.sh