0% found this document useful (0 votes)
2 views26 pages

Unit-4 Shell Programming

The document provides an overview of shell programming in Linux, explaining the concept of a shell as an interface between the user and the operating system. It outlines different types of shells, including command line and graphical shells, and discusses various shell scripting elements, advantages, and disadvantages. Additionally, it covers shell variables, keywords, basic operators, and the syntax for assigning values to variables.

Uploaded by

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

Unit-4 Shell Programming

The document provides an overview of shell programming in Linux, explaining the concept of a shell as an interface between the user and the operating system. It outlines different types of shells, including command line and graphical shells, and discusses various shell scripting elements, advantages, and disadvantages. Additionally, it covers shell variables, keywords, basic operators, and the syntax for assigning values to variables.

Uploaded by

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

Shell Programming in Linux

What is a shell?
Technically speaking in UNIX language, it is an interface between the user and operating system
service. A shell provides users with an interface that not only accepts human-readable commands
but also executes them.

An operating system consists of many parts, but the 2 core parts are the kernel and shell, we
know that the kernel is like the atom of the operating system, the sole heart of the operating
system. The kernel works a mediator between the software and hardware.

The shell on the other hand is more like the right hand of the kernel, it takes input from you in
the form of commands, processes it, and then gives an output. The shell is retrieved via the
terminal that runs it. Shell also provides an interface through which a user works on programs
and scripts.

Types of shell
Shell is broadly classified into 2 categories:

1. Command line shell

The shell can be accessed by using a command-line interface, and we all know what a command
line is. It is nothing but the terminal in Linux – the lean, mean coding machine. The terminal is a
special program to type in the human-readable commands such as “cat”, “ls”, “cd”, “pwd”,
“mkdir”, and a thousand more!

When you execute the commands, the result is then displayed on the terminal to the user. The
terminal is powerful and allows users to store commands in a file and execute them together.
Linux is not the only operating system with a command line, even windows had the command
prompt and so doe mac OS

The command line is a boon for developers and administrators, but it may not be everyone’s cup
of coffee as memorizing so many command sand and an even longer list of options is not an easy
task. That is why there are 2 options for the shell so that users can use what they prefer most.

2. Graphical shell

The graphical shell is nothing but the GUI – Graphical interface design. The graphical shell
provides an interface for manipulating programs based on the graphical user interface. The GUI
allows users to do bath tasks that the terminal can and can not do.
For example, you can do tasks like allowing for operations such as opening, closing, moving,
and resizing windows, as well as switching focus between windows and more which the terminal
can’t. The main advantage of the GUI is that users need not type commands for every small task.

There are various shells that are available for Linux based operating systems, let us look at some
of them:

1. BASH (Bourne Again SHell)

The bash shell is the most loved and commonly used shell in Linux-based operating systems. It
is a default login shell in Linux systems and in macOS, however, it can also be installed on
Mi9crosoft Windows.

The prompt for the bash shell is a dollar sign ($). The bash shell is a derivative of the Bourne
shell, other derivatives of the Bourne shell include POSIX and Korn.

2. KSH (Korn SHell)

The Korn shell is also derived from the Bourne shell but is the base for POSIX. It was written,
designed, and developed by David Korn in the early 1980s Some features of KSH are:

a. It provides object-oriented programing since ksh93t.


b. It has many mathematical and dynamic search functions.
c. it is very similar to C language and even has C-language-like expressions
d. You get a choice of 3 command line editing styes: vi, Emacs, and Gosling.
e. KSH includes reference variables and hierarchically nested variables.
f. It has associative arrays and built-in floating-point arithmetic operators
g. the Korn shell provides process substitution and process redirection.

3. CSH (C shell)

The C shell was written, designed, and developed at the University of California by Bill Joy.
J8ust like bash has the prompt “$”, chs has the prompt “%”. The C shell was developed to be
very similar to the C programming language, it also includes useful programming features like
in-built support for arithmetic operations

