Unit-3
Unit-3
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.
• The username
HOME
EDITOR
UID
• User’s unique ID
TERM
SHELL
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
+ (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
/ (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
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
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.
Output Redirection
• 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
• This would attach the file with the email, and it would be sent to the
recipient.
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
Top
• This utility tells the user about all the running processes on the
Linux machine.
Unit-3: Shell Programming
S ‘R’ = running S R
‘S’ = sleeping
‘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
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
• If there is some process already running on the system, then you can
‘Renice’ its value using syntax.
• 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.