Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment 2- Shell Programming

Aim:
The aim of this laboratory is to introduce the shell script that offers the student
with an interface to include a sequence of commands need to employ
frequently for saving me.

Learning Objec ve:


Students will be able to learn the basics of shell scrip ng to use variables,
accept input from a user and perform tests and make decisions.

Lab Exercise(s):
1. Write a shell script to produce a mul plica on table.
echo “Enter the number”
read n
i=1
while [ $i -le 10 ]
do
let “x=$n * $i”
i=`expr $i +1`
echo $x
done
2. Write a shell script program to implement a small calculator.
echo “Enter 2 numbers”
read a
read b
echo “Enter your Choice”
echo “1. Add”
echo “2. Sub”
echo “3. Mul ply”
echo “4. Division”
read ch
case “$ch” in
“1”) echo “ `expr $a + $b` “
;;
“2”) echo “ `expr $a - $b` “
;;
“3”) echo “ `expr $a * $b` ”
;;
“4”) echo “ `expr $a / $b` “
;;
*) echo “wrong choice entered”
;;
esac
3. Write a shell script to display prime numbers up to the given limit.
read n
i=2
while(($i < $n))
do
ans =$((n%i))
if [ $ans -eq 0 ]
then
echo “$n is not prime number”
exit 0

done
echo “ $n is a prime number”

Viva Ques ons:


1. What do you understand by a shell?
Your interface to the opera ng system is called a shell. The shell is the
outermost layer of the opera ng system. Shells incorporate a
programming language to control processes and files, as well as to start
and control other programs.
2. How can you define a shell variable?
A shell variable is a character string in 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.

3. What is the alterna ve to if-else statements in bash?


‘case’ statement is used as an alterna ve to ‘is-else’ statement.

4. How can we get input in shell script?


To read the Bash user input, we use the built-in Bash command called
read. It takes input from the user and assigns it to the variable. It reads
only a single line from the Bash shell.

5. How set executable permission on a shell script file?


We can provide the executable permission by using , chmod+x
filename.sh. chmod (Change Mode)-Using chmod we can change the
access permissions to file system objects. +x -lt makes the file
executable.

You might also like