The C shell feature ‘aliases’ and command history which was missing in different types of shells
in Linux like the Bourne shell. The complete pathname for the C shell is /bin/sch.

There are many other shells available like:

 ZSH (Z shell)

 Fish Shell

 POSIX
 TENEX

What is shell scripting?


We saw that shells are interactive, they accept commands as input from users and execute them.
But if we want to execute a bunch of commands again and again (like a loop), we have to type in
all commands each time in the terminal.

To avoid this, the shell can also take in the commands as the input from a file, where we can
compile all of these commands and execute them. These files are called Shell programs or shell
scripts and have the extension of “.sh”.

Shell programs, just like any other programming language in the world, have a specific syntax
and a set of rules. A shell script contains the following elements:

 Keywords: if, else, break continue, etc.

 Commands: cd, ls, echo, touch, cat, pwd, mkdir, etc.

 Loops: if…then…else, while…do, do…while, case, etc.

 Functions

Why use shell programming?


There are a gazillion reasons why you should use shell programming, here are a few:

1. Avoids repetitive work and automation


2. Helps in system monitoring
3. Used for routine backups
4. Adding new functionality to shell
5. Creating custom filters
7. Helps in automating various tasks
8. Changing time and date
9. Printing current working directory
10. Scheduling jobs

Advantages of shell programming


Again, if we start writing the advantages of the list advantages of shell programming, it may
never end, nonetheless here are 8 advantages of it:

 Easy to use
 Portable

 Qiuck start

 Interactive debugging

 Same syntax as that of command line

 Avoids repetitive work and automation

 Shell scripting is much quicker than writing commands in the terminal

 Simple up learn

Disadvantages of shell programming


They say that every good thing has its negatives, which also hold true for shell programming,
here are some of its disadvantages:

 Slow execution speed

 Not suited for large and complex tasks

 Design flaws within the language syntax

 Provides minimal data structure, unlike other scripting languages

 Prone to costly errors

Shell Variables

A shell variable is a character string in a shell that stores some value. It could be an integer,
filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored
in memory. We have a few rules that have to be followed while writing variables in the script
(which will be discussed in the article). Overall knowing the shell variable scripting leads us to
write strong and good shell scripts.

Variable Types
We can discuss three main types of variables:

1) Local Variable:
Variables which are specific to the current instance of shell. They are basically used within the
shell, but not available for the program or other shells that are started from within the current
shell.

For example:

`name=Jayesh`

In this case the local variable is (name) with the value of Jayesh. Local variables is temporary
storage of data within a shell script.

2) Environment Variable:

These variables are commonly used to configure the behavior script and programs that are run by
shell. Environment variables are only created once, after which they can be used by any user.

For example:

`export PATH=/usr/local/bin:$PATH` would add `/usr/local/bin` to the beginning of the shell’s


search path for executable programs.

3) Shell Variables:

Variables that are set by shell itself and help shell to work with functions correctly. It contains
both, which means it has both, some variables are Environment variable, and some are Local
Variables.

For example:

`$PWD` = Stores working directory

`$HOME` = Stores user’s home directory

`$SHELL` = Stores the path to the shell program that is being used.

Rules for variable definition


A variable name could contain any alphabet (a-z, A-Z), any digits (0-9), and an underscore ( _ ).
However, a variable name must start with an alphabet or underscore. It can never start with a
number. Following are some examples of valid and invalid variable names:

 Valid Variable Names


ABC
_AV_3
AV232

 Invalid variable names

2_AN
!ABD
$ABC
&QAID

Note: It must be noted that no other special character except underscore can be used in a variable
name because all other special characters have special meanings in Shell Scripting.

Defining Variables
Syntax

variable_name = <variable data>

Example

num="1"
name="Devil"

These kinds of variables are scalar variables as they could hold one value at a time.

1) Accessing variable
Variable data could be accessed by appending the variable name with ‘$’ as follows:

VAR_1="Devil"
VAR_2="OWL"

