GTU OS Program - 8

8. Write a shell script to read n numbers as command arguments and sort them in descending order.

Complete code of the program

read -p "Enter the number of values you want to sort "  n

for((i=0; i<$n; i++))
do
        read -p "Enter value of arr[$i]: " arr[$i]
done
#sorting code
for((i=0; i<$n; i++))
do
        for((j=0; j<n-i-1; j++))
        do
                if [ ${arr[j]} -lt ${arr[$((j+1))]} ]
                then
                        #swapping
                        temp=${arr[j]}
                        arr[$j]=${arr[$((j+1))]}
                        arr[$((j+1))]=$temp
                fi
        done
done

echo "Numbers sorted in descending order as"
echo ${arr[*]}

Interpreting the sort.sh file and executing it in terminal

tkanu025@hp:~/lab_solutions$ chmod +x sort.sh
tkanu025@hp:~/lab_solutions$ ./sort.sh
Output
gtu study material best gtu study material for engineering students

gtu os lab manual,

os GTU Practical,

operating system Programs,

Operating system shellscript programs,

Gtu study material 3140705,

OS 3140702,

os practical list,

shell script programming,

gtu os practical,

os lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OOP Programs - 11

Mini project ideas for Operating System