如何制作仅接受Capitals的If-Then-Else语句

时间:2015-03-07 06:51:15

标签: unix if-statement

我有一个用korn shell编写的UNIX脚本。我需要做到这一点:

while true
            do
                echo "What is the last name of the person you would like to modify:"
                read last_name
                if line=$(grep -i "^${last_name}:" "$2")
                then
                    IFS=: read c1 c2 c3 c4 rest <<< "$line"
                    echo -e "Last Name: $c1\nFirst Name: $c2\nState: $c4"
                    while true
                    do
                        echo "What would you like to change the state to?:"
                        read state
                            if [[ $state -eq [A-Z] ]];then
                                echo "State: $state"
                                echo "This is a valid input"
                                break
                            else
                                echo "Not a valid input:"
                            fi
                    done                        
                else
                    echo "ERROR: $last_name is not in database"
                    echo "Would you like to search again (y/n):"
                    read delete_choice
                    case $delete_choice in [Nn]) break;; esac
                fi  
            done
 ;;

具体来说,我遇到了这段代码的问题:

if [[ $state -eq [A-Z] ]];then

该程序的目的是修改文本文件中的记录,但只接受状态缩写的输入,如(MI,WA,KS,....)。

1 个答案:

答案 0 :(得分:1)

尝试类似:

if echo $state | egrep -q '^[A-Z]{2}$'
then
    ...
fi

^[A-Z]{2}$表示您的州的开头和结尾都是长度为2的CAPS字母。