0% found this document useful (0 votes)
72 views

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

The document provides examples of shell scripts to perform various operations on arrays in Bash shell scripting. The examples include scripts to: 1) Read and display array elements 2) Calculate the sum of array elements 3) Add two arrays by element and store in a third array 4) Sort an array in ascending and descending order 5) Reverse the elements of an array For each example, the script code and sample input/output is given.

Uploaded by

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

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

The document provides examples of shell scripts to perform various operations on arrays in Bash shell scripting. The examples include scripts to: 1) Read and display array elements 2) Calculate the sum of array elements 3) Add two arrays by element and store in a third array 4) Sort an array in ascending and descending order 5) Reverse the elements of an array For each example, the script code and sample input/output is given.

Uploaded by

Dhruv Pandit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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