for ((初始值; 限制值; 執行步階)) do 程式段 done
i=1
;
i<=100
;
i=i+1
。
[dywang@dywmac zzz]$ cat sh11.sh #!/bin/bash num=$(echo $1 | egrep -o "[0-9]+") [ -n "$num" ] && [ "$num" -gt 1 ] && [ "$num" -lt "100" ] \ && max="$num" || max="100" sum=0 for ((i=1; i<=max; i++)); do ((sum+=i)) done echo "1+..+$max=$sum"
[dywang@dywmac zzz]$ sh sh11.sh 1+..+100=5050 [dywang@dywmac zzz]$ sh sh11.sh num 1+..+100=5050 [dywang@dywmac zzz]$ sh sh11.sh -12 1+..+12=78 [dywang@dywmac zzz]$ sh sh11.sh 201 1+..+100=5050 [dywang@dywmac zzz]$ sh sh11.sh 56 1+..+56=1596
for var in con1 con2 con3 ... do 程式段 done
[dywang@dywmac zzz]$ cat sh12.sh #!/bin/bash num=$(echo $@ | egrep -o "[0-9]+") sum=0 for i in $num; do ((sum+=$i)) done echo "$(echo $num | sed 's/\ \ */+/g')=$sum"
[dywang@dywmac zzz]$ sh sh12.sh 12x34f56fe328fad 12+34+56+328=430 [dywang@dywmac zzz]$ sh sh12.sh 12 74n 23d wf3 12+74+23+3=112