【问题标题】:Check User Input String in array with case in esac?用esac中的大小写检查数组中的用户输入字符串?
【发布时间】:2014-06-05 08:39:33
【问题描述】:

我正在尝试在 bash 中编写一个 ksh shell 脚本来检查用户输入的字符串是否等于存储在数组中的元素。

loc_ora=/etc/oratab
osid=`sed -nr 's/^(.*):\/.*$/\1/p' $loc_ora`
set -A arr $osid
OIFS=$IFS;
IFS=" ";
read usid
case "${arr[*]}" in
    * /$usid/ *)
        echo -e " \t\t Entered SID Matches ... \n"
        ORACLE_SID=$usid
        echo -e " \t\t SID: $ORACLE_SID \n"
        export ORACLE_SID
        export ORACLE_HOME
        ;;
    *)
        sleep 03
        echo -e " \t\t Entered SID Does not Match the System !!! \n \t\t Please Re-run the Script with a Valid SID."
        sleep 02
        echo -e " \t\t ABORTING !!! ..."
        sleep 03
        exit 0
        ;;
 esac
IFS=$OIFS;

Error/scenario1:
I am facing a syntax error: `/' unexpected if I keep /$usid/.

Error/scenario2:
If I remove the // and keep like *$usid* in the pattern, I do not face any error but only the second *) half pattern is printing even when I provide the correct SID.

如何进行此验证?请帮忙。

谢谢, 卡提克

【问题讨论】:

    标签: case ksh


    【解决方案1】:

    我建议以不同的方式检查输入的 uid 与 /etc/oratab:

        read uid
        loc_ora=/etc/oratab
        sed -nr 's/^(.*):\/.*$/\1/p' $loc_ora | grep -q "^$uid\$" 
        if [ $? -eq 0 ]; then
            echo "sid valid"
        else
            echo "sid Not Valid"
        Fi
    

    【讨论】:

    • 感谢它的魅力。我根据自己的要求更改了脚本。所以也不需要数组了吧?
    • 哦,伙计,这个单班轮命令我在这里和那里颤抖了 5 天。谢谢男人。
    • 我可以知道或者你能解释一下这些行是如何或做什么的吗? sed -nr 's/^(.*):\/.*$/\1/p' $loc_ora | grep -q "^$uid\$" --> grep -q 将提取输入的字符串?如果 [ $? -eq 0] --> 不知道这个 $ 和 ?在这里做..
    • 如果您注意到 sed 行与原始解决方案中的完全相同,它只是通过删除其他所有内容从 /etc/oratab 中选择 sid。替换命令 s 正在计算整行,方法是仅选择感兴趣的部分 (.*),然后将其 [the line] 替换为 selectedstring only \\1
    • sed 行 if 与您的原始解决方案完全相同,它只是通过删除其他所有内容从 /etc/oratab 中选择 sid。替换命令s 正在计算整行,仅选择感兴趣的部分(.*),然后将其[行] 替换为仅选择字符串\1。然后输出 [sid ids] 通过管道输入到 grep 并且 grep 正在搜索您的输入 sid 只是它悄悄地执行此操作 -q [我们在这里不需要 grep 输出]。可以从 grep 退出代码检查结果,该退出代码可以使用最后执行的退出代码 $? 进行检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2022-01-20
    • 2013-12-07
    • 1970-01-01
    相关资源
    最近更新 更多