Dear all,

What is wrong with my if condition, it never enters the if:

#!/bin/bash
echo "Enter integers, enter -1 to stop."

declare -a FOO

for((i=0;;i++)); do
 read FOO[i] 
 if [ "[$FOO[i]" == -1] ]; then #Never enters here
	echo "inside"
    break
  fi 
done

foonum=${#FOO}

for ((i=0;i<$foonum;i++)); do
  echo ${FOO[${i}]}
done

By the way, if you see any other syntax or logical error, please let me know.

Thank you.

Hello,

It looks the syntax you are using is based in PHP and your first line tells the system to use the bash shell to execute the script. If this is a php script remove the first line and it should run from a browser. If you meant to use bash then your syntax is all wrong.

It's weird, Of course this is Linux Shell programming (Bash) and I found these part by part in online tutorials.

So, can you give some directions what parts I went wrong and how to fix it???

Hi.

Dear all,

What is wrong with my if condition, it never enters the if:

#!/bin/bash
echo "Enter integers, enter -1 to stop."

declare -a FOO

for((i=0;;i++)); do
 read FOO[i] 
 if [ "[$FOO[i]" == -1] ]; then #Never enters here
	echo "inside"
    break
  fi 
done

foonum=${#FOO}

for ((i=0;i<$foonum;i++)); do
  echo ${FOO[${i}]}
done

By the way, if you see any other syntax or logical error, please let me know.

Thank you.

Both of last two lines of if worked for me:

#!/bin/bash
echo "Enter integers, enter -1 to stop."

declare -a FOO

for((i=0;;i++)); do
 read FOO[i] 
 # if [ "[$FOO[i]" == -1] ]; then #Never enters here
 # if [[ "${FOO[i]}" == -1 ]]; then #Never enters here
 if [ "${FOO[i]}" = -1 ]; then #Never enters here
	echo "inside"
    break
  fi 
done

foonum=${#FOO}

for ((i=0;i<$foonum;i++)); do
  echo ${FOO[${i}]}
done

The bash man page is long, but the description of [[ and test ([) should help you understand this ... cheers, hotcold

Thanks, it's working now. If any other problem came up, I will ask again.

Thank you so much.

hotCold, thank you so much, that indeed was tiny problem and was the only problem, thanks it's working now.

Appreciate your help very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.