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

后端 未结 5 366
迷失自我
迷失自我 2020-12-10 19:24

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

##########################################################################
         


        
相关标签:
5条回答
  • 2020-12-10 20:14

    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 讨论(0)
  • 2020-12-10 20:20

    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.

    0 讨论(0)
  • 2020-12-10 20:21

    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.

    0 讨论(0)
  • 2020-12-10 20: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
    
    0 讨论(0)
  • 2020-12-10 20:24

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

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

    0 讨论(0)
提交回复
热议问题