簡體   English   中英

bash腳本從上次失敗的地方開始執行

[英]bash script to execute from where it was failed last time

任何人都可以告訴我如何編寫一個腳本來執行它上次停止的位置。 我的bash腳本包含24個順序執行的腳本文件。 但是,如果任何一個腳本失敗,下次當我執行腳本文件時,我不希望腳本從腳本1開始,而應該從上次失敗的地方開始。 請指教。

一種粗暴的方式:

#!/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

但通過Makefile做這件事似乎也是一個很好的解決方案......

.NOTPARALLEL
.PHONY: all frest_start

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

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

frest_start:
    rm step*
    make all
#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

每當在腳本上出現任何錯誤時,都會在我的機器上測試的terminal.i上給出錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM