How would I tell a bash script to start over from the top?

二次信任 提交于 2019-11-27 19:21:40

问题


For example, in the below script startover starts back from the top:

##########################################################################
## 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.


回答1:


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 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



回答2:


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.




回答3:


DO NOT USE WHILE LOOP at the start of the script since the condition below will exit the script and break the 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. 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.




回答4:


How about enclosing the entire script in a while loop? For example,

while :
do
    script
done

You may want to add a condition to break out of the loop.




回答5:


This is not good practice, but what you asked for.

Put this at the end of your script. "$( cd "$( dirname "$0" )" && pwd )/$(basename $0)"



来源:https://stackoverflow.com/questions/27219901/how-would-i-tell-a-bash-script-to-start-over-from-the-top

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!