0

The Code Below showing "Command not found " at line 12.I have tried many way but it did not fix.What is the problem?Can anyone explain?

#!bin/bash
echo "How many number do you want to input?"
read numbers
echo "Give your input seperated by enter"
array=()
for (( i = 0 ; i < $numbers ; i=$i+1 ));
do
    read num;
    array+=($num);
done
for (( j = 0 ; j < $numbers-1 ; j=$j+1 ));
do
    if ["${array[$j]}" -gt "${array[$j+1]}"];
    then
        tempo=${array[$j]};
        array[$j]=${array[$j+1]};
        array[$j+1]=$temp;
    fi
done

for (( i = 0 ; i < $numbers ; i=$i+1 )); 
do 
    echo ${array[$i]};
done
Mahir Mahbub
  • 126
  • 1
  • 11
  • You forgot to describe your problem, add expected and actual output, and ask a question. – Benjamin W. Jul 24 '17 at 14:30
  • You're missing a `/` in your shebang, it should be `#!/bin/bash`. (I had added that as an answer but then noticed you said the error was thrown at line 12, so I guess that's not your main problem) – Aaron Jul 24 '17 at 14:33
  • @BenjaminW. Sorry for the trouble.I have added details.Can you help me? – Mahir Mahbub Jul 24 '17 at 14:34
  • @Aaron I have changed this.But It do not stop showing the same error. – Mahir Mahbub Jul 24 '17 at 14:38
  • 1
    You also need to have spaces inside your `[ ... ]` tests : `if [ "${array[$j]}" -gt "${array[$j+1]}" ];`. I ran your code in ideone.com with my suggestions included and that was the next part to fail. You should include the whole error message next time : even if the `[9` part in `line 13: [9: command not found` means nothing to you, it's a huge hint about what's going wrong. – Aaron Jul 24 '17 at 14:55
  • I suggest checking your code with http://www.shellcheck.net/ which would probably indicate all these problems without the need to wait for a back-and-forth on stackoverflow. – Aaron Jul 24 '17 at 14:56
  • @Aaron Within `(( ))`, you can have spaces around `=`. – Benjamin W. Jul 24 '17 at 15:00
  • @BenjaminW. thanks, I removed my incorrect comment. – Aaron Jul 24 '17 at 15:01
  • This really should be run through shellcheck, as Aaron said, and most syntax errors would be pointed out. – Benjamin W. Jul 24 '17 at 15:02
  • 2
    Possible duplicate of [if compare strings get a "command not found"-Error](https://stackoverflow.com/questions/19733437/if-compare-strings-get-a-command-not-found-error) – Aserre Jul 24 '17 at 15:04

3 Answers3

1

Copy-pasting your original code into shellcheck.net lends this useful result :

Line 13:
    if ["${array[$j]}" -gt "${array[$j+1]}"];
    ^-- SC1009: The mentioned parser error was in this if expression.
       ^-- SC1035: You need a space after the [ and before the ].
       ^-- SC1073: Couldn't parse this test expression.
                                            ^-- SC1020: You need a space before the ].
                                            ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.

This is your first problem, a syntax error that makes bash parse [ and whatever follows it once expanded as a single command, which it fails to find.

Once fixed, shellcheck will be able to bring up many other problems, such as defining a tempo variable then referencing an undefined temp variable afterwards.

Some of the tips it produces probably aren't relevant to your use-case (such as read mangling backslashes). It's up to you to apply them or not, but it's always a good thing to know the shortcomings of the commands you're using.

Aaron
  • 24,009
  • 2
  • 33
  • 57
1

Be careful with your spaces in BASH. Line 12 should read:

 if [ "${array[$j]}" -gt "${array[$j+1]}" ];
monomeric
  • 48
  • 5
0

Remove all spaces in operations like this: j = 0. It must be: j=0

In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. Bash treats most lists of words as a command with parameters.

And add spaces in this line if ["${array[$j]}" -gt "${array[$j+1]}"];. it must be if [ "${array[$j]}" -gt "${array[$j+1]}" ];. You can read about spaces more here: Why should there be a space after '[' and before ']' in Bash?

Vadim Beskrovnov
  • 941
  • 6
  • 18