2015-10-07 31 views
4

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

+5

聽起來像你需要一個makefile依賴於一些狀態文件,隨着make過程的進展而寫入 –

回答

2

一種粗暴的方式:

#!/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 "[email protected]" 

frest_start: 
    rm step* 
    make all 
+1

請注意';&'需要'bash' 4或更高版本。 – chepner

+0

感謝很多傢伙..它的工作.. – vinu

+0

如果這對你有用,不要忘記將它標記爲接受的答案...另外,對於我的信息,讓我知道,你使用了2個選項中的哪一個 - bash /生成文件? – anishsane

-1
#mail.sh 

function errortest { 
    "[email protected]" 
    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 
每當發生在腳本的任何錯誤

,它給出終端錯誤我在我的機器上測試過。

+0

不要將您的函數名稱稱爲'test'。 'test'是內置的'bash'。 – anishsane

+0

這只是報告錯誤。它不允許腳本在之前的錯誤之後恢復到原來的位置。 – chepner