You are on page 1of 8

1.

Implement a shell script to read & display array element


Input:-
for(( i=0; i<2; i++ ))
do
read a[$i]
done
for(( i=0; i<2; i++ ))
do
echo " "$[a[$i]]
done
Output:- ubuntu@ubuntu:~$ bash cb.sh
1
3

1
3

2. Implement a shell script for sum of array element


Input:- echo "Enter elements of array"
for(( i=0; i<5; i++ ))
do
read a[$i]
done
for(( i=0; i<5; i++ ))
do
w=`expr $w + $[a[$i]]`
done
echo $w

Output:- Enter elements of array


1
3
2
5
4
15

3. Implement a shell script for addition of 2 arrays


Input:- echo "Enter elements of first array"
for(( i=0; i<5; i++ ))
do
read a[$i]
done
echo "Enter array of second array"
for(( i=0; i<5; i++ ))
do
read b[$i]
done
echo "Finally"
for(( i=0; i<5; i++ ))
do
c[$i]=`expr $[a[$i]] + $[b[$i]]`
done
for(( i=0; i<5; i++ ))
do
echo $[c[$i]]
done

Output:- Enter elements of first array


1
3
2
5
4
Enter array of second array
5
3
4
1
2
Finally
6
6
6
6
6

4. Implement a shell script to sort array (ascending & descending)


Input:- for(( i=0; i<5; i++ ))
do
read a[$i]
done
for(( i=0; i<5; i++ ))
do
for(( j=0; j<5; j++ ))
do
if [ $[a[$i]] -gt $[a[$j]] ]
then
temp=$[a[$i]]
a[$i]=$[a[$j]]
a[$j]=$temp
fi
done
done
echo "Descending"
for(( i=0; i<5; i++ ))
do
echo $[a[$i]]
done
for(( i=0; i<5; i++ ))
do
for(( j=0; j<5; j++ ))
do
if [ $[a[$i]] -lt $[a[$j]] ]
then
temp=$[a[$i]]
a[$i]=$[a[$j]]
a[$j]=$temp
fi
done
done
echo "Ascending"
for(( i=0; i<5; i++ ))
do
echo $[a[$i]]
done

Output:- 1
5
2
4
3
Descending
5
4
3
2
1
Ascending
1
2
3
4
5

5. Implement a shell script to reverse the array


Input:- for(( i=0; i<5; i++ ))
do
read a[$i]
done
echo "echo"
for(( i=4; i>=0; i-- ))
do
echo $[a[$i]]
done

Output:- 1
5
2
4
3
echo
3
4
2
5
1
U.V. Patel College of Engineering
B. Tech Computer Science and
Engineering

Sub: Basics of Operating System and Shell Scripting


(2CSE204)

NAME : Pinkesh Siddhapura

CLASS : CBA

ENROLMENT NO : 16012101038

BATCH : CSE - 22

Practical 5 :
U.V. Patel College of Engineering
B. Tech Computer Science and
Engineering

Sub: Basics of Operating System and Shell Scripting


(2CSE204)

NAME : Pinkesh Siddhapura

CLASS : CBA

ENROLMENT NO : 16012101038

BATCH : CSE - 22

Practical 6 :

You might also like