echo "$VAR_1$VAR_2"

Output:

DevilOWL
Example of Accessing variable

2) Unsetting Variables
The unset command directs a shell to delete a variable and its stored data from list of variables. It
can be used as follows:

var1="Devil"
var2=23
echo $var1 $var2

unset var1

echo $var1 $var2

Output:

DEVIL 23
23
Example of Unsetting Variables

Note: The unset command could not be used to unset read-only variables.

3) Read only Variables.


These variables are read only i.e., their values could not be modified later in the script. Following
is an example:

var1="Devil"
var2=23
readonly var1
echo $var1 $var2
var1=23
echo $var1 $var2

Output:
Devil 23
line 8: var1: readonly variable
Devil 23

Shell Keywords in Linux


Shell keywords are reserved words that have special meanings in shell scripting. These
keywords cannot be used as variable names because they are part of the shell's syntax.

1. List of Common Shell Keywords

Here are some commonly used shell keywords:

Keyword Description

if Starts a conditional statement

then Used with if to execute commands when the condition is true

else Specifies commands to run when the if condition is false

elif Stands for "else if" (used for multiple conditions)

fi Ends an if statement

for Starts a loop that iterates over a range or list

while Starts a loop that runs as long as a condition is true

do Starts the body of a for or while loop

done Ends a loop (for or while)

case Used for multi-way branching (like a switch statement in other languages)

esac Ends a case statement

function Defines a function

return Exits a function and optionally returns a value

break Exits a loop immediately


Keyword Description

continue Skips the current iteration of a loop and moves to the next one

exit Exits the script with a status code

export Makes a variable available to child processes

unset Removes a variable from the environment

readonly Makes a variable read-only (cannot be changed)

eval Evaluates a command inside a script

Basic Operators in Shell Scripting

There are 5 basic operators in bash/shell scripting:

 Arithmetic Operators

 Relational Operators

 Boolean Operators

 Bitwise Operators

 File Test Operators

1. Arithmetic Operators: These operators are used to perform normal


arithmetics/mathematical operations. There are 7 arithmetic operators:

 Addition (+): Binary operation used to add two operands.

 Subtraction (-): Binary operation used to subtract two operands.

 Multiplication (*): Binary operation used to multiply two operands.

 Division (/): Binary operation used to divide two operands.


 Modulus (%): Binary operation used to find remainder of two operands.

 Increment Operator (++): Unary operator used to increase the value of operand by one.

 Decrement Operator (- -): Unary operator used to decrease the value of a operand by
one

2. Relational Operators: Relational operators are those operators which define the relation
between two operands. They give either true or false depending upon the relation. They are of 6
types:

 ‘==’ Operator: Double equal to operator compares the two operands. Its returns true is
they are equal otherwise returns false.

 ‘!=’ Operator: Not Equal to operator return true if the two operands are not equal
otherwise it returns false.

 ‘<‘ Operator: Less than operator returns true if first operand is less than second operand
otherwise returns false.

 ‘<=’ Operator: Less than or equal to operator returns true if first operand is less than or
equal to second operand otherwise returns false

 ‘>’ Operator: Greater than operator return true if the first operand is greater than the
second operand otherwise return false.

 ‘>=’ Operator: Greater than or equal to operator returns true if first operand is greater
than or equal to second operand otherwise returns false

3. Logical Operators : They are also known as boolean operators. These are used to perform
logical operations. They are of 3 types:

 Logical AND (&&): This is a binary operator, which returns true if both the operands are
true otherwise returns false.

 Logical OR (||): This is a binary operator, which returns true if either of the operands is
true or if both the operands are true. It returns false only if both operands are false.

 Not Equal to (!): This is a unary operator which returns true if the operand is false and
returns false if the operand is true.

4. Bitwise Operators: A bitwise operator is an operator used to perform bitwise operations


on bit patterns. They are of 6 types:
 Bitwise And (&): Bitwise & operator performs binary AND operation bit by bit on the
