What my program does is allow the user to input how many times to input the name and then push all the names to the array
After adding the names to an array they are supposed to be sorted alphabetically and then echod out.
The names are displayed in an array succesfully but I get an error. In this case I tried putting in "John" as a name input and got the integer expression expected error.
line 36: [: John: integer expression expected
I understand that the names are not an integer but would like some guidance on how to go about converting it to alphabetical.
Would appreciate any help as I am new to bubblesorting
sp=()
x=0
function code()
{
read -p "Number of times to run code: " smw
}
function name()
{
read -p "Enter Name: " sp[x]
echo "${sp[x]}"
let "x++"
}
function repeat()
{
for (( l=1; l<$smw; l++))
do
name
done
}
function show()
{
echo "Names in the array are:"
for (( x = 0; x <= $smw; x++ ))
do
echo ${sp[x]}
done
for (( x = 0; x < $smw ; x++ ))
do
for (( p = $x; p < $smw; p++ ))
do
if [ ${sp[x]} -gt ${sp[$p]} ]; then
r=${sp[x]}
sp[$x]=${sp[$p]}
sp[$p]=$r
fi
done
done
echo -e "\nSorted Names Alphabetically: "
for (( x=0; x < $smw; x++ ))
do
echo ${sp[x]}
done
}
code
repeat
name
show