syntax error near unexpected token `(' 问题解决方法

 #!/bin/bash

 #Program:
#
#History:
#2018/11/10 LY first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "please input a number, I will count for 1+2+3+...+your_input: " nu
s=0
for ( ( i=1; i<=$nu; i=i+1 ) )
do
s=$ ( ($s+$i) )
done"
echo "the result of '1+2+3+...+$nu' is ==> $s"
 

假设运行一直出现一个错误是

please input a number, I will count for 1+2+3+...+your_input: 10
sh09.sh: line 11: syntax error near unexpected token `('
sh09.sh: line 11: `for ( ( i=1; i<=$nu; i=i+1 ) )'

 

有两种解决方法:第一种:可以将

for ( ( i=1; i<=$nu; i=i+1 ) )  改成for (( i=1; i<=$nu; i=i+1 ))

s=$ ( ($s+$i ) ) 改成 s=$[$s+$i]

第二种 删除括号之间的空格

for ( ( i=1; i<=$nu; i=i+1 ) )  改成for (( i=1; i<=$nu; i=i+1 ))

s=$ ( ($s+$i ) ) 改成 s=$(($s+$i))

 

PS:若有不合理之处,望留言

 

你可能感兴趣的:(syntax error near unexpected token `(' 问题解决方法)