Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

repeat

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.

like image 376
David Custer Avatar asked Dec 01 '22 15:12

David Custer


2 Answers

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.

like image 74
chepner Avatar answered Dec 04 '22 05:12

chepner


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
like image 33
Ivan X Avatar answered Dec 04 '22 03:12

Ivan X