0% found this document useful (0 votes)
9 views14 pages

Shell Scripting Basics and Examples

The document provides an overview of shell scripting concepts including string comparison, logical operators, file checks, loops, and functions. It also includes assignments that require writing scripts for securing a system, cleaning and compiling code files, and calculating factorials. Each section outlines specific commands and syntax used in shell scripting.

Uploaded by

Ohm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Shell Scripting Basics and Examples

The document provides an overview of shell scripting concepts including string comparison, logical operators, file checks, loops, and functions. It also includes assignments that require writing scripts for securing a system, cleaning and compiling code files, and calculating factorials. Each section outlines specific commands and syntax used in shell scripting.

Uploaded by

Ohm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Shell Scripting

String Comparison

To check whether two strings are equal
–test “$str1” = “$str2” Space required
–test “$str1” == “$str2” (bash & korn shell)

To check whether two strings are not equal
–test “$str1” != “$str2”

To check whether a variable is assigned and not null
–test “$str”
–test -n “$str”

To check whether a variable is null
–test -z “$str”
15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 2
Logical Operator

Logical AND
–-a

–test “$str1” = “$str2” -a “$str2” = “$str3”



Logical OR:
–-o
–test “$str1” = “$str2” -o “$str2” = “$str3”

Logical NOT:
–!

–test ! -z “$str”

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 3
File Check

[ -f filename ] : Check if exists.

[ -x filename ] : Check if executable.

[ -w filename ] : Check if writable.

[ -r filename ] : Check if readable.

[ -d fname ] : Check if directory.

[ -s fname ] : Check if size > 0.

[ -e fname ] : Check if file exists.

[ f1 -nt f2 ] : Check if f1 is newer than f2

[ f1 -ot f2 ] : Check if f1 is older than f2
15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 4
case

case <expression> in
<pattern1a> | <pattern1b> | ...) <command1> ;;
<pattern1a> | <pattern1b> | ...) <command2> ;;
:
*) <default command>
esac

Write a script to take the rank of a student and print “First” or
“Second” or “Third” based on the rank being 1 or 2 or 3. Otherwise
show “You are not a topper!”

Write a script to take user choice Y or N in either case and show
“Continued” if it is Y or y, “Stopped” if it is N or n. Otherwise show
“Invalid Input!”,
15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 5
Integer Handling

let command

$ not required

let a=12 b=8 “c=2” “d = 3”
let z=a-b
let z++
echo $z

let can be replaced with $(( expression ))

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 6
Array

0 based index.

A=(1 2 3 4 5)

To access one element in array – A[i]

To display an element in array – echo ${A[i]}

To display all the elements of an array – echo ${A[@]}

To display the length of array – echo ${#A[@]}

To find the length of a string – echo ${#<String>}

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 7
While Loop

while [ condition ]
do
commands
done

Infinite Loop
–while true;
do
echo “Infinite loop”
done

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 8
Until Loop

Complement of While loop.

The loop continues till a condition remains false
–i.e. until the condition becomes true

Until [ condition ]
do
commands
done

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 9
For Loop

Does not test condition.

Works with list

for <variable> in <list>
do
commands
done

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 10
Function

<function-name> ( ) {
<function definition>
}

Formal parameters are not required, passed arguments are
accessed by $1, $2 etc.

Function is called as <function-name> <parameters, if any>

Return value of a function is obtained from exit status.

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 11
Assignment 1 :: Securing System
We want to make our system secure. So, we want all the
executable files of a specified folder to be made executable only
for the owner. So that nobody else can harm the system by
executing some vulnerable program.

Write a script that will take a folder path as command line input. It
will then check if the folder path is a valid one or not. If the folder
path is not valid, it will show an appropriate message to the user
and exit with status 1. Otherwise, it will then change the
permission of only the executable files of that folder such that only
the owner can read, write and execute those executable files, rest
can only read.
15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 12
Assignment 2 :: Universal Clean Compiler
We being the passionate coders, we write code here and there in
our systems. All the folders are now filled up with too many C, C++
and Java code files along with other files. We want to clean the
specified folders in a systematic way and compile those code files
so that they become ready for execution.

Write a script that will take a folder path as command line input. It
will then check if the folder path is a valid one or not. If the folder
path is not valid, it will show an appropriate message to the user
and exit with status 1. Otherwise, it will try to find out all the c, cpp
and java files in that folder. Then create three folders named C,
Cpp and Java
15/11/2025 within
Preetam that folder.
K Sur, Assistant Professor,And move
Computer Sciencethose filesGCETTS
& Enginnering, accordingly 13
Assignment 3 :: Factorial
Write a script that will take an integer from the user and pass it to
a user defined function which will then compute the factorial of the
integer and return the factorial value. Your script should then print
the factorial value.

15/11/2025 Preetam K Sur, Assistant Professor, Computer Science & Enginnering, GCETTS 14

You might also like