0% found this document useful (0 votes)
50 views

Unit-3

Uploaded by

sabhadiyaruchit9
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)
50 views

Unit-3

Uploaded by

sabhadiyaruchit9
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/ 19

Unit-3: Shell Programming

• Shell Scripting is an open-source computer program designed to be run


by the Unix/Linux shell.
• Shell Scripting is a program to write a series of commands for the shell to
execute.
• It can combine lengthy and repetitive sequences of commands into a
single and simple script that can be stored and executed anytime which,
reduces programming efforts.

3.1. Screen Editor (vi)


• The default editor that comes with the UNIX operating system is called vi
(visual editor).
• Using vi editor, we can edit an existing file or create a new file from
scratch.
• we can also use this editor to just read a text file.

Syntax:
vi filename
Modes of Operation in vi editor There are three modes of operation in vi:
Unit-3: Shell Programming
• Command Mode:
When vi starts up, it is in Command Mode.
This mode is where vi interprets any characters we type as commands
and thus does not display them in the window.
This mode allows us to move through a file, and to delete, copy, or paste
a piece of text.
To enter into Command Mode from any other mode, it requires pressing
the [Esc] key.
If we press [Esc] when we are already in Command Mode, then vi will
beep or flash the screen.
• Insert mode:
This mode enables you to insert text into the file.
Everything that’s typed in this mode is interpreted as input and finally, it
is put in the file. The vi always starts in command mode.
To enter text, you must be in insert mode. To come in insert mode, you
simply type i.
To get out of insert mode, press the Esc key, which will put you back into
command mode.
• Last Line Mode(Escape Mode):
Line Mode is invoked by typing a colon [:], while vi is in Command Mode.
The cursor will jump to the last line of the screen and vi will wait for a
command.
This mode enables you to perform tasks such as saving files, executing
commands.
Commands Description
vi filename Creates a new file if it already not exist, otherwise opens existing
file.
vi -R filename Opens an existing file in read only mode.
view filename Opens an existing file in read only mode.

Moving within a File(Navigation):


• k : Moves the cursor up one line.
• j : Moves the cursor down one line.
• h : Moves the cursor to the left one character position.
• l : Moves the cursor to the right one character position.
• 0 or | : Positions cursor at beginning of line.
• $ : Positions cursor at end of line.
• W : Positions cursor to the next word.
• B : Positions cursor to previous word.
• ( : Positions cursor to beginning of current sentence.
• ) : Positions cursor to beginning of next sentence.
• H : Move to top of screen.
• nH : Moves to nth line from the top of the screen.
• M : Move to middle of screen.
• L : Move to bottom of screen.
• nL : Moves to nth line from the bottom of the screen.
Unit-3: Shell Programming
Editing and inserting in Files(Entering and Replacing Text)
• i : Inserts text before current cursor location.
• I : Inserts text at beginning of current line.
• a : Inserts text after current cursor location.
• A : Inserts text at end of current line.
• o : Creates a new line for text entry below cursor location.
• O : Creates a new line for text entry above cursor location.
• r : Replace single character under the cursor with the next character typed.
• R : Replaces text from the cursor to right.
• s : Replaces single character under the cursor with any number of characters.
• S :Replaces entire line.
Deleting Characters:
• X Uppercase: Deletes the character before the cursor location.
• x Lowercase : Deletes the character at the cursor location.
• Dw : Deletes from the current cursor location to the next word.
• d^ : Deletes from current cursor position to the beginning of the line.
• d$ : Deletes from current cursor position to the end of the line.
• Dd : Deletes the line the cursor is on.
Copy and Past Commands:
• Yy : Copies the current line.
• 9yy : Yank current line and 9 lines below.
• p : Puts the copied text after the cursor.
• P : Puts the yanked text before the cursor.
Save and Exit Commands of the ex Mode
Need to press [Esc] key followed by the colon (:) before typing the following commands
• q : Quit
• q! : Quit without saving changes i.e. discard changes.
• r fileName : Read data from file called fileName.
• wq : Write and quit (save and exit).
• w fileName : Write to file called fileName (save as).
• w! fileName : Overwrite to file called fileName (save as forcefully).
• !cmd : Runs shell commands and returns to Command mode.
Searching and Replacing in (ex Mode):
:s/string
:%s/pattern/replace/

