在bash中循环显示一系列我可以做的数字
to loop through a continous list of numbers in bash I can do
for s in $(seq 1 5);do echo ${s} done遍历连续的数字列表,但我可以在python中将给定的数字排除在外:
to loop through a continous list of numbers leaving a given number out in python I can do:
list = [s2 for s2 in range(6)[1:] if s2 != s1] for s1 in list: print s1其中列表包含除s1之外的范围内的所有数字
where list contains all numbers in range except s1
我如何在bash中做同样的事情?
How do I do the same in bash?
推荐答案只需使用continue跳过此步骤:
for s in {1..5} # note there is no need to use $(seq...) do [ "$s" -eq 3 ] && continue # if var is for example 3, jump to next loop echo "$s" done这将返回:
1 2 4 # <--- 3 is skipped 5来自 Bash参考手册→4.1 Bourne Shell Builtins :
继续 continue [n]
针对以下内容恢复封闭的下一个迭代: 环形.如果提供了n,则执行第n个封闭循环 恢复. n必须大于或等于1.返回状态为 除非n不大于1,否则为零.
Resume the next iteration of an enclosing for, while, until, or select loop. If n is supplied, the execution of the nth enclosing loop is resumed. n must be greater than or equal to 1. The return status is zero unless n is not greater than or equal to 1.
更多推荐
bash遍历数字列表(给定数字除外)
发布评论