0

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.

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by group256
0

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.

0

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???

0

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

Edited by hotcold: n/a

0

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

Thank you so much.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.