Assignment No: 08
Assignment No: 08
Assignment No: 08
Aim: To study, implement and practice shell programming: Shell programming, basics of
shell programming, various types of shell, shell programming in bash.
Theory:
Introduction to Shell Scripting:
Shell scripting or programming mostly consists of the features which today’s modern programming
languages offer. Shell scripting is nothing but series or sequence of UNIX commands written in a
plain text file. Instead of specifying one job/command at a time, in shell scripting we give a list of
UNIX commands like a to-do list in a file to execute it.
Example: $ a=10
In the above statement a=10, the 10 stored in „a‟ is not treated as a number, but as a string of
characters 1 and 0. The lifespan of a variable inside shell script is only until the end of execution.
Variables can be made unchangeable using readonly. For instance, if we want variable a value to
remain as 10 and not to be changed then we can achieve this using readonly.
Example:
$ a=10
$ readonly a
In a shell, we can easily execute multiple scripts i.e. one script can be called from the other.
What we have to do is, we need to mention the name of a script to be called when we want to
invoke it.
Example: In the below program/script upon executing the first two echo statements of script1,
shell script executes script2. Once after executing script2, the control comes back to script1
which executes a pwd command and then terminates.
echo command in linux is used to display line of text/string that are passed as an argument. This is a
built-in command that is mostly used in shell scripts and batch files to output status text to the
screen or a file.
The if...else...fi statement is one of the form of control statement that allows Shell to execute
statements in a controlled way and make the right choice.
Syntax
if [ expression ]
then
else
fi
The Shell expression is evaluated in the above syntax. If the resulting value is true,
given statement(s) are executed. If the expression is false, then no statement will be executed.
for statement
The for loop operate on lists of items. It repeats a set of commands for every item in a list.
Here var is the name of a variable and word1 to wordN are sequences of characters separated by
spaces (words). Each time the for loop executes, the value of the variable var is set to the next word
in the list of words, word1 to word N.
Syntax:
do
Statement to be executed
done
Practical:
1.)Write a shell script to ask your name, program name and enrollment number and print it
on the screen.
Input:
Output:
Output:
3.) Write a shell program to find largest of three numbers.
Code:
Output:
4.)Write a shell program to add, subtract and multiply 2 numbers passed as command line
arguments.
Code:
Output:
Output:
Conclusion:
After going through all the above shell scripting practical’s, mainly we understood that a shell is an
interface between a user and an operating system which interprets the command entered by a user
to the kernel or operating system. Due to this, shell plays a vital role in the operating system.