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

2020-05-07 06:28发布

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.

标签: shell
1条回答
▲ chillily
2楼-- · 2020-05-07 06:55

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.

查看更多
登录 后发表回答