bash 脚本从上次失败的地方开始执行
bash script to execute from where it was failed last time
谁能告诉我如何编写一个脚本从上次停止的地方执行。我的 bash 脚本包含 24 个顺序执行的脚本文件。但是,如果任何一个脚本失败,下次我执行脚本文件时,我不希望脚本从 script1 开始,而是应该从上次失败的地方开始。请指教
一种粗略的方式:
#!/bin/bash
# Needs bash 4 or later, for `;&` to work
[ "" = "--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 " >&2
fi
return $status
}
errortest sh /home/user1/test1.sh
errortest sh /home/user1/test2.sh
每当脚本出现任何错误时,它都会在 terminal.i 上出现错误,在我的机器上进行了同样的测试。
谁能告诉我如何编写一个脚本从上次停止的地方执行。我的 bash 脚本包含 24 个顺序执行的脚本文件。但是,如果任何一个脚本失败,下次我执行脚本文件时,我不希望脚本从 script1 开始,而是应该从上次失败的地方开始。请指教
一种粗略的方式:
#!/bin/bash
# Needs bash 4 or later, for `;&` to work
[ "" = "--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 " >&2
fi
return $status
}
errortest sh /home/user1/test1.sh
errortest sh /home/user1/test2.sh
每当脚本出现任何错误时,它都会在 terminal.i 上出现错误,在我的机器上进行了同样的测试。