3.2. Environmental & user defined variables


Environment variables are dynamic values which affect the processes or programs
on a computer.
They exist in every operating system, but types may vary.
Environment variables can be created, edited, saved, and deleted and give
information about the system behaviour.
Unit-3: Shell Programming
PATH
• This variable contains a colon (:)-separated list of directories in which your
system looks for executable files.
• When you enter a command on terminal, the shell looks for the command in
different directories mentioned in the $PATH variable. If the command is found,
it executes. Otherwise, it returns with an error ‘command not found’.
USER

• The username

HOME

• Default path to the user’s home directory

EDITOR

• Path to the program which edits the content of files

UID

• User’s unique ID

TERM

• Default terminal emulator

SHELL

• Shell being used by the user

Accessing Variable values


echo $VARIABLE

Set New Environment Variables


VARIABLE_NAME= variable_value

Deleting Variables
unset variablename

‘env’ command
The ‘env’ command displays all the environment variables.
Unit-3: Shell Programming
Sr.No. Variable & Description

1
$0
The filename of the current script.

2
$n
These variables correspond to the arguments with which a script was
invoked. Here n is a positive decimal number corresponding to the
position of an argument (the first argument is $1, the second argument is
$2, and so on).

3
$#
The number of arguments supplied to a script.

4
$*
All the arguments are double quoted. If a script receives two arguments,
$* is equivalent to $1 $2.

5
$@
All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.

6
$?
The exit status of the last command executed.

7
$$
The process number of the current shell. For shell scripts, this is the
process ID under which they are executing.

8
$!
The process number of the last background command.
Unit-3: Shell Programming
3.3. Conditional Execution
IF/THEN
if [ condition ]
then
statements for when the condition is true
fi

MY_SHELL="zsh"

if [ "$MY_SHELL" = "zsh" ]
then
echo "You are the zsh shell user!"
fi

ELSE
if [ condition ]; then
statements for when the condition is true
else
statements for when the condition is false
fi

if [ -r "$1" ]; then
cat "$1"
else
echo "Error: $1 is not a readable file."
Fi

ELIF
OS=`uname -s`
if [ "$OS" = "FreeBSD" ]; then
echo "This Is FreeBSD"
elif [ "$OS" = "CYGWIN_NT-5.1" ]; then
echo "This is Cygwin"
elif [ "$OS" = "SunOS" ]; then
echo "This is Solaris"
elif [ "$OS" = "Darwin" ]; then
echo "This is Mac OSX"
elif [ "$OS" = "Linux" ]; then
echo "This is Linux"
else
echo "Failed to identify this OS"
fi
Unit-3: Shell Programming
case statement
case "$VAR" in
pattern_1)
# Some commands here.
;;
# Execution will stop when the double semicolon is reached
pattern_n)
# Some commands here.
;;
Esac

OS=`uname -s`

case "$OS" in
FreeBSD) echo "This is FreeBSD" ;;
CYGWIN_NT-5.1) echo "This is Cygwin" ;;
SunOS) echo "This is Solaris" ;;
Darwin) echo "This is Mac OSX" ;;
Linux) echo "This is Linux" ;;
*) echo "Failed to identify this OS" ;;
Esac

3.4. Arithmetic expression evaluation


Operator Description Example

+ (Addition) Adds values on either side of the operator `expr $a + $b` will give
30

- (Subtraction) Subtracts right hand operand from left `expr $a - $b` will give -
hand operand 10

* Multiplies values on either side of the `expr $a \* $b` will give


(Multiplication) operator 200

/ (Division) Divides left hand operand by right hand `expr $b / $a` will give
operand 2

% (Modulus) Divides left hand operand by right hand `expr $b % $a` will give
operand and returns remainder 0

