重击:select语句不中断 [英] Bash: select statement doesn't break

查看:61
本文介绍了重击:select语句不中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个包含select语句的函数,除非您选择"quit",否则它不会中断:

Here's a function containing a select statement that doesn't break unless you choose 'quit':

function func_set_branch () {
        local _file=$1
        local _expr=$2
        local _bdate=$3
        local _edate=$4
        local _mid=$(awk -F'\t' -v ref="${_expr}" 'BEGIN {IGNORECASE = 1} match($0, ref) {print $5}' "$CONF")

        if (( $(grep -c . <<<"${_mid}") > 1 )); then
                mapfile -t arr <<< "${_mid}"

                PS3="Please choose an option "
                select option in "${arr[0]}" "${arr[1]}" quit
                do
                        case $option in
                                1) _mid="${arr[0]}"; break 2;;
                                2) _mid="${arr[1]}"; break 2;;
                                quit) exit 0;;
                        esac
                done
        fi

        sed "s#{{mid}}#${_mid}#
             s#{{bdate}}#${_bdate}#
             s#{{edate}}#${_edate}#" "$_file"
}

我尝试了不同级别的 break ..no骰子.呆了这么久,我一直想念什么?

I've tried different levels of break ..no dice. What have I missed after staring at this waaaay tooo long?

输出:

automation@automation-workstation2:~/scripts/branch-fines$ bash get_data.sh -f branch-fines.sql -B coulee -b 2014-01-01 -e 2014-12-31
coulee 2014-01-01 to 2014-12-31
1) 472754
2) 472758
3) quit
Please choose an option 1
Please choose an option 2
Please choose an option 3
automation@automation-workstation2:~/scripts/branch-fines$

UPDATE

UPDATE

非常感谢rici和glenn jackman编写的代码.

Working code with much thanks to rici and glenn jackman.

PS3="Please choose an option "
select option in "${arr[0]}" "${arr[1]}" quit
do
    case $option in
        "${arr[0]}") MID="${arr[0]}"; break;;
        "${arr[1]}") MID="${arr[1]}"; break;;
        quit) exit 0;;
    esac
done

推荐答案

select 语句的主体中,指定的变量(在这种情况下为 $ option )为设置为所选单词的值,而不是其索引.这就是为什么 quit 起作用的原因;您正在检查 $ option 是否为 quit ,而不是 3 .同样,您应该检查 $ {arr [0]} $ {arr [1]} ,而不是 1 2 .

In the body of the select statement, the specified variable ($option in this case) is set to the value of the selected word, not its index. That's why quit works; you're checking to see if $option is quit, not 3. Similarly, you should be checking for ${arr[0]} and ${arr[1]} rather than 1 and 2.

由于数组值不是 1 2 ,因此 case 语句中的任何子句都不匹配,因此 case 语句不会执行任何操作;在这种情况下,不会执行 break ,因此 select 会继续循环.

Since the array values are not 1 or 2, no clause in the case statement will match, so the case statement will do nothing; in that case, no break is executed so the select continues looping.

这篇关于重击:select语句不中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