我如何告诉 bash 脚本从头开始?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27219901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I tell a bash script to start over from the top?
提问by David Custer
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
应该能够停止脚本。
回答by Ivan X
Put it in a while loop. I'd also suggest you add a "sleep" so that you're not racing your machine's CPU as fast as it will go:
把它放在一个while循环中。我还建议你添加一个“睡眠”,这样你就不会像机器的 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
回答by chepner
You could "recurse" using the following line:
您可以使用以下行“递归”:
exec bash "echo "Not a good time to transcode video!" && exit 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
是当前脚本的路径,因此该行启动脚本而不创建新进程,这意味着您无需担心太多重启会溢出您机器上的进程表。
回答by repzero
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,因为下面的条件将退出脚本并中断循环。
##########################################################################
## 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
You can try trapping the exit signal so that when the script exits it restarts
您可以尝试捕获退出信号,以便在脚本退出时重新启动
while :
do
script
done
Note: I add a sleep of 1 second because this will give you the time to see message. trap the exit signal and re-running the script is acting like a while loop. I am also assuming that these codes are in a script.
注意:我添加了 1 秒的睡眠时间,因为这将使您有时间查看消息。捕获退出信号并重新运行脚本就像一个 while 循环。我还假设这些代码在脚本中。
回答by unxnut
How about enclosing the entire script in a while
loop? For example,
如何将整个脚本包含在一个while
循环中?例如,
You may want to add a condition to break out of the loop.
您可能想要添加一个条件以跳出循环。
回答by MeetTitan
This is not good practice, but what you asked for.
这不是好的做法,而是您所要求的。
Put this at the end of your script. "$( cd "$( dirname "$0" )" && pwd )/$(basename $0)"
将其放在脚本的末尾。 "$( cd "$( dirname "$0" )" && pwd )/$(basename $0)"