2015-02-11 84 views
-1

我有一个脚本有几个输入,脚本最终会启动一个下载,一旦下载完成,我想提示用户如果他们想下载别的东西来启动这个过程。什么是回到脚本开始的最佳方式?

while true;do 
    read -p "Is this correct? (yes/no/abort) " yno 
    case $yno in 
     [Yy]*) break;; 
     [Nn]*) echo "Lets Start Over" 'restart script code goes here';; 
     [Aa]*) exit 0;; 
      *) echo "Try again";; 

    esac 
done 

echo 
echo "Starting $build download for $opt1 from Jenkins" 
echo 

while true;do 
    read -p "Do you want to download something else? " yesno 
    case $yesno in 
     [Yy]*) 'restart script code goes here';; 
     [Nn]*) break;; 
     *) echo "Try Again " 
    esac 
done 
+0

您可以使用'exec $ 0'来替换当前调用的新调用,从顶部开始采用新的流程,这有一些优势)。或者,使用'redo = yes; while [“$ redo”=“yes”];做...你现在的代码...; done',如果用户不想再次尝试,您可以在其中修改代码以设置'redo = no'。在这两者之间,循环更传统。 – 2015-02-11 00:33:57

+0

这与shell脚本无关,您需要在开始实现它们之前了解有关条件语句和循环的基础知识。我建议先创建一个程序流程图。 (使用铅笔) – hek2mgl 2015-02-11 00:36:41

回答

3

如果设计与壳功能的shell脚本,重复的代码块变得容易得多:

main() { 
    while true; do 
     next 
     if ! validate_opt 'Do you want to download something else?'; then 
      break 
     fi 
    done 
} 
validate_opt() { 
    local PS3="$1 (Press ctrl-c to exit) " 
    local choice 
    select choice in yes no; do 
     # This can be written more tersely, 
     # but for clarity... 
     case $choice in 
      yes) return 0;; 
      no) return 1;; 
     esac 
    done 
} 
do_download() { 
    echo 
    echo "Starting $build download for $opt1 from Jenkins" 
    echo 
    fetch "$1" # or whatever 
} 
next() { 
    if validate_opt 'Is this correct?'; then 
     do_download "$opt" 
    else 
     echo "Let's start over" 
    fi 
} 
main 
0
function stage1 { 
while true;do 
    read -p "Is this correct? (yes/no/abort) " yno 
    case $yno in 
     [Yy]*) stage2;; 
     [Nn]*) continue;; 
     [Aa]*) exit 0;; 
      *) echo "Try again";; 

    esac 
done 
} 

function stage2 { 
echo 
echo "Starting $build download for $opt1 from Jenkins" 
echo 

while true;do 
    read -p "Do you want to download something else? " yesno 
    case $yesno in 
     [Yy]*) stage1;; 
     [Nn]*) exit 0;; 
     *) echo "Try Again ";; 
    esac 
done 

} 
stage1 

为此,您可以使用函数

第一个功能是第1阶段和第2阶段2 列出所有函数后,在文件底部我们调用stage1。 当函数stage1执行时,它将跳转到stage2函数,反之亦然,当我们在阶段2时

相关问题