syntax error near unexpected token `(' 問題解決方法
阿新 • • 發佈:2018-12-20
#!/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:若有不合理之處,望留言