How would I tell a bash script to start over from

2019-01-19 06:51发布

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.

5条回答
ら.Afraid
2楼-- · 2019-01-19 07:12

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楼-- · 2019-01-19 07:20

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-19 07:22

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
查看更多
再贱就再见
5楼-- · 2019-01-19 07:29

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

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

查看更多
孤傲高冷的网名
6楼-- · 2019-01-19 07:30

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.

查看更多
登录 后发表回答