= (Assignment) a = $b would assign


Assigns right operand in left operand
value of b into a

== (Equality) Compares two numbers, if both are same [ $a == $b ] would


then returns true. return false.
Unit-3: Shell Programming
!= (Not Equality) Compares two numbers, if both are [ $a != $b ] would
different then returns true. return true.

Integer Comparisons Function


-gt Greater-than
-lt Less-than
-ge Greater-than-or-equal-to
-le Less-than-or-equal-to
-eq Equal
-ne Not-equal
String Comparisons
-z Tests for empty string
= Equal strings
!= Not-equal strings
Logical Operations
-a Logical AND
-o Logical OR
! Logical NOT
File Tests
-f File exists and is a regular file
-s File is not empty
-r File is readable
-w File can be written to, modified
-x File is executable
-d Filename is a directory name
#!/bin/sh

a=10
b=20

val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"
Unit-3: Shell Programming
val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
echo "a is equal to b"
fi

if [ $a != $b ]
then
echo "a is not equal to b"
fi

3.5. Control Structure


1. while statement
2. for statement
3. until statement
To alter the flow of loop statements, two commands are used they are,
1. break
2. continue

1) until
The statements in the until loop are executed until the command in the
condition becomes true.
Syntax
until command
do
Statement(s) to be executed
done
a=5
until [ $a -lt 1 ] # a < 1
do
echo $a # print a
Unit-3: Shell Programming
a=`expr $a - 1` # a = a - 1
done

2) while
The statements in the while loop are executed until the command in the
condition becomes false.
Syntax
while command
do
Statement(s) to be executed
done
a=5
while [ $a -gt 0 ] # a > 0
do
echo $a # print a
a=`expr $a - 1` # a = a - 1
done

3) for
The for loop operates on a list of items. The statements in the for loop are
executed for all items in the list.
Syntax
for var in word1 word2 ... wordN
do
Statement(s) to be executed
done
for a in 5 4 3 2 1
do
echo $a # print a
done
Unit-3: Shell Programming

1.break
We use the break statement to terminate the loop. It executes the line above
break and ignores all the lines below break. It then starts execution from the
next line after the end of the loop.
for a in 2 5 3 1 9 8 0 7 6 4
do
if [ $a -eq 0 ] # a == 0
then
break
fi
echo $a
done

2.continue
We use the continue statement to terminate iteration(s) of the loop. It
executes the line above continue and ignores all the lines below continue. It
then starts execution from the start of the next iteration in the loop.
for a in 1 2 3 4 5 6 7 8 9 10
do
if [ `expr $a % 2` -eq 0 ] # a % 2 == 0
then
continue
fi
echo $a
done

3.6. Redirection
• Redirection is a feature in Linux such that when executing a command,
you can change the standard input/output devices.
• The basic workflow of any Linux command is that it takes an input and
give an output.

• The standard input (stdin) device is the keyboard.


Unit-3: Shell Programming
• The standard output (stdout) device is the screen.

Output Redirection

• The ‘>‘ symbol is used for output (STDOUT) redirection.


• Example:
ls -al > listings

Here the output of command ls -al is re-directed to file “listings” instead


of your screen.

• Note: Use the correct file name while redirecting command output to a
file. If there is an existing file with the same name, the redirected
command will delete the contents of that file and then it may be
overwritten.”

• If you do not want a file to be overwritten but want to add more content
to an existing file, then you should use ‘>>‘ operator.

• You can redirect standard output, to not just files, but also devices!
• $ cat music.mp3 > /dev/audio
The cat command reads the file music.mp3 and sends the output to
/dev/audio which is the audio device. If the sound configurations in your
PC are correct, this command will play the file music.mp3
Unit-3: Shell Programming

Input Redirection
3.8. Argument Processing & Shells interpretation

• The ‘<‘ symbol is used for input(STDIN) redirection


• Example:
The mail program in Linux can help you send emails from the Terminal.
You can type the contents of the email using the standard device
keyboard. But if you want to attach a File to email you can use the input
re-direction operator in the following format.
Mail -s "Subject" to-address < Filename

