bash select menu get index
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #!/bin/bash INSTALL_PATH="/usr/local" BFGMINER_INSTALL_PATH="${INSTALL_PATH}/bfgminer" BFGMINER_REPO="https://github.com/luke-jr/bfgminer.git" list_last_ten_bfgminer_tags () { cd ${BFGMINER_INSTALL_PATH} git for-each-ref --format="%(refname)" --sort=-taggerdate --count=10 refs/tags | cut -c 6- } clone_bfgminer () { cd ${INSTALL_PATH} git clone ${BFGMINER_REPO} ${BFGMINER_INSTALL_PATH} } echo"select number to switch tag or n to continue" select result in master $(list_last_ten_bfgminer_tags) do # HOW DO I CHECK THE INDEX??????? <================================= QUESTION if [[ ${result} == [0-9] && ${result} < 11 && ${result} > 0 ]] then echo"switching to tag ${result}" cd ${BFGMINER_INSTALL_PATH} git checkout ${result} else echo"continue installing master" fi break done |
因此,如果用户输入1,case语句将检查文本上的匹配情况,那么如何在1上进行匹配呢?
使用
1 2 3 4 5 6 7 8 9 10 | PS3="Select what you want>" select answer in"aaa""bbb""ccc""exit program" do case"$REPLY" in 1) echo"1" ; break;; 2) echo"2" ; break;; 3) echo"3" ; break;; 4) exit ;; esac done |
您不需要检查选择了哪个值;您可以简单地使用它。你唯一想检查的是
1 2 3 4 5 6 7 8 9 10 11 12 13 | select result in master $(list_last_ten_bfgminer_tags) do if [[ $result = master ]]; then echo"continue installing master" elif [[ -z"$result" ]]; then continue else echo"switching to tag ${result}" cd ${BFGMINER_INSTALL_PATH} git checkout ${result} fi break done |
我很难理解您的问题,但这里有一些示例代码;数组可以动态填充,我猜这就是您的来源:
1 2 3 4 5 6 7 8 9 10 | $ cat t.sh #!/bin/bash foo=(one two three four) echo"Please select an option:" select reply in"${foo[@]}"; do [ -n"${reply}" ] && break done echo"You selected: ${reply}" |
.
1 2 3 4 5 6 7 8 9 10 | $ ./t.sh Please select an option: 1) one 2) two 3) three 4) four #? 5 #? 100 #? 2 You selected: two |
这怎么不够?
当然,如果您希望输出/逻辑与