I want to run a command n number of times if it returns unsuccessful I have started with the below loop
如果它返回不成功,我想多次运行一个命令我已经开始使用下面的循环
until find . -type f -exec md5sum {} \;
do
<something >
done
I want to run the about n times before it goes continues to the next file Not sure how I can continue from here. I have tried using the return variable $? and looping using this but had no luck Also how could I use the above loop or what is proposed here to put the output of the find into a variable thanks
我想在它继续下一个文件之前运行大约n次不确定我怎么能从这里继续。我试过使用return变量$?并使用此循环,但没有运气另外我怎么能使用上面的循环或这里提出的将查找的输出放入变量谢谢
Let me make this abit clearer
让我更清楚一点
so I'm actually running like this with a function
所以我实际上是用这个函数运行的
fcn () {
for file
do
until md5sum "$file"
do
<something >
done
done
}
Calling with
find . -type f -print0 | xargs -0 -P 0 bash -c 'fcn "$@"'
So the return value of md5sum "$file"
is the one I have looked into
所以md5sum“$ file”的返回值是我所研究的
2 个解决方案
#1
attempts=0
while ! result=$(find . …) || (( attempts++ > 5 )); do
…
done
The above will set the results of the successful find command into the variable result
. If attempts exceeds five, then the loop will end, but the value of result
is unclear if that happens.
以上将成功查找命令的结果设置为变量结果。如果尝试次数超过五次,那么循环将结束,但如果发生这种情况,则结果的值不清楚。
#2
You can check the result of a command in the environment variable $?
您可以在环境变量$中检查命令的结果?
This variable will hold the value 0
if the last command executed was successful.
如果执行的最后一个命令成功,则此变量将保持值0。
My guess would be something like this:
我的猜测是这样的:
while true; do
$(command)
if [ $? -eq 0 ] ; then
exit
fi
done
Remember that if anything is executed AFTER your command, the value of $?
will change. So just make sure whenever you execute the command in question here, save $?
value in another variable or check it immediately.
请记住,如果在命令之后执行了任何操作,$的值是多少?将改变。所以只要确保无论何时执行相关命令,请保存$?另一个变量中的值或立即检查它。
#1
attempts=0
while ! result=$(find . …) || (( attempts++ > 5 )); do
…
done
The above will set the results of the successful find command into the variable result
. If attempts exceeds five, then the loop will end, but the value of result
is unclear if that happens.
以上将成功查找命令的结果设置为变量结果。如果尝试次数超过五次,那么循环将结束,但如果发生这种情况,则结果的值不清楚。
#2
You can check the result of a command in the environment variable $?
您可以在环境变量$中检查命令的结果?
This variable will hold the value 0
if the last command executed was successful.
如果执行的最后一个命令成功,则此变量将保持值0。
My guess would be something like this:
我的猜测是这样的:
while true; do
$(command)
if [ $? -eq 0 ] ; then
exit
fi
done
Remember that if anything is executed AFTER your command, the value of $?
will change. So just make sure whenever you execute the command in question here, save $?
value in another variable or check it immediately.
请记住,如果在命令之后执行了任何操作,$的值是多少?将改变。所以只要确保无论何时执行相关命令,请保存$?另一个变量中的值或立即检查它。