• This would attach the file with the email, and it would be sent to the
recipient.

3.7. Background process & priorities of process, Batch Process


• An instance of a program is called a Process. In simple terms, any
command that you give to your Linux machine starts a new process.
• Foreground Processes: They run on the screen and need input from the
user. For example, Office Programs.

• Background Processes: They run in the background and usually do not


need user input. For example, Antivirus.

Running a Background process


• If you start a foreground program/process from the terminal, then you
cannot work on the terminal, till the program is up and running.
• Particular, data-intensive tasks take lots of processing power and may
even take hours to complete. You do not want your terminal to be held up
for such a long time.
Unit-3: Shell Programming
• To avoid such a situation, you can run the program and send it to the
background so that terminal remains available to you.

Fg
• You can use the command “fg” to continue a program which was
stopped and bring it to the foreground.
• The simple syntax for this utility is:

fg jobname
• Example

1. Launch ‘banshee’ music player


2. Stop it with the ‘ctrl +z’ command
3. Continue it with the ‘fg’ utility.

Top
• This utility tells the user about all the running processes on the
Linux machine.
Unit-3: Shell Programming

Press ‘q’ on the keyboard to move out of the process display.

Field Description Example 1 Example 2


PID The process ID of each task 1525 961
User The username of task owner Home Root
Priority
PR 20 20
Can be 20(highest) or -20(lowest)
NI The nice value of a task 0 0
VIRT Virtual memory used (kb) 1775 75972
RES Physical memory used (kb) 100 51
SHR Shared memory used (kb) 28 7952
Status

There are five types:

‘D’ = uninterruptible sleep

S ‘R’ = running S R

‘S’ = sleeping

‘T’ = traced or stopped

‘Z’ = zombie
%CPU % of CPU time 1.7 1.0
%MEM Physical memory used 10 5.1
TIME+ Total CPU time 5:05.34 2:23.42
Command Command name Photoshop.exe Xorg

PS
• This command stands for ‘Process Status’. It is similar to the “Task
Manager” that pop-ups in a Windows Machine when we use
Cntrl+Alt+Del. This command is similar to ‘top’ command but the
information displayed is different.
• To check all the processes running under a user, use the command

Unit-3: Shell Programming
ps ux

• You can also check the process status of a single process, use the syntax –

ps PID

Kill
• This command terminates running processes on a Linux machine.
• To use these utilities, you need to know the PID (process id) of the process
you want to kill
• Syntax –

kill PID
• To find the PID of a process simply type

pidof Process name

NICE

• Linux can run a lot of processes at a time, which can slow down the speed
of some high priority processes and result in poor performance.
• To avoid this, you can tell your machine to prioritize processes as per your
requirements.
Unit-3: Shell Programming
• This priority is called Niceness in Linux, and it has a value between -20 to
19. The lower the Niceness index, the higher would be a priority given to
that task.
• The default value of all the processes is 0.
• To start a process with a niceness value other than the default value use
the following syntax

nice -n 'Nice value' process name

• If there is some process already running on the system, then you can
‘Renice’ its value using syntax.

renice 'nice value' -p 'PID'

• To change Niceness, you can use the ‘top’ command to determine the
PID (process id) and its nice value. Later use the renice command to
change the value.
• Example

DF
• This utility reports the free disk space(Hard Disk) on all the file systems.
Unit-3: Shell Programming

• If you want the above information in a readable format, then use the
command

'df -h'

Free
This command shows the free and used memory (RAM) on the Linux system.

You can use the arguments

• free -m to display output in MB


• free -g to display output in GB

3.8. Argument Processing & Shells interpretation


Unit-3: Shell Programming
• The shell command interpreter is the command line interface between the
user and the operating system. It is what you will be presented with once
you have successfully logged into the system.
• The shell allows you to enter commands that you would like to run, and
also allows you to manage the jobs once they are running. The shell also
enables you to make modifications to your requested commands.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy