clear
echo "Enter size : "
read size
echo "Enter elements : "
for (( i=0; i<size; i++ ))
do
echo "Element " $i " : "
read arr[$i]
done
m=$(($n-1))
for (( i=0; i<m; i++ ))
do
cond=$((size-$i-1))
for (( j=0; j<cond; j++ ))
do
l=$(($j+1));
if [ ${a[$j]} -gt ${a[$l]} ]
then
tmp=${a[$j]}
a[$j]=${a[$l]}
a[$l]=$tmp
fi
done
done
echo "Sorted Data : "
echo ${a[*]}
This code is executing, while it takes the input and stores them in the array. The the next thing I see is, "Sorted Array" being printed. and no elements are printed after that.
Tried, sh -x sort.sh
and the interim code isn't being executed.
I am quite new to shell script and cannot understand what's wrong.
clear
echo "Enter size : "
read size
echo "Enter elements : "
for (( i=0; i<size; i++ ))
do
echo "Element " $i " : "
read arr[$i]
done
m=$(($size-1))
for (( i=0; i<m; i++ ))
do
cond=$(($size-$i-1))
for (( j=0; j<cond; j++ ))
do
l=$(($j+1));
if [ ${arr[$j]} -gt ${arr[$l]} ]
then
tmp=${arr[$j]}
arr[$j]=${arr[$l]}
arr[$l]=$tmp
fi
done
done
echo "Sorted Data : "
echo ${arr[*]}
Two things that are wrong in your program:
arr[]
but later referring it as a[]
. Change one of the names to match the other.$n
in your program. You mean $size
. Change this as well.I think your program should work after this.