Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash中是否有do-while循环?_Bash - Fatal编程技术网

bash中是否有do-while循环?

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

bash中是否有
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中,它们是同义词。