operands.

 Bitwise OR (|): Bitwise | operator performs binary OR operation bit by bit on the
operands.

 Bitwise XOR (^): Bitwise ^ operator performs binary XOR operation bit by bit on the
operands.

 Bitwise complement (~): Bitwise ~ operator performs binary NOT operation bit by bit
on the operand.

 Left Shift (<<): This operator shifts the bits of the left operand to left by number of times
specified by right operand.

 Right Shift (>>): This operator shifts the bits of the left operand to right by number of
times specified by right operand.

5. File Test Operator: These operators are used to test a particular property of a file.
 -b operator: This operator check whether a file is a block special file or not. It returns
true if the file is a block special file otherwise false.

 -c operator: This operator checks whether a file is a character special file or not. It
returns true if it is a character special file otherwise false.

 -d operator: This operator checks if the given directory exists or not. If it exists then
operators returns true otherwise false.

 -e operator: This operator checks whether the given file exists or not. If it exits this
operator returns true otherwise false.

 -r operator: This operator checks whether the given file has read access or not. If it has
read access then it returns true otherwise false.

 -w operator: This operator check whether the given file has write access or not. If it has
write then it returns true otherwise false.

 -x operator: This operator check whether the given file has execute access or not. If it
has execute access then it returns true otherwise false.

 -s operator: This operator checks the size of the given file. If the size of given file is
greater than 0 then it returns true otherwise it is false.

Assigning Values to Variables in Linux (Shell Scripting)


In Linux shell scripting, variables store values like numbers, strings, or commands. Unlike other
programming languages, variables in the shell do not require a data type.

1. Assigning a Value to a Variable

Basic Syntax:

variable_name=value

Rules:

 No spaces around = (e.g., x = 5 is incorrect).

 Variable names must start with a letter or _ (underscore).

 Only letters, numbers, and _ are allowed.

Examples:

name="Alice" # Assign a string

age=25 # Assign a number

path="/home/user" # Assign a file path

echo $name # Output: Alice

echo $age # Output: 25

echo $path # Output: /home/user

2. Assigning Values from User Input

echo "Enter your name:"


read user_name

echo "Hello, $user_name!"

3. Assigning Command Output to a Variable

