首页 > 技术文章 > shell练习

tony9 2016-10-12 10:45 原文

1、报错:

shell2: line 16: syntax error: unexpected end of file

原因:

shell中条件判断式的格式为:

if [ ]; then

...

elif [] ; then

...

else

...

fi

或者

if [] 

then

...

else

...

fi

但是我将elif写成了else if,所以这边报错。

2、

报错:syntax error near unexpected token `else'

原因:if或者else后面少写了then

3、打印1-100的和

结果打印:

[root@jason shell]# sh shell10

the sum=0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100

原因:

expr需要用空格隔开每个项,我没有隔开,所以将每一项打印出来了

4、读取脚步后的参数

使用for循环去读取时需要使用如下:

for arg in "$@"

错误使用:

for((i=0;i<3;i++))

do

echo $i

done

推荐阅读