0

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
jww
  • 97,681
  • 90
  • 411
  • 885
MONKAX
  • 11
  • 2
  • `[ "$foo" -gt "$bar" ]` -- `-gt` is a numeric test only. Use `[[ $foo > $bar ]]` (the double-brackets are critical) for a lexicographic comparison. – Charles Duffy Mar 30 '18 at 16:43
  • BTW, `function foo() {` is munging together legacy ksh syntax bash supports for backwards compatibility, of `function foo {` with no `()`, and POSIX-standard syntax, of `foo() {` with no `function`. Pick one or the other -- I suggest the latter, as bash's "ksh-like" function syntax doesn't actually behave the way the ksh versions it's taken from treated the real thing (where using the `function` keyword made variables local-by-default). See http://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Mar 30 '18 at 16:45
  • Note that while bash supports `[ "$foo" \> "$bar" ]`, that's not POSIX-standardized; see the note near the end of the RATIONALE section of http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html for an explanation of *why* that's not in the standard. – Charles Duffy Mar 30 '18 at 16:47
  • Also, `echo -e` is problematic in general -- even bash doesn't always support it, emitting `-e` on output when both `xpg_echo` and `posix` runtime flags are active. Much safer to use `printf` when you want to use format strings; see also http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html, particularly the APPLICATION USAGE section. – Charles Duffy Mar 30 '18 at 16:48
  • @CharlesDuffy ` [[ $foo > $bar ]] ` worked for me perfectly. I was under the assumption that bubblesort just converts letters to numbers and sorts them out like that. In regards to `echo -e` I completely agree. This program was just made to test out bubblesorting mechanisms to understand how it works. I appreciate all the references too, cheers! – MONKAX Mar 30 '18 at 16:53

0 Answers0