Reputation: 3238
I need to implement something like:
if [ $i -ne $hosts_count - 1] ; then
cmd="$cmd;"
fi
But I get
./installer.sh: line 124: [: missing `]'
What I am doing wrong?
Upvotes: 4
Views: 8851
Reputation: 4797
In bash, you can avoid both [ ]
and [[ ]]
by using (( ))
for purely arithmetic conditions:
if (( i != hosts_count - 1 )); then
cmd="$cmd"
fi
Upvotes: 3
Reputation: 798536
]
must be a separate argument to [
.You're assuming you can do math in [
.
if [ $i -ne $(($hosts_count - 1)) ] ; then
Upvotes: 4
Reputation: 6780
The command [
can't handle arithmetics inside its test. Change it to:
if [ $i -ne $((hosts_count-1)) ]; then
Edit: what @cebewee wrote is also true; you must put a space in front of the closing ]
. But, just doing that will result in yet another error: extra argument '-'
Upvotes: 8
Reputation: 3667
The closing ]
needs to be preceded by a space, i.e. write
if [ $i -ne $hosts_count - 1 ] ; then
cmd="$cmd;"
fi
Upvotes: 0