我想写一个bash脚本,它不断地重复运行程序,每次递增前四位数,直到程序的输出从一件事变成另一件事。
例如:
./exampleProgram userName 0000-4567-4561-4564
输出:错误
./exampleProgram userName 0001-4567-4561-4564
输出:错误
./exampleProgram userName 0002-4567-4561-4564
输出:正确
-Loop终止
最后三组四位数将保持不变,只有前四组会发生变化,所以最坏的情况是大约10,000个循环。
发布于 2017-04-01 23:18:40
例如:
exampleProgram() { #demo
echo "Debug: $1 $2" >&2
(($RANDOM % 10)) && { echo "Wrong"; return 1; } || { echo "Correct for $1 $2" ; return 0; }
}
for i in {0000..9999}
do
res=$(exampleProgram JohnDoe "$i-4567-4561-4564")
[[ "$res" =~ Correct ]] && break;
done
echo "Loop terminated at ($i) - result $res"打印
Debug: JohnDoe 0000-4567-4561-4564
Debug: JohnDoe 0001-4567-4561-4564
Debug: JohnDoe 0002-4567-4561-4564
Debug: JohnDoe 0003-4567-4561-4564
Loop terminated at (0003) - result Correct for JohnDoe 0003-4567-4561-4564如果exampleProgram有不同的exit status,最好检查它而不是返回的字符串,如下所示:
res=$(exampleProgram JohnDoe "$i-4567-4561-4564")
(( $? )) || break;https://stackoverflow.com/questions/43158434
复制相似问题