带有多个提示和选项的shell脚本

时间:2013-02-07 03:31:25

标签: bash shell sh

我正在编写一个脚本来自动化Linux内核构建过程。在我的脚本中,我有提示,有些是/否,其他是特定的操作。这是我的问题: 该函数运行后如何正确关闭函数并移动到下一个选项? 如何跳转到指定的操作并绕过两者之间的提示/操作? 这是一个例子:

#!/bin/bash
echo "Do you need to install the necessary compiling tools?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) sudo apt-get install tools; break;;
        No ) <Here I want to skip to the next step. I originally left it 
              blank and removed the "done" command (after esac command) 
              to do this, but if I choose yes, it skips to the end 
              (where the next "done" command is and ends the script>
    esac

<I had to flip the yes/no for the script to skip to the next prompt>
echo "Do you need to eidt your configuration?"
select ny in "No" "Yes"; do
    case $ny in
        No ) <what do I put here to skip to the next tag 
             (labeled compile for example purposes); break;;
        Yes ) 
    esac

echo "You have 3 options with how you can edit you config file."
select yn in "Gtk" "KDE" "Terminal"; do
    case $yn in
    Gtk ) make gconfig; break;;
    KDE ) make xconfig; break;; 
        Terminal ) make menuconfig; break;;
    esac
done
done
done        <I had to insert all these "done" to end the file, but 
                 the scripts doesn't work the way I want it to...>
echo "Are you ready to compile"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) make; break;;
        No ) exit;;
    esac
done

所以基本上脚本分阶段运行。 1.您需要安装工具吗?     - 如果是,请安装它们并转到下一步     - 如果不是,请转到下一步 2.是否要编辑配置文件     - 如果是,如何;            - opt1            - opt2            - opt3          - 然后,完成后,跳到下一步     - 如果不是,请跳到下一步 3.编译

1 个答案:

答案 0 :(得分:0)

企业风险管理...

echo "Do you need to eidt your configuration?"
select ny in "No" "Yes"; do
    case $ny in
        No ) break;;
        Yes ) 
          echo "You have 3 options with how you can edit you config file."
          select yn in "Gtk" "KDE" "Terminal"; do
              case $yn in
              Gtk ) make gconfig; break;;
              KDE ) make xconfig; break;; 
                  Terminal ) make menuconfig; break;;
              esac;
             ...
          break;;
    esac

或者更好的是,将每个部分放在一个函数中并根据需要调用它们。