You can assign the output of a command using backticks () or $()`:

current_date=`date`

echo "Today is: $current_date"

# Alternative method:

current_time=$(date)

echo "Current time: $current_time"

Input and Output (I/O) in Shell Scripting

In Linux shell scripting, I/O (Input and Output) refers to reading input from the user, files, or
commands and writing output to the terminal, files, or other commands.

1. Standard Input, Output, and Error

Linux provides three standard streams:

 stdin (0) → Standard Input (Keyboard, file, or command)

 stdout (1) → Standard Output (Screen, file, or another command)

 stderr (2) → Standard Error (Error messages)


2. Display Output (echo and printf)

Using echo (Simple Output)

echo "Hello, World!"

 -n → Prevents a newline

 -e → Enables escape sequences (e.g., \n, \t)

echo -e "Line1\nLine2"

Using printf (Formatted Output)

printf "Hello, %s! You are %d years old.\n" "Alice" 25

 %s → String

 %d → Integer

 \n → Newline

3. Taking User Input (read Command)

Basic Input

echo "Enter your name:"

read name

echo "Hello, $name!"

Read Multiple Values

echo "Enter two numbers:"

read num1 num2

echo "You entered: $num1 and $num2"


Silent Input (e.g., Password)

read -s -p "Enter password: " password

echo -e "\nPassword saved."

4. Redirecting Output (>, >>)

Writing Output to a File

echo "Hello, World!" > output.txt # Overwrites file

echo "Appending text" >> output.txt # Appends to file

Redirecting Standard Error (2> and 2>>)

ls /nonexistent 2> error.log # Save error to a file

ls /nonexistent 2>> error.log # Append error to a file

5. Redirecting Input (<)

Read from a File

while read line; do

echo "Line: $line"

done < input.txt

6. Using Pipes (|)

Pipes (|) allow passing output from one command as input to another.

Example:
ls -l | grep ".txt"

Here:

 ls -l lists files

 grep ".txt" filters .txt files

Conditional Statements | Shell Script

Conditional Statements: There are total 5 conditional statements which can be used in bash
programming

1. if statement

2. if-else statement

3. if..elif..else..fi statement (Else If ladder)

4. if..then..else..if..then..fi..fi..(Nested if)

5. switch statement

Their description with syntax is as follows:

if statement

This block will process if specified condition is true.

Syntax:

if [ expression ]

then

statement
fi

if-else statement
If specified condition is not true in if part then else part will be execute.

Syntax

if [ expression ]

then

statement1

else

statement2

fi

if..elif..else..fi statement (Else If ladder)

To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1
is true then it executes statement 1 and 2, and this process continues. If none of the condition is
true then it processes else part.

Syntax

if [ expression1 ]

then

statement1

statement2

elif [ expression2 ]

then
statement3

statement4

else

statement5

fi

if..then..else..if..then..fi..fi..(Nested if)

Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. In the syntax, if expression1 is false then it processes else part, and again expression2
will be check.

Syntax:

if [ expression1 ]

then

statement1

statement2

else

if [ expression2 ]

then

statement3

fi
fi

switch statement

case statement works as a switch statement if specified value match with the pattern then it will
execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.

Syntax:

case in

Pattern 1) Statement 1;;

Pattern n) Statement n;;

esac

Example Programs

Example 1:
Implementing if statement

#Initializing two variables

a=10

b=20

#Check whether they are equal

if [ $a == $b ]

then

echo "a is equal to b"


fi

#Check whether they are not equal

if [ $a != $b ]

then

echo "a is not equal to b"

fi

Output

$bash -f main.sh

a is not equal to b

How to Create a Shell Script in linux

Shell is an interface of the operating system. It accepts commands from users and interprets them
to the operating system. If you want to run a bunch of commands together, you can do so by
creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking
a backup. You can list those commands and execute them all with just a single script. Let’s see
how you can create a shell script and run it on Linux.

Creating a Shell Script


Login to your Linux machine and open the terminal, navigate to the folder where you want to
store the shell script. Shell scripts end with the extension “.sh”. Let’s create our first shell script.
Type in

touch script.sh

Now, this script file is not executable by default, we have to give the executable permission to
this file. Type in

chmod +x script.sh
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.
Type in

nano script.sh

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

Save the changes, and run the shell script by typing in

./script.sh

Screenshot of above steps

You can see, it executed all the specified commands.

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, except the shebang line, which we will see later in this article. Let’s see
an example. A shell script is created with the following content.

# This is a comment
echo Testing comments in shell script

Variables in Shell Script


Yes, Shell scripts support the use of variables, and we need not define a variable’s type during its
declaration. There are two types of variables:

 System Defined variables

 User-Defined Variables.

System-defined variables, also called environment variables, are generally Capitalised. You can
view all the current environment variables using the printenv command. User-Defined variables
are set by the user, and they exist only during script execution. You can define a variable by
simply typing its name and assigning a value with = sign and access a variable by adding a $
before the variable name. Variables are demonstrated in the following example script.

# Accessing an Environment Variable

echo $USER

# Creating and accessing User defined Variable

variable_name="Geeksforgeeks"

echo $variable_name

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. Let’s see a few conditional statements.

If statement

It checks the condition, and if it is conditioned true, it executes the commands.

Syntax

if [ condition ]
then

#statements

fi

Let’s see an example.

x=10

y=11

if [ $x -ne $y ]

then

echo "Not equal"

fi

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

Let’s see an example

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.
Let’s see some of the loops.

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

Let’s see an example.

x=2

while [ $x -lt 6 ]

do

echo $x

x=`expr $x + 1`

done
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

Let’s see an example.

for var in 2 4 5 8

do

echo $var

done

You might also like