0% found this document useful (0 votes)
28 views5 pages

Osy Amir PR - 11

The document provides examples of shell scripts using loops and conditional statements to perform various tasks. The first example uses a for loop to print the table of a given number. The second example uses nested for loops to print a pattern of asterisks. Additional examples demonstrate using a while loop, case statement, and other shell scripting concepts.

Uploaded by

manishkadam3525
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Osy Amir PR - 11

The document provides examples of shell scripts using loops and conditional statements to perform various tasks. The first example uses a for loop to print the table of a given number. The second example uses nested for loops to print a pattern of asterisks. Additional examples demonstrate using a while loop, case statement, and other shell scripting concepts.

Uploaded by

manishkadam3525
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1) Execute shell script by considering example like printing table of given number by FOR

loop
Ans.
read -p 'Enter no = ' no
echo "Table of $no"
for (( i=1; i<=10; i++ ))
do
echo " "$(( no * i ))
done
Output :

Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 abc_1.sh


Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./abc_1.sh
Enter no = 60
Table of 60
60
120
180
240
300
360
420
480
540
600
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

2) Execute shell script by considering example like printing


following output by FOR loop
******
****
**
*
Ans.
for (( i=1;i<8;i=i+2 ))
do
for (( j=1;j<7;j++ ))
do
if [ $i -le $j ]
then
echo -n "* "
else
echo -n " "
if [ $i -eq 7 -a $j -eq 5 ]
then
echo "*"
fi
fi
done
echo ""
done
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 abc_2.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$./abc_2.sh
******
****
**
*

Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

Exercise
1) Give output of the following a.
a.
NUMBERS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q='expr $NUM % 2
if [ SQ -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done

Error :-
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 oddeven.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$./oddeven.sh
./oddeven.sh: line 5: unexpected EOF while looking for matching `''
./oddeven.sh: line 13: syntax error: unexpected end of file
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

b. #!/bin/sh
a=0
while [ $a -lt 10 |
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=$(( $a + 1 ))
done
Output:
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 lessthan.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./lessthan.sh
0
1
2
3
4
5
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

3) Write a shell script to display Fibonacci series for n numbers.


Ans.
read -p 'Enter no for fibonacci = ' no
x=0
y=1
no1=$no
while [ $no -gt 0 ]
do
z=$(( x + y ))
#echo $z
x=$y
y=$z
no=$(( no - 1 ))
done
echo "Fibonacci of $no1 is $y"

Output:
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 fibo.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./fibo.sh
Enter no for fibonacci = 34
Fibonacci of 34 is 9227465
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

2). Write a shell script to display tables of 2 to 10 numbers.


EXERCISE :
Execute the script for the following
1) The for loop using day of week list
Ans.
for i in sun mon tue wed thur fri sat
do
echo $i
done
Output :
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 week.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./week.sh
sun
mon
tue
wed
thur
fri
sat
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

2) The while loop to print different * patterns.


Ans
read -p 'Enter no for pattern = ' no
i=1
j=1
while [ $i -le $no ]
do
while [ $j -le $no ]
do
if [ $i -le $j ]
then
echo -n "* "
fi
j=$(( j + 1 ))
done
j=1
i=$(( i + 1 ))
echo ""
done

Output :
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 pattern.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./pattern
bash: ./pattern: No such file or directory
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./pattern.sh
Enter no for pattern = 9
*********
********
*******
******
*****
****
***
**
*
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

3) The case statements for performing various mathematical operations.


Ans.
read -p "enter 1st no" no1
read -p "enter 2nd no" no2
echo -e "1 add \n2 subtract \n3 multiply \n4 divide"
read -p "enter your choice " ch
case $ch in
1) c=$(( $no1 + $no2 ))
echo "addition = $c ";;
2) c=$(( $no1 - $no2 ))
echo "subtraction = $c ";;
3) c=$(( $no1 * $no2 ))
echo "multiplication = $c ";;
4) c=$(( $no1 / $no2 ))
echo "division = $c ";;
*) echo "invalid choice";;
esac
Output :
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ chmod 777 cal.sh
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$ ./cal.sh
enter 1st no5
enter 2nd no10
1 add
2 subtract
3 multiply
4 divide
enter your choice 1
addition = 15
Shaikhamir@shaikh-HP-Laptop-15s-gy0xxx:~$

You might also like