SHELL PROGRAMMING add two no, three no, etc
SHELLPROGRAMMING
EX.NO:2.a PRINT STRING
Aim:
To write a shell Program to print Haldia.
Algorithm:
Step1: Start
Step2: Print the string using echo command on
screen and stop the execution..
Programcode:
echo 'Haldia'
Sample
O/P:
Haldia
Result:
Thus the shell program to define variable x with value 10 and print it on screen is
executed and output is verified successfully.
EX.NO:2. bPRINT INITIALIZE VALUE
Aim:
To write
a shell Program to define variable x with value 15 and print it on screen.
Algorithm:
Step1: Define the
variable x=15.
Step2: Print the value of x on screen and stop the execution.
Programcode:
n=15
echo The value of
n is $n
Sample
O/P:
The value of n is 15
Result:
Thus the shell program to define variable x with value 15 and print it on screen is
executed and output is verified successfully.
VIVA-VOCE
QUESTIONS:
1. What is the
purpose of ECHO command?
2. What
is the use of man command in Linux?.
3. What is Shell Script?
EX.
N0: 2.c PRINT SUM OF TWO NUMBERS
Aim:
ToWrite a shell Program to Print sum
of two numbers.
Algorithm:
Step1: Define x=20 and y=10.
Step2:Calculate the sum of two variables and
store the value in variable z using arithmetic operations .
Step 5 : Print the value
of variable z and stop the execution.
Programcode:
x =20
y =10
z= `expr$x + $y`
echo $z
Sample
O/P:
30
Result:
Thus
the shell program to Print
sum of two numbers is
executed and output is verified successfully.
Ex. NO: 2.dPRINT SUM OF TWO
NUMBERS USING EXPR COMMAND
Aim:
ToWrite a shell Program to Print sum
of two numbers.
Algorithm:
Step1: Start
Step2: Calculate the sum of twovalues usingexpr command .
Step 5 : Print the result
and stop the execution.
Programcode:
expr 3 + 5
Sample
O/P:
8
Result:
Thus
the shell program toPrint
sum of two numbers is
executed and output is verified successfully.
VIVA-VOCE
QUESTIONS:
1. What is the
purpose of “expr” command?
2. What
is the use of man command in Linux?.
3. What is the
purpose of “echo” command?
EX.NO:2.ePRINT
MULTIPLICATION OF TWO NUMBERS USING EXPR COMMAND
Aim:
ToWrite a shell Program to Print s
multiplication of two numbers.
Algorithm:
Step1: Start
Step2:Calculate the sum of two values using expr command .
Step 5 : Print the
result and stop the execution.
Programcode:
expr 3 \* 2
Sample
O/P:
6
Result:
Thus
the shell program to Print sum of two multiplication is executed and output is verified
successfully.
EX.NO:2.fADDITION OF TWO NUMBERS USING LET COMMAND
Aim:
ToWrite a shell Program to Print sum
of two numbers.
Algorithm:
Step1: Start
Step2:Calculate the sum of two values using let command and stored the
value in x.
Step 5 : Print the value
of x and stop the execution.
Program Code:
let x=15+10
echo $x
Sample
O/P:
25
Result:
Thus
the shell program to to
Print sum of two numbers is
executed and output is verified successfully.
VIVA-VOCE
QUESTIONS:
1. What is the
purpose of “expr” command?
2. What
is the use of man command in Linux?.
3. What is the
purpose of “echo” command?
EX.NO:2.gADDITION OF THREE
NUMBERS
Aim:
ToWrite a shell Program to Print sum
of three numbers.
Algorithm:
Step1: Start
Step2:Calculate the sum of three values and stored the value in z.
Step 5 : Print the value
of z and stop the execution.
Programcode:
x=22 y=28 z=5
((z=x+y+z))
echo $z
Sample
O/P:
55
Result:
Thus the shell program to Print sum of two numbers is executed and output is verified successfully.
VIVA-VOCE QUESTIONS:
1. What is the purpose of “bc” command?
2. How to use pipe to assign a variable?
3. What is the
purpose of “echo” command?
4.. What is the use of man command in Linux?.
EX.NO:2.hPRINT SUM OF TWO NUMBERS USING “bc”COMMAND
Aim:
How
to use decimal numbers in shell script.
Algorithm:
Step1: Define a and b.
Step2: Calculate the sum of two variables
using bc command and store the value in variable c.
Step 5 : Print the value
of variable c .
Step: Stop
Programcode:
a=81.3
b=15.7
c=`echo $a + $b |
bc`
echo $c
Sample
O/P:
c=97
Result:
Thus
the shell program is executed and output is verified successfully
VIVA-VOCE
QUESTIONS:
1. What is the purpose of “bc” command?
2. How to use pipe to assign a variable?
3. What is the
purpose of “echo” command?
EX.NO:2.iPRINT DEFINE BUILT-IN SHELL VARIABLES
Aim:
To write
a shell Program to define Built-in shell variables.
Algorithm:
Step1: Start.
Step2: Represent the
total no of arguments.
Step3: Represent all
arguments.
Step4: Represent
arguments 1 through 3.
Step5: Represent the
command or script.
Step6: Print the result
and stop the execution..
Programcode:
echo "The
number of parameters are $#"
echo "The
parameters are $*"
echo "The
parameters are $1 $2 $3"
echo "The
shell scripts command is $0"
Sample
O/P:
The number of parameters are 3
The parameters are hello world earth
The parameters are hello world earth
The shell scripts command is ./file.sh
Result:
Thus
the shell program is executed and output is verified successfully.
VIVA-VOCE
QUESTIONS:
1. What is $# in Shell?
2. What does $* mean?
3. What is $0 in
Shell?
EX.NO:2.j PRINT YOUR NAME
Aim:
To Write a shell Program to Print
your name. Name should be taken at runtime from
keyword.
Algorithm:
Step1: Print Enter your
1st name and last name.
Step2: Name should be taken at runtime from keyword.
Step3: Print the name on screen and stop the
execution..
Programcode:
echo -n
"Enter your first name "
read f
echo -n
"Enter your last name "
read l
echo "Your
name is $f $l"
Sample I/P:
Enter your first
name
Rinku
Enter your last
name
Bhunia
Sample
O/P:
Your name is Rinku Bhunia
Result:
Thus
the shell program to
Print your name is
executed and output is verified successfully.
VIVA-VOCE
QUESTIONS:
1. What is the purpose of “bc” command?
2. what is “read “ command?
3. What is the
purpose of “echo -n” command?
EX.NO:2.k SHOW DATE
Aim:
To write a shell Program to show your
date on screen.
Algorithm:
Step1: Start
Step2: The output of
date can be formatted with a sequence of format control characters preceded by
a +sign.The format controls start with the % symbol and are substituted by
their values. The %y character will be replaced with the year,%m with month and
%d with the day of the month. store the value in variable m.
Step3:display the output
of m.
Step4: Stop
Program
code:
m=`date +%d/%m/%y`
echo "Current
system date is $m"
Program
code:
echo $(date
“+%d-%m-%y”)
Sample O/P:
Current system
date is 16/05/1990
Sample O/P:
16-05-1990
Result:
Thus the shell program to Print the
current date is executed and output is verified successful
VIVA-VOCE
QUESTIONS:
1. What is the purpose of “date” command?
2. What is the command to change date in Linux?
3. What is the
purpose of “echo” command?
EX.NO:2.l FOR LOOP THROUGH A LIST
Aim:
How
to write a BASH ‘for’ loop through a List.
Algorithm:
Step1: Start
Step2:In a BASH, “for”
loop all, the statements between do and done are performed once every item in
the list. Each time the loop iterates, the next value in the list is inserted
into the variable specified after the word for. In the below loop, the variable
is called x.
Step3: The echo
statement displays information to the screen.Therefore; this example takes the
numbers 1 through 5 and outputs them one by one to the screen.
Step4:Stop
Program
code:
for x in 1 2 3 4 5
do
echo "The value of x $x"
done
Sample O/P:
The value of x 1 2 3 4 5
Result:
Thus the shell program is executed and output is verified successful
VIVA-VOCE
QUESTIONS:
1. What is the syntax of “for” loop?
2. What is the syntax of do ,done and in?
3. What is the
purpose of “echo ” command?
Comments
Post a Comment