Execute a bash script from the moment it was unsuccessful for the last time

Can someone tell me how to write a script to execute from which it was stopped last time. My bash script contains 24 script files that are executed sequentially. But if any of the script fails, the next time I run the script file, I do not want the script to start with script1, instead it should start from where it failed last time. Please inform.

+4
source share
2 answers

One rough way:

#!/bin/bash
# Needs bash 4 or later, for `;&` to work
[ "$1" = "--frest_start" ] && rm statusfile

touch statusfile
read status < statusfile
[ "$status" = "" ] && status=0

case $status in
0) ./script1; echo 1 > statusfile ;&
1) ./script2; echo 2 > statusfile ;&
2) ./script3; echo 3 > statusfile ;&

# ....... & so on till

23) ./script24; echo 24 > statusfile ;;

esac

But doing it through Makefileseems like a good solution too ...

.NOTPARALLEL
.PHONY: all frest_start

all:
    make step1
    make step2
    make step3
    ....
    make step24

step%: script%
    "./$<"
    touch "$@"

frest_start:
    rm step*
    make all
+2
source
#mail.sh

function errortest {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        echo "error with $1" >&2
    fi
    return $status
}
errortest sh /home/user1/test1.sh
errortest sh /home/user1/test2.sh

, script, terminal.i, .

-1

Source: https://habr.com/ru/post/1610568/


All Articles