bash中是否有do-while循环?
bash中是否有bash中是否有do-while循环?,bash,Bash,bash中是否有do-while循环 我知道如何在bash中编写循环时编写: while [[ condition ]]; do body done 是否有类似的构造,但对于do-while循环,主体至少执行一次,而不管while条件如何?否,在bash中没有do while操作符。您可以通过以下方式进行模拟: while true; do body [[ condition ]] || break done 你可能只是想试试这个,我不相信bash中有这样的东西 do
do
-while
循环
我知道如何在bash中编写循环时编写:
while [[ condition ]]; do
body
done
是否有类似的构造,但对于do
-while
循环,主体
至少执行一次,而不管while
条件如何?否,在bash
中没有do while
操作符。您可以通过以下方式进行模拟:
while true; do
body
[[ condition ]] || break
done
你可能只是想试试这个,我不相信bash中有这样的东西
doStuff(){
//first "iteration" here
}
doStuff
while [condition] do
doStuff
done
while
循环足够灵活,可以用作do while
循环:
while
body
condition
do true; done
例如,要请求输入并循环直到它成为整数,可以使用:
while
echo "Enter number: "
read n
[[ -z $n || $n == *[^0-9]* ]]
do true; done
这甚至比正常的do while
循环更好,因为您可以使用仅在第一次迭代后执行的命令:将true
替换为echo“请输入有效数字”
阅读手册页中的bash内置列表并查看其中是否有一个是在执行的有多难?@Barmar查找不存在的东西要比查找存在的东西困难得多。如果不在循环外调用函数体,则永远不会执行。修复了先调用函数的代码。+1。但是do:;done
是一个更好的noopIt。同样的noop写得不同。我坚持纠正。至少在bash中,它们是同义词。