使用esac中的case检查数组中的用户输入字符串?

时间:2014-06-05 08:39:33

标签: case ksh

我正在尝试在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.

我该如何进行验证?请帮忙。

谢谢, KARTHIK

1 个答案:

答案 0 :(得分:0)

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

    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