syntax error near unexpected token `then' ./sleep.sh: line 8: ` then let count++;'

时间:2021-05-16 22:46:39

从书上抄的个shell脚本 sleep.sh

如下:

echo -n count:
tput sc

count=0;
while true;
do
  if [ $count   -lt  40 ]
  then let count++;
       sleep 1;
       tput rc
       tput ed
       echo -n $count;
  else exit 0;
  fi
done
~

执行后报错:

count:./sleep.sh: line 8: syntax error near unexpected token `then'
./sleep.sh: line 8: `  then let count++;'

 

刚开始学习对shell脚本语法不清楚,原因如下:

1.if后要有空格

2.[] 中括号的开头和结尾要有空格!

3. [ $count   -lt  40 ]中括号中的$1和-eq和"root"之间不需要空格!

 

========================

echo -n count:
tput sc

count=0;
while true;
do
  if [ $count-lt40 ]
  then let count++;
       sleep 1;
       tput rc
       tput ed
       echo -n $count;
  else exit 0;
  fi
done