[英]descending loop with variable bash
$ cat fromhere.sh
#!/bin/bash
FROMHERE=10
for i in $(seq $FROMHERE 1)
do
echo $i
done
$ sh fromhere.sh
$
Why doesn't it works?为什么它不起作用?
I can't find any examples searching google for a descending loop..., not even variable in it.我找不到任何在谷歌搜索降序循环的例子......,甚至没有变量。 Why?为什么?
您应该使用 seq 指定增量:
seq $FROMHERE -1 1
Bash has a for
loop syntax for this purpose.为此,Bash 有一个for
循环语法。 It's not necessary to use the external seq
utility.没有必要使用外部seq
实用程序。
#!/bin/bash
FROMHERE=10
for ((i=FROMHERE; i>=1; i--))
do
echo $i
done
You might prefer Bash builtin Shell Arithmetic instead of spawning external seq :您可能更喜欢 Bash内置Shell Arithmetic 而不是产生外部seq :
i=10
while (( i >= 1 )); do
echo $(( i-- ))
done
loop down with for (stop play)循环使用 for(停止播放)
for ((q=500;q>0;q--));do echo $q ---\>\ `date +%H:%M:%S`;sleep 1;done && pkill mplayer
500 ---> 18:04:02
499 ---> 18:04:03
498 ---> 18:04:04
497 ---> 18:04:05
496 ---> 18:04:06
495 ---> 18:04:07
...
...
...
5 ---> 18:12:20
4 ---> 18:12:21
3 ---> 18:12:22
2 ---> 18:12:23
1 ---> 18:12:24
pattern :模式:
for (( ... )); do ... ; done
example例子
for ((i=10;i>=0;i--)); do echo $i ; done
result结果
10
9
8
7
6
5
4
3
2
1
0
with while: first step使用while:第一步
AAA=10
then那么
while ((AAA>=0));do echo $((AAA--));sleep 1;done
or: "AAA--" into while或:“AAA--”进入while
while (( $((AAA-- >= 0)) ));do echo $AAA;sleep 1;done
"sleep 1" isn't need不需要“睡眠 1”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.