bash遍历数字列表(给定数字除外)

编程入门 行业动态 更新时间:2024-10-24 22:20:47
本文介绍了bash遍历数字列表(给定数字除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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遍历数字列表(给定数字除外)

本文发布于:2023-11-30 21:30:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   遍历   列表   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!