繁体   English   中英

如何从特定行启动shell脚本?

[英]How to start a shell script from a specific line?

#!/bin/sh
echo "install 1"
echo "install 2"
[ $? -eq 0 ] || exit $?;
cyberciti 
[ $? -eq 0 ] || exit $?;
echo "install 3"
echo "install 4"

上面的脚本在第4行( cyberciti )失败了。 在进行必要的更正后,如何跳过已经执行的命令,如何获取行号,并从该行开始重新运行脚本?

要跳过前4行(因此从第5行开始):

tail --lines=+5 script.sh | sh

根据手册页 , - --lines=K表示output the last K lines, instead of the last 10; or use -n +K to output starting with the Kth output the last K lines, instead of the last 10; or use -n +K to output starting with the Kth

幸得亚伦Digulla他的回答在这里 编辑:感谢l0b0防止我的cat 接管世界

当你认为你经常要做这样的事情时,你可以使用一个特殊的变量。 并创建一个可选的命令行参数(默认从开头开始),告诉您从哪里开始。
非结构化版本看起来像:

#!/bin/sh
if [ $# -eq 1 ]; then
   PHASE=$1
else
   PHASE=1
fi
echo phase $PHASE

if [ ${PHASE} -eq 1 ]; then
        echo "install 1"
        (( PHASE = PHASE + 1 ))
fi

if [ ${PHASE} -eq 2 ]; then
        echo "install 2"
        [ $? -eq 0 ] || exit $?;
        (( PHASE = PHASE + 1 ))
fi

if [ ${PHASE} -eq 3 ]; then
        echo cyberciti
[ $? -eq 0 ] || exit $?;
        # Still phase 3, cyberciti is not a special phase

        echo "install 3"
        (( PHASE = PHASE + 1 ))
fi

if [ ${PHASE} -eq 4 ]; then
        echo "install 4"
        (( PHASE = PHASE + 1 ))
fi

改进将使用while循环:

#!/bin/sh
if [ $# -eq 1 ]; then
   PHASE=$1
else
   PHASE=1
fi

function phase2 {
        echo "install 2"
        [ $? -eq 0 ] || exit $?;
}

function phase3 {
        echo cyberciti
        [ $? -eq 0 ] || exit $?;
        # Still phase 3, cyberciti is not a special phase
        echo "install 3"
}

while [ ${PHASE} -lt 5 ]; do
        [ ${PHASE} -eq 1 ] && echo "install 1"
        [ ${PHASE} -eq 2 ] && phase2
        [ ${PHASE} -eq 3 ] && phase3
        [ ${PHASE} -eq 4 ] && echo "install 4"
        (( PHASE = PHASE + 1 ))
done

进一步的改进可能包括逻辑名称和一些可在一个阶段内改变的特殊流量控制。

编辑:使用案例构建的改进

#!/bin/sh
if [ $# -eq 1 ]; then
   PHASE=$1
else
   PHASE=1
fi

function phase2 {
        echo "install 2"
        [ $? -eq 0 ] || exit $?;
}

function phase3 {
        echo cyberciti
        [ $? -eq 0 ] || exit $?;
        # Still phase 3, cyberciti is not a special phase
        echo "install 3"
}

endloop=false
while [ ${endloop} = false ]; do
   case ${PHASE} in
       1) echo "install 1" ;;
       2) phase2 ;;
       3) phase3 ;;
       4) echo "install 4" ;;
       5) endloop=true ;;
       *) echo "Phase ${PHASE} not supported" ;;
    esac
    (( PHASE = PHASE + 1 ))
done
#!/bin/sh
if [ $# -eq 1 ]; then
 PHASE=$1
else
 PHASE=1
fi
if [ ${PHASE} -le 6 ]; then
    echo "install 1 "
    echo "install 2 "
fi
if [ ${PHASE} -le 10 ]; then
  cyberciti
  [ $? -eq 0 ] || exit $?;
  echo "install 3 "
  [ $? -eq 0 ] || exit $?;
fi
if [ ${PHASE} -le 16 ]; then
    echo "install 4"
    [ $? -eq 0 ] || exit $?;
fi

这是我需要的最终脚本,当我们第一次运行时( sh test.sh )将在cyberciti失败,然后我们只需要在用有效命令替换cyberciti之后从这一行运行,所以它从这一行恢复运行参数为错误行( sh test.sh 11

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM