Scripting Symphony: A Shell Script Cheat Sheet

Dhanush V
10 min readDec 11, 2023

--

In my previous post I had explained about Shell scripting —

In this blog let’s learn how to create shell scripts.

01_basic.sh

echo "Hello Guys!"

Output:-

Hello Guys!
02_comments.sh 

echo "Checking Comments"

#This is a single line comment

#comment
#This
#is
#multi-line
#comment

Output:-

echo "Checking Comments"

#This is a single line comment

<<com
This
is
multi-line
com
03_vardemo.sh

#Script to show how to use variables

a=10
name="Dhanush"
age=22

echo "My name is $name and age is $age"

name="Dhanu"
echo "My name is $name"

#Variable to store the output of a command
HOSTNAME=$(hostname)
echo "Name of this machine is $HOSTNAME"

Output:-

My name is Dhanush and age is 22
My name is Dhanu
Name of this machine is modus
04_constant_var.sh

#Constant variable
readonly COLLEGE="VTU"

echo "My college name is $COLLEGE"

Output:-

My college name is VTU
05_ui.sh

read -p "What is your name? " name
echo "Your name is $name"

Output:-

What is your name? Dhanush
Your name is Dhanush
06_strings.sh 

myVar="Hey Buddy, how are you?"

myVarLength=${#myVar}
echo "Length of the myVar is $myVarLength"

echo "Upper case is ${myVar^^}"

echo "Lower case is ${myVar,,}"

#To replace a string
newVar=${myVar/Buddy/1421}
echo "New Var is ---- $newVar"

#To slice a string
echo "After slice ${myVar:4:5}"

Output:-

Length of the myVar is 23
Upper case is HEY BUDDY, HOW ARE YOU?
Lower case is hey buddy, how are you?
New Var is ---- Hey 1421, how are you?
After slice Buddy
07_arithmetic.ksh

#Maths Calculation
x=10
y=2

let sum=$x+$y
echo "$sum"

echo "Subtraction is $(($x-$y))"

echo "Multiplication is $(($x*$y))"

Output:-

12
Subtraction is 8
Multiplication is 20
08_array.sh

#Array

myArray=( 0 1 20.5 Hello "Hello Guys" )
echo "All the values in the array are ${myArray[*]}"

echo "Value of 3rd index is ${myArray[3]}"

#How to find no. of values in an array
echo "Length of an array is ${#myArray[*]}"

#Updating our array with new values
myArray+=( New 30 40 )

echo "Values of new array are ${myArray[*]}"

Output:-

All the values in the array are 0 1 20.5 Hello
Value of 3rd index is Hello Hello Guys
Length of an array is 4
Values of new array are 0 1 20.5 Hello New 30 40
09_if_else.sh

read -p "Enter your marks:" marks

if [ $marks -gt 40 ]
then
echo "You have Passed"
else
echo "You have Failed!"
fi

Output:-

Enter your marks:80
You have Passed

Enter your marks:30
You have Failed!
10_elif.sh

read -p "Enter your marks: " marks

if [ $marks -ge 80 ]
then
echo "Distinction"
elif [ $marks -ge 60 ]
then
echo "First Class"
elif [ $marks -ge 40 ]
then
echo "Second Class"
else
echo "You have Failed!"
fi

Output:-

Enter your marks: 80
Distinction

Enter your marks: 60
First Class

Enter your marks: 40
Second Class

Enter your marks: 20
You have Failed!
11_loops.sh

#Shorter and preferred version

for i in {2..20..2}
do
echo "Table of 2 : $i"
done

Output:-

Table of 2 : 2
Table of 2 : 4
Table of 2 : 6
Table of 2 : 8
Table of 2 : 10
Table of 2 : 12
Table of 2 : 14
Table of 2 : 16
Table of 2 : 18
Table of 2 : 20

#Lengthy version

for i in 1 2 3 4 5 6 7 8 9 10
do
echo "Number is $i"
done

Output:-

Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10

#Shorter version

for i in {2..20..2}
do
echo "Table of 2 : $i"
done

Output:-

Table of 2 : 2
Table of 2 : 4
Table of 2 : 6
Table of 2 : 8
Table of 2 : 10
Table of 2 : 12
Table of 2 : 14
Table of 2 : 16
Table of 2 : 18
Table of 2 : 20

#Normal version

for (( i=10; i>=1; i-- ))
do
echo "Number is $i"
done

Output:-

Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10
12_for_file.sh (read names for .txt file)

#Getting values from a file names.txt

FILE="/users/mod1421/1421/names.txt"

for name in $(cat $FILE)
do
echo "Name is $name"
done

Output:-

Name is ChristopherNolan
Name is Yash
Name is DulquerSalmaan
Name is PrithvirajSukumaran
Name is ShahrukhKhan
13_while.sh

count=0
num=10

while [[ $count -le $num ]]
do
echo "Value of count var is $count"
let count++
done

Output:-

Value of count var is 0
Value of count var is 1
Value of count var is 2
Value of count var is 3
Value of count var is 4
Value of count var is 5
Value of count var is 6
Value of count var is 7
Value of count var is 8
Value of count var is 9
Value of count var is 10
14_for_with_array.sh

myArray=(1 2 3 HELLO Hi )

length=${#myArray[*]}

for(( i=0;i<length;i++ ))
do
echo "Value of array is ${myArray[$i]}"
done

Output:-

Value of array is 1
Value of array is 2
Value of array is 3
Value of array is HELLO
Value of array is Hi
15_while_with_file.sh

while IFS="," read id name age
do
echo "Id is $id"
echo "Name is $name"
echo "Age is $age"
done < test.csv

Output:-

Id is id
Name is name
Age is age
Id is 01
Name is dhanush
Age is 22
Id is 02
Name is sonu
Age is 24
Id is 03
Name is bunty
Age is 24
16_case.sh

echo "Choose an option"
echo "a to print date"
echo "b to print list of scripts"
echo "c to check the current location"

read choice

case $choice in
a)
echo "Today's date is:"
date
echo "Ending..."
;;
b)ls;;
c)pwd;;
*)echo "Please choose a valid option"
esac

