0% found this document useful (0 votes)
15 views13 pages

Shell Scripting

The document provides a comprehensive guide on shell scripting for UNIX-based operating systems, detailing the creation, execution, and components of shell scripts. It covers topics such as comments, variables, conditional statements, loops, positional arguments, and exit codes. Additionally, it explains the use of different shell interpreters and comparison operators essential for scripting.

Uploaded by

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

Shell Scripting

The document provides a comprehensive guide on shell scripting for UNIX-based operating systems, detailing the creation, execution, and components of shell scripts. It covers topics such as comments, variables, conditional statements, loops, positional arguments, and exit codes. Additionally, it explains the use of different shell interpreters and comparison operators essential for scripting.

Uploaded by

Anmol Malhotra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

NATIONAL FORENSIC SCIENCES

UNIVERSITY

OPERATING SYSTEM

Practical Assignment on SHELL SCRIPTING

Submitted by: ANMOL MALHOTRA


Enrolment No.: 012201000001002
Shell Script
A shell script is a text file that contains a sequence of commands for a UNIX-
based operating system. It is called a shell script because it combines a sequence
of commands, that would otherwise have to be typed into the keyboard one at a
time, into a single script. The shell is the operating system's command-line
interface (CLI) and interpreter for the set of commands that are used to
communicate with the system.

Shell scripts end with the extension “.sh”.

Create a Shell Script

Step 1: Type in touch script.sh to create a shell file.

Step 2: Type chmod +x script.sh to convert the file from read to executable form.

Now, we will add some commands to this shell script. Open this shell script with
any text editor of your choice (command-line-based or GUI-based) and add some
commands. We will use nano.

Step 3: Type in nano script.sh

Step 4: Add the following commands to test this shell script

• echo This is my first shell script


• touch testfile
• ls
• echo End of my shell script

Step 5: Save the changes, and run the shell script by typing in ./script.sh
Comments in the shell script

Any line which starts with “#” in the shell script is treated as a comment and is
ignored by the shell during execution.

Illustrations:

# This is a comment

It can be seen that the statement “This is a comment” is not executed as it is


ignored during execution.
Variables in Shell Script

Variables are supported by the shell scripts and are not required to be defined
during the declaration. There are two types of variables:
• System Defined variables
• User-Defined Variables.

System-defined variables are also known as environment variables and are


generally Capitalised. User-defined variables are set by the user, and they exist
only during script execution. It can be defined by typing its name and assigning
a value with a = sign and can be accessed by adding a $ before the variable name.
Variables are demonstrated in the following example script.

# Accessing an Environment Variable echo $USER defined Variable

# Creating and accessing User defined variable

variable_name = "Geeksforgeeks"

echo $variable_name
Defining the Shell Script interpreter

There are several shells available for Linux, including the bourne shell (sh), the
Korn shell (ksh), and the GNU Bourne-Again shell (bash). Scripts written for the
sh shell are called shell scripts and can be interpreted by the ksh shell and the
bash shell. ksh and Bash are better versions of the original shell with more
features than sh. Bash is the default shell for most Linux distributions, and scripts
written for the bash shell are called bash scripts.

You can specify which frame the script uses, even if the script runs in a different
frame type. To do this, add "#!" The absolute path to the specified frame follows
the top of the text file. To define bash as an interpreter, add the following line at
the top of your shell script:

#!/bin/bash

This line is called the shebang line.

Note: This will only work if bash is installed on your system.

Comparison Operators

Two variables in shell scripting can be compared using the following operators
during decision making statements.

• Integer comparison
Operator Description

-eq is equal to

-ne is not equal to

-gt is greater than

-ge is greater than or equal


to
-lt is less than

-le is less than or equal to

• String Comparison
Operator Description

== is equal to

!= is not equal to

\< is less than, in ASCII alphabetical order

\> is greater than, in ASCII alphabetical


order

Conditional statements
Conditional statements are used to execute a block of code only when certain conditions are
met.
Shell scripts support the use of conditional statements. We use comparison operators to check
the
conditions
1) If statement: It checks the condition, and if it is conditioned true, it executes the
commands.
Syntax:

if [ condition ]
then
#statements
fi
Illustration:

#!/bin/sh
x=10
y=11
if [ $x -ne $y ]
then
echo "Not equal"
fi
2) If-else statement: In an if-else statement, you can specify a set of commands to run if the
condition is not met.
Syntax

if [ condition ]
then
#set of statements if the condition is true
else
#set of statements if the condition is false
fi

Illustration:

#!/Syntaxbin/sh
x=10
y=10
if [ $x -ne $y ]
then
echo "Not equal"
else
echo "They are equal"
fi

Loops
Using loops, you can a set of commands over and over again, until a certain condition is met.
1) While loop: It starts running the specified commands if the condition is true and repeats
them until the condition is false.
Syntax:
while [ condition ]
do
#set of statements
done
Illustration:
#!/bin/sh
x=2
while [ $x -lt 6 ]
do
echo $x
x=`expr $x + 1`
done

2) For loop: In a for loop, the variable iterates over a list of values and ends when there are no
more values to iterate over.
Syntax:
for var in val1 val2 val3
do
#statements
done
Illustration:
#!/bin/sh
for var in 2 4 5 8
do
echo $var
done

Positional Arguments
Arguments and values that we pass to the shell script during execution of a script are position
arguments. You can access them by variables $0, $1, $2,$9. And then they're referenced by
$10, $11 and so on. $# stores the no of passed arguments and $0 stores the name of the script.
Illustration:
#!/bin/sh
echo "No of arguments is $#"
echo "Name of the script is $0"
echo "First argument is $1"
echo "Second argument is $2"
Storing the output of commands
You can store the output of commands inside a variable in a shell script. There are two ways:
Syntax:
#Syntax 1
var=$(a valid linux command)
#Syntax 2
var2=`a valid linux command`
Illustration:
#!/bin/sh
b=$(pwd)
c=`pwd`
echo $b
echo $c
d=$(ls /bin | grep bash)
echo $d
Exit Codes of shell commands
Whenever a command ends and returns the control to the parent process, it returns exit codes
between 0 and 255. Exit code 0 means the command was successful, and any other exit code
means, the command was unsuccessful. You can view the exit code after running any
command by accessing the #? Variable.
Exit codes of the command can also be set manually instead of letting the OS pick a random
number between 1 to 225 for failed execution.
Example
#!/bin/sh
read x
if [ $x -ne 10 ]
then
echo failed
exit 1
else
echo
passed exit 0
fi

You might also like