bash set -e 导致for循环随机退出
bash set -e causing random exit from for loop
当我使用 set -e 选项时,下面的简化循环随机退出。如果我删除 set -e 选项,它总是会完成。如果可能的话,我想使用 set -e 选项,但到目前为止,我对它退出的原因以及每次我 运行 它时随机循环迭代发生的原因感到茫然(试试吧!)。如您所见,仅有的命令是 let 和 echo。为什么 let 或 echo 命令 return 会随机出现一个非零代码,或者是其他原因?
#!/bin/bash
# Do Release configuration builds so we can set the build parameters
set -e
CFG=Release
for CASE in {0..511}
do
# CASE [0...511] iterate
# MMMM [2...255] random test cases
# NNNN [1..MMMM) random test cases
# RRRR [0...255] random test cases
# XXXX [0...255] random test cases
# DSXX [1...128] random test cases
# OASM [1...255] random test cases
# OLSM [1...255] random test cases
let "MMMM = $RANDOM % 254 + 2"
let "NNNN = $RANDOM % ($MMMM - 1) + 1"
let "RRRR = $RANDOM % 256"
let "XXXX = $RANDOM % 256"
let "DSXX = $RANDOM % 128 + 1"
let "OASM = $RANDOM % 255 + 1"
let "OLSM = $RANDOM % 255 + 1"
echo CFG = $CFG, CASE = $CASE, MMMM = $MMMM, NNNN = $NNNN, RRRR = $RRRR, XXXX = $XXXX, DSXX = $DSXX, OASM = $OASM, and OLSM = $OLSM
# Some other stuff (build and test), that is not causing the problem, goes here
done
# Some other stuff, that is not causing the problem, goes here
exit 0
将 || true
附加到您的 let
命令或使用 $((...))
进行计算。
来自help let
:
Exit Status: If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.
当我使用 set -e 选项时,下面的简化循环随机退出。如果我删除 set -e 选项,它总是会完成。如果可能的话,我想使用 set -e 选项,但到目前为止,我对它退出的原因以及每次我 运行 它时随机循环迭代发生的原因感到茫然(试试吧!)。如您所见,仅有的命令是 let 和 echo。为什么 let 或 echo 命令 return 会随机出现一个非零代码,或者是其他原因?
#!/bin/bash
# Do Release configuration builds so we can set the build parameters
set -e
CFG=Release
for CASE in {0..511}
do
# CASE [0...511] iterate
# MMMM [2...255] random test cases
# NNNN [1..MMMM) random test cases
# RRRR [0...255] random test cases
# XXXX [0...255] random test cases
# DSXX [1...128] random test cases
# OASM [1...255] random test cases
# OLSM [1...255] random test cases
let "MMMM = $RANDOM % 254 + 2"
let "NNNN = $RANDOM % ($MMMM - 1) + 1"
let "RRRR = $RANDOM % 256"
let "XXXX = $RANDOM % 256"
let "DSXX = $RANDOM % 128 + 1"
let "OASM = $RANDOM % 255 + 1"
let "OLSM = $RANDOM % 255 + 1"
echo CFG = $CFG, CASE = $CASE, MMMM = $MMMM, NNNN = $NNNN, RRRR = $RRRR, XXXX = $XXXX, DSXX = $DSXX, OASM = $OASM, and OLSM = $OLSM
# Some other stuff (build and test), that is not causing the problem, goes here
done
# Some other stuff, that is not causing the problem, goes here
exit 0
将 || true
附加到您的 let
命令或使用 $((...))
进行计算。
来自help let
:
Exit Status: If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.