Output:-

Choose an option
a to print date
b to print list of scripts
c to check the current location
a
Today's date is:
Fri Dec 8 18:35:29 GMT 2023
Ending...

Choose an option
a to print date
b to print list of scripts
c to check the current location
b
01_basic.ksh 06_array.ksh 11_case.ksh 16_while.ksh 21_function_demo.ksh 26_continue.sh
02_comments.ksh 07_strings.ksh 12_logical.ksh 17_until.ksh 22_function_with_arg.ksh 27_redirect.sh
03_vardemo.ksh 08_arithmetic.ksh 13_ternary.ksh 18_infinite.ksh 23_args.sh names.txt
04_constant_var.ksh 09_if_else.ksh 14_loops.ksh 19_for_with_array.ksh 24_shift_args.sh redirect.log
05_ui.ksh 10_elif.ksh 15_for_file.ksh 20_while_with_file.ksh 25_break.sh test.csv
Choose an option
a to print date
b to print list of scripts
c to check the current location
c

Choose an option
a to print date
b to print list of scripts
c to check the current location
d
Please choose a valid option
17_logical.sh

#AND OPERATOR

read -p "What is your age? " age
read -p "Your country: " country
if [[ $age -ge 18 ]] && [[ $country -eq "India" ]]
then
echo "You can vote"
else
echo "You can't vote"
fi

Output:-

What is your age? 22
Your country: India
You can vote

OR

What is your age? 16
Your country: USA
You can't vote
18_ternary.sh

#cond1 && cond2 || cond3

read -p "Enter your age:" age
[[ $age -ge 18 ]] && echo "Adult" || echo "Minor"

Output:-

Enter your age:22
Adult

OR

Enter your age:16
Minor
19_until.sh  

a=0

until [ $a -eq 9 ]
do
echo "Value of a is $a"
let a++
done

Output:-

Value of a is 0
Value of a is 1
Value of a is 2
Value of a is 3
Value of a is 4
Value of a is 5
Value of a is 6
Value of a is 7
Value of a is 8
20_infinite.sh (infinite loop with interval)

#Infinite loop

for (( ;; ))
do
echo "Hi buddy"
sleep 2
done

Output:-

Hi buddy
Hi buddy
Hi buddy
Hi buddy
21_break.sh

#Example of break in a loop
#We just have to confirm if a given no. is presnt or not

no=6

for i in 1 2 3 4 5 6 7 8 9 10
do
if [ $no -eq 6 ]
then
echo "$no is found!!!"
break
fi
echo "Number is $i"
done

