if [“$i” -gt “$count”]; IS GIVING AN ERROR

2020-05-07 06:24发布

问题:

I'm trying to put the 'f-$count'(f-1,f-2) name into the array.

Below is the code,

echo "Enter the count"
read count
echo $count

#arr=()
i=1
while true;
do
 if ["$i" -gt "$count"]; then
  exit 0
 else
  arr[$i]=f-$i
  i=$((i+1))
 fi
done
echo ${arr[@]}

I'm getting the error as 'script.sh: line 11: [3570: command not found ' continuously.

回答1:

In shell programming, the brackets in the if MUST be delimited by spaces:

if ["$i" -gt "$count"]; then

MUST be:

if [ "$i" -gt "$count" ]; then

[EDIT] The left bracket ([) is actually a built-in shell command and so requires the space afterwards to delimit it from its parameters, as with any command.



标签: shell