bash - 在具有多个条件的bash中编写do while循环
问题描述
我在使用多个条件的 bash 中编写 do-while 循环时遇到了一些麻烦。
我的代码目前可以这样工作:
while
count=$((count+1))
( MyFunction $arg1 $arg2 -eq 1 )
do
:
done
但我想在“do-while”循环中添加第二个条件,如下所示:
while
count=$((count+1))
( MyFunction $arg1 $arg2 -eq 1 ) || ( $count -lt 20 )
do
:
done
当我这样做时,我得到一个“找不到命令错误”。
我一直在尝试这篇文章中的一些 while 循环示例,但没有运气,我使用的 do-while 示例来自这里。特别是有 137 个赞的答案。
解决方案
这(
是语法的一部分,$count
不是有效的命令。test
or[
是用于“测试”表达式的有效命令。
while
count=$((count+1))
[ "$(MyFunction "$arg1" "$arg2")" -eq 1 ] || [ "$count" -lt 20 ]
do
:
done
您提到的答案使用算术表达式 with ((
(不是 single (
,而是 double((
之间没有任何东西)。你也可以这样做:
while
count=$((count+1))
(( "$(MyFunction "$arg1" "$arg2")" == 1 || count < 20 ))
do
:
done
推荐阅读
- c# - 如何使用带有请求正文数据的 POST 方法使用 Azure 函数调用外部 api
- string - 如何根据另一个字符串表(Lua)定义的顺序对字符串表进行排序
- gradle - 如何在 mac 上从 jenkins 运行 testng.xml 文件
- python - 无法让 python 正确格式化日期
- content-management-system - 适用于管理文件并通过公共端点公开文件的无头 CMS
- python - 在python的同一列中添加两个值
- android - 预期为 BEGIN_ARRAY,但在第 1 行第 2 列改造时为 BEGIN_OBJECT2
- python - 终端中的 pip install pyautogui 错误
- google-chrome - 下载受 DRM 保护的媒体
- visual-studio-code - 如何使用 VScode 从另一个远程 SSH 到远程?