Output:-

6 is found!!!
22_continue.sh

#Example of continue in a loop
#To print odd numbers

for i in 1 2 3 4 5 6 7 8 9 10
do
r=$((i%2))
if [ $r -q 0 ]
then
continue
fi
echo "Odd numbers is $i"
done

Output:-

Odd numbers is 1
Odd numbers is 3
Odd numbers is 5
Odd numbers is 7
Odd numbers is 9
23_function_demo.sh

#To create function
welcomenote () {
echo "------------------------"
echo "Welcome"
echo "------------------------"
}

#To call function
welcomenote
welcomenote

Output:-
------------------------
Welcome
------------------------
------------------------
Welcome
------------------------
24_function_with_arg.sh

welcomenote () {
echo "-----------"
echo "Welcome $1"
echo "Age is $2"
echo "-----------"
}

welcomenote Dhanush 22
welcomenote Chandra 24
welcomenote Sonu 24

Output:-
-----------
Welcome Dhanush
Age is 22
-----------
-----------
Welcome Sonu
Age is 24
-----------
-----------
Welcome Bunty
Age is 24
-----------
25_args.sh 

#To access the arguments

if [ $# -eq 0 ]
then
echo "Please enter an argument"
exit 1
fi

echo "First argument is $1"
echo "Second argument is $2"

echo "All the arguments are - $@"
echo "Number of arguments are - $#"

Output:-

./25_args.sh dhanush 1421 is empid

Please enter an argument

OR

First argument is dhanush
Second argument is 1421
All the arguments are - dhanush 1421 is empid sample.txt
Number of arguments are - 4
26_shift_args.sh

#To create a user, provide username and description

echo "Creating user"
echo "Username is $1"

shift
echo "Description is $@"

Output:-

Creating user
Username is Dhanush
Description is 1421 is empid
27_redirect.sh

echo "List of all files in my directory-1421 are: $(ls -lt)" > redirect.log

Output:- (vi redirect.log)

List of all files in my directory-1421 are: total 240
-rw-r--r-- 1 mod1421 dba 2176 Dec 08 15:37 redirect.log
-rwxrwxr-x 1 mod1421 dba 77 Dec 08 15:35 27_redirect.sh
-rw-rw-r-- 1 mod1421 dba 160 Dec 08 14:59 26_continue.sh
-rw-rw-r-- 1 mod1421 dba 204 Dec 08 14:56 25_break.sh
-rw-rw-r-- 1 mod1421 dba 130 Dec 08 14:53 24_shift_args.sh
-rw-rw-r-- 1 mod1421 dba 296 Dec 08 14:48 23_args.sh
-rwxrwxr-x 1 mod1421 dba 285 Dec 08 13:03 11_case.ksh
-rwxrwxr-x 1 mod1421 dba 131 Dec 08 12:22 09_if_else.ksh
-rwxrwxr-x 1 mod1421 dba 265 Dec 08 11:18 22_function_with_arg.ksh
-rwxrwxr-x 1 mod1421 dba 116 Dec 08 11:08 16_while.ksh
-rwxrwxr-x 1 mod1421 dba 162 Dec 08 11:05 21_function_demo.ksh
-rwxrwxr-x 1 mod1421 dba 125 Dec 08 10:48 20_while_with_file.ksh
-rw-rw-r-- 1 mod1421 dba 103 Dec 08 10:45 test.csv
-rwxrwxr-x 1 mod1421 dba 76 Dec 07 19:02 18_infinite.ksh
-rwxrwxr-x 1 mod1421 dba 336 Dec 07 18:58 06_array.ksh
-rwxrwxr-x 1 mod1421 dba 138 Dec 07 18:34 19_for_with_array.ksh
-rwxrwxr-x 1 mod1421 dba 80 Dec 07 18:16 17_until.ksh
-rwxrwxr-x 1 mod1421 dba 145 Dec 07 17:23 15_for_file.ksh
-rw-rw-r-- 1 mod1421 dba 71 Dec 07 13:22 names.txt
-rwxrwxr-x 1 mod1421 dba 74 Dec 07 13:15 14_loops.ksh
-rwxrwxr-x 1 mod1421 dba 120 Dec 07 12:58 13_ternary.ksh
-rwxrwxr-x 1 mod1421 dba 207 Dec 07 12:53 12_logical.ksh
-rwxrwxr-x 1 mod1421 dba 231 Dec 07 12:22 10_elif.ksh
-rwxrwxr-x 1 mod1421 dba 139 Dec 07 11:55 08_arithmetic.ksh
-rwxrwxr-x 1 mod1421 dba 74 Dec 07 11:45 05_ui.ksh
-rwxrwxr-x 1 mod1421 dba 309 Dec 07 11:37 07_strings.ksh
-rw-rw-r-- 1 mod1421 dba 110 Dec 07 11:01 02_comments.ksh
-rwxrwxr-x 1 mod1421 dba 32 Dec 07 10:48 01_basic.ksh
-rw-rw-r-- 1 mod1421 dba 91 Dec 06 12:10 04_constant_var.ksh
-rw-rw-r-- 1 mod1421 dba 263 Dec 06 12:07 03_vardemo.ksh
crontab -e

0 16 8 12 05 cd /users/mod1421/1421 && ./27_redirect.sh

Output:-

List of all files in my directory-1421 are: total 240
-rw-r--r-- 1 mod1421 dba 2176 Dec 08 15:37 redirect.log
-rwxrwxr-x 1 mod1421 dba 77 Dec 08 15:35 27_redirect.sh
-rw-rw-r-- 1 mod1421 dba 160 Dec 08 14:59 26_continue.sh
-rw-rw-r-- 1 mod1421 dba 204 Dec 08 14:56 25_break.sh
-rw-rw-r-- 1 mod1421 dba 130 Dec 08 14:53 24_shift_args.sh
-rw-rw-r-- 1 mod1421 dba 296 Dec 08 14:48 23_args.sh
-rwxrwxr-x 1 mod1421 dba 285 Dec 08 13:03 11_case.ksh
-rwxrwxr-x 1 mod1421 dba 131 Dec 08 12:22 09_if_else.ksh
-rwxrwxr-x 1 mod1421 dba 265 Dec 08 11:18 22_function_with_arg.ksh
-rwxrwxr-x 1 mod1421 dba 116 Dec 08 11:08 16_while.ksh
-rwxrwxr-x 1 mod1421 dba 162 Dec 08 11:05 21_function_demo.ksh
-rwxrwxr-x 1 mod1421 dba 125 Dec 08 10:48 20_while_with_file.ksh
-rw-rw-r-- 1 mod1421 dba 103 Dec 08 10:45 test.csv
-rwxrwxr-x 1 mod1421 dba 76 Dec 07 19:02 18_infinite.ksh
-rwxrwxr-x 1 mod1421 dba 336 Dec 07 18:58 06_array.ksh
-rwxrwxr-x 1 mod1421 dba 138 Dec 07 18:34 19_for_with_array.ksh
-rwxrwxr-x 1 mod1421 dba 80 Dec 07 18:16 17_until.ksh
-rwxrwxr-x 1 mod1421 dba 145 Dec 07 17:23 15_for_file.ksh
-rw-rw-r-- 1 mod1421 dba 71 Dec 07 13:22 names.txt
-rwxrwxr-x 1 mod1421 dba 74 Dec 07 13:15 14_loops.ksh
-rwxrwxr-x 1 mod1421 dba 120 Dec 07 12:58 13_ternary.ksh
-rwxrwxr-x 1 mod1421 dba 207 Dec 07 12:53 12_logical.ksh
-rwxrwxr-x 1 mod1421 dba 231 Dec 07 12:22 10_elif.ksh
-rwxrwxr-x 1 mod1421 dba 139 Dec 07 11:55 08_arithmetic.ksh
-rwxrwxr-x 1 mod1421 dba 74 Dec 07 11:45 05_ui.ksh
-rwxrwxr-x 1 mod1421 dba 309 Dec 07 11:37 07_strings.ksh
-rw-rw-r-- 1 mod1421 dba 110 Dec 07 11:01 02_comments.ksh
-rwxrwxr-x 1 mod1421 dba 32 Dec 07 10:48 01_basic.ksh
-rw-rw-r-- 1 mod1421 dba 91 Dec 06 12:10 04_constant_var.ksh
-rw-rw-r-- 1 mod1421 dba 263 Dec 06 12:07 03_vardemo.ksh

--

--