[英]How would I tell a bash script to start over from the top?
For example, in the below script startover
starts back from the top:例如,在下面的脚本中,
startover
启动从顶部开始:
##########################################################################
## CHECK TIME
##########################################################################
time=$(date +%k%M)
if [[ "$time" -ge 1800 ]] && [[ "$time" -le 2200 ]];then
echo "Not a good time to transcode video!" && exit 0
else
echo "Excellent time to transcode video!" && echo "Lets get started!"
fi
##########################################################################
## CHECK TIME
##########################################################################
startover
Also keeping in mind exit 0
should be able to stop the script.还要记住
exit 0
应该能够停止脚本。
You could "recurse" using the following line:您可以使用以下行“递归”:
exec bash "$0" "$@"
Since $0
is the path to the current script, this line starts the script without creating a new process, meaning you don't need to worry about too many restarts overflowing the process table on your machine.由于
$0
是当前脚本的路径,此行启动脚本而不创建新进程,这意味着您无需担心太多重新启动会溢出您机器上的进程表。
Put it in a while loop.把它放在一个while循环中。 I'd also suggest you add a "sleep" so that you're not racing your machine's CPU as fast as it will go:
我还建议你添加一个“睡眠”,这样你就不会像机器的 CPU 那样快地运行:
while true; do
##########################################################################
## CHECK TIME
##########################################################################
time=$(date +%k%M)
if [[ "$time" -ge 1800 ]] && [[ "$time" -le 2200 ]]; then
echo "Not a good time to transcode video!" && exit 0
else
echo "Excellent time to transcode video!" && echo "Lets get started!"
fi
##########################################################################
## CHECK TIME
##########################################################################
for i in {1..5}; do
echo $i
sleep 1
done
done
DO NOT USE WHILE LOOP at the start of the script since the condition below will exit the script and break the loop.不要在脚本开始时使用 WHILE LOOP,因为下面的条件将退出脚本并中断循环。
echo "Not a good time to transcode video!" && exit 0
You can try trapping the exit signal so that when the script exits it restarts您可以尝试捕获退出信号,以便在脚本退出时重新启动
##########################################################################
## CHECK TIME
############bash##############################################################
trap '<path to script> ' EXIT
time=$(date +%k%M)
if [[ "$time" -ge 1800 ]] && [[ "$time" -le 2200 ]];then
echo "Not a good time to transcode video!" && exit 0
sleep 1;
else
echo "Excellent time to transcode video!" && echo "Lets get started!"
sleep 1;
fi
##########################################################################
## CHECK TIME
##########################################################################
echo 1
echo 2
echo 3
echo 4
echo 5
startover
Note: I add a sleep of 1 second because this will give you the time to see message.注意:我添加了 1 秒的睡眠时间,因为这将使您有时间查看消息。 trap the exit signal and re-running the script is acting like a while loop.
捕获退出信号并重新运行脚本就像一个 while 循环。 I am also assuming that these codes are in a script.
我还假设这些代码在脚本中。
How about enclosing the entire script in a while
loop?如何将整个脚本包含在一个
while
循环中? For example,例如,
while :
do
script
done
You may want to add a condition to break out of the loop.您可能想要添加一个条件以跳出循环。
This is not good practice, but what you asked for.这不是好的做法,而是您所要求的。
Put this at the end of your script.将其放在脚本的末尾。
"$( cd "$( dirname "$0" )" && pwd )/$(basename $0)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.