Professional Documents
Culture Documents
SRISHTI GUPTA (MCA-2 SEM-4) (Roll No-2190212054) Assingment-4 On Loops
SRISHTI GUPTA (MCA-2 SEM-4) (Roll No-2190212054) Assingment-4 On Loops
MCA -2
IV semester
2190212054
1. IF….ELSE statement-
Program to compare if the two numbers are equal-
2. a=10
3. b=20
4.
5. if [ $a == $b ]
6. then
7. echo "a is equal to b"
8. else
9. echo "a is not equal to b"
10. Fi
2. for loop-
Example 1-Program of “for loop” to print “Welcome n times”-
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Output-
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
Output-
3. until loop-
Example 1- Program to display that the until loop prints the current
value of the variable and counter increments the variable by one.
#!/bin/bash
counter=0
Output-
Example 2- This program will print the value of ‘a’ two times from 1 to
a=1
until [ $a -ge 3 ]
do
a=`expr $a + 1`
done
Output-
4. while loop-
Example 1- This program will print the value of ‘a’ five times, from 1 to
5.
a=1
while [ $a -le 5 ]
do
a=`expr $a + 1`
done
Output-
#!/bin/bash
counter=$1
factorial=1
do
counter=$(( $counter - 1 ))
done
echo $factorial
Output-
5. read command-
echo "one two three four" | while read word1 word2 word3; do
done
Output-
Example 2- Program to read first middle and last name in unix and
print it.
#!/bin/bash
Output-
6. Array-
array1[1]=1
echo ${array1[0]}
echo ${array1[1]}
echo ${array2[2]}
echo ${array3[9]}
echo ${array3[11]}
read -a array4
for i in "${array4[@]}"
do
echo $i
done
exit 0
Output-