运行 一系列任务,但取决于 bash 中任务 x 的进度开始(继续切换案例)
Run a series of tasks, but depending on progress start on task x in bash (continue switch case)
我有一系列任务,一个接一个 运行,但根据进度,这些任务保存在一个变量中,它必须在适当的步骤开始。
比如我有5个任务。任务 1 应该开始,然后是任务 2,然后是任务 3,依此类推。但是如果变量 progress 包含值 2,它应该从任务 2 开始,继续任务 3 等等。
我认为这可以通过开关盒实现,例如:
case $progress in
1)
echo "Task1"
$progress=2;;
2)
echo "Task2"
$progress=3;;
3)
echo "Task3";;
*)
echo "null";;
esac
这里的问题是 switch case 在第一次匹配后结束。如何让 switch case 在第一场比赛后继续?
我发现最近的 bash 版本提供 ;&
允许在 switch case 语句中使用 "falltrough"。这正是我要找的。
case $progress in
1)
echo "Task1"
$progress=2
;&
2)
echo "Task2"
$progress=3
;&
3)
echo "Task3";;
*)
echo "null";;
esac
您还可以在默认情况下使用 while
循环和 break 2
语句。
为了补充 ,这里是 case
-分支终止符的概述:
;;
[唯一的选项高达Bash3.x]
- 立即退出
case
语句(从匹配的分支)。
;&
[Bash 4.0+]
- 无条件进入下一个分支 - 就像在C [=17中一样=] 语句的
case
处理程序没有 break
。请注意,无条件意味着进入下一个分支,无论其条件如何。
;;&
[Bash 4.0+]
- 继续评估分支条件直到另一个分支条件(如果有的话)匹配。
;;
例子;输出行 one
:
case 1 in
1) # entered, because it matches and is the first condition
echo 'one'
;; # exits the `case` statement here
?) # never entered
echo 'any single char.'
;;
esac
;&
示例 (Bash 4.0+);输出行 one
和 two
:
case 1 in
1) # entered, because it matches and is the first condition
echo 'one'
;& # fall through to very next branch
2) # entered, because the previous branch matched and fell through
echo 'two'
;; # exits the `case` statement here
esac
;;&
示例 (Bash 4.0+);输出行 one
和 any
:
case 1 in
1) # entered, because it matches and is the first condition
echo 'one'
;;& # continue evaluation branch conditions
2) # not entered, because its condition doesn't match
echo 'two'
;;
*) # entered, because it's the next *matching* condition
echo 'any'
;; # using a terminator is optional in the * branch
esac
我有一系列任务,一个接一个 运行,但根据进度,这些任务保存在一个变量中,它必须在适当的步骤开始。
比如我有5个任务。任务 1 应该开始,然后是任务 2,然后是任务 3,依此类推。但是如果变量 progress 包含值 2,它应该从任务 2 开始,继续任务 3 等等。
我认为这可以通过开关盒实现,例如:
case $progress in
1)
echo "Task1"
$progress=2;;
2)
echo "Task2"
$progress=3;;
3)
echo "Task3";;
*)
echo "null";;
esac
这里的问题是 switch case 在第一次匹配后结束。如何让 switch case 在第一场比赛后继续?
我发现最近的 bash 版本提供 ;&
允许在 switch case 语句中使用 "falltrough"。这正是我要找的。
case $progress in
1)
echo "Task1"
$progress=2
;&
2)
echo "Task2"
$progress=3
;&
3)
echo "Task3";;
*)
echo "null";;
esac
您还可以在默认情况下使用 while
循环和 break 2
语句。
为了补充 case
-分支终止符的概述:
;;
[唯一的选项高达Bash3.x]- 立即退出
case
语句(从匹配的分支)。
- 立即退出
;&
[Bash 4.0+]- 无条件进入下一个分支 - 就像在C [=17中一样=] 语句的
case
处理程序没有break
。请注意,无条件意味着进入下一个分支,无论其条件如何。
- 无条件进入下一个分支 - 就像在C [=17中一样=] 语句的
;;&
[Bash 4.0+]- 继续评估分支条件直到另一个分支条件(如果有的话)匹配。
;;
例子;输出行 one
:
case 1 in
1) # entered, because it matches and is the first condition
echo 'one'
;; # exits the `case` statement here
?) # never entered
echo 'any single char.'
;;
esac
;&
示例 (Bash 4.0+);输出行 one
和 two
:
case 1 in
1) # entered, because it matches and is the first condition
echo 'one'
;& # fall through to very next branch
2) # entered, because the previous branch matched and fell through
echo 'two'
;; # exits the `case` statement here
esac
;;&
示例 (Bash 4.0+);输出行 one
和 any
:
case 1 in
1) # entered, because it matches and is the first condition
echo 'one'
;;& # continue evaluation branch conditions
2) # not entered, because its condition doesn't match
echo 'two'
;;
*) # entered, because it's the next *matching* condition
echo 'any'
;; # using a terminator is optional in the * branch
esac