0

I looked everybody but I'm stuck with this code.

For example : The user calls ./array.sh 3 5 6 2 1 I am supposed to sort (i thought bubble sort) and print the array sorted.

    #! /bin/bash
    tab=( $@ )
    define -i temp
    for ((i = ${#tab[*]-1 ; i >= 0 ; i--)) ; do
        for ((j = 0 ; i - 1; j++)) ; do
        if [ ${tab[$i]} > ${tab[$i+1]} ] then
            $temp = ${tab[$i+1]
            ${tab[$i+1]} = ${tab[$i]}
            ${$tab[$i]} = $temp 
        fi
    done
    echo ${tab[*]} #print the array

But bash is not happy with that, he keeps tellin me that I cannot assing values like that. What do I do wrong ? Can you help me please ? I looked in a lot of places but there is no way to find the solution.

Thanks you in advance guys.

3
  • Run your script through shellcheck.net; most of your problems are simple syntax errors. Commented Oct 15, 2014 at 19:29
  • I strongly urge you to fix your errors: that will help your development as a programmer. I also urge you not to reinvent the wheel: printf "%s\n" "$@" | sort -n suffices. Commented Oct 15, 2014 at 19:36
  • chepner thank you very much for the website, I almost corrected the code when @JohnBollinger showed me the way. for GlennJackman , I know right, but that's an assignment from a very old school teacher, thanks again everybody Commented Oct 15, 2014 at 20:16

3 Answers 3

2

You cannot assign a value to a value (e.g. ${tab[$i+1]} = ${tab[$i]}); you must assign to a name or to an array element. Also, you may not have space on either side of the equals sign (=) in a bash assignment, except in numeric context.

And you dropped some closing braces.

And you misspelled "declare".

And you need a semicolon or newline between your if condition and your then.

And you didn't terminate your inner loop.

And you reference a non-existent array element via ${tab[$i+1]}.

And your inner loop has a constant as its termination condition.

And ${#tab[*]-1} incorrectly attempts to do math inside the braces delimiting the variable reference.

And you referenced the wrong index variable (consistently) in your inner loop.

And > is a redirection operator, not greater than, except in numeric context.

Once you clear up that multitude of errors, you end up with

#! /bin/bash
tab=( $@ )
declare -i temp
for ((i = ${#tab[*]} - 1; i > 0 ; i--)) ; do
  for ((j = 0 ; $j < $i; j++)) ; do
    if [ ${tab[$j]} -gt ${tab[$j+1]} ]; then
      temp=${tab[$j+1]}
      tab[$j+1]=${tab[$j]}
      tab[$j]=$temp 
    fi
  done
done
echo ${tab[*]} #print the array

which actually works.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you a lot, i am a beginner and i was used to code in c, so bash is really new for me, sorry for wasting your time but I really looked in a lot of places even here. thanks again a lot
2

Your variable assignment syntax is all wrong.

First, you don't put $ before the variable being assigned.

Second, you must not have spaces around =.

So it should be:

temp=${tab[$i+1]}
tab[$i+1]=${tab[$i]}
tab[$i]=$temp

Comments

1

I understand that you are working on a class assignment or such and are restricted in how you are allowed to solve the problem. For those not so restricted, here is a simple solution using standard unix tools:

#!/bin/bash
( IFS=$'\n'; echo "$*" ) | sort -n

Sample usage:

$ script.sh 3 5 6 2 1
1
2
3
5
6

Explanation:

  • ( IFS=$'\n'; echo "$*" )

    This causes the command line arguments to be printed, one per line. This is in a subshell so that the assignment to IFS does not affect the rest of the script.

  • sort -n

    -n tells sort to apply a numeric sort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.