如何制作仅接受大写字母的 If-Then-Else 语句
How to make an If-Then-Else statement that accepts only Capitals
我有一个用 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}:" "")
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, ....)。
试试这样的东西:
if echo $state | egrep -q '^[A-Z]{2}$'
then
...
fi
^[A-Z]{2}$
表示您的州以长度为 2 的大写字母开始和结束。
我有一个用 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}:" "")
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, ....)。
试试这样的东西:
if echo $state | egrep -q '^[A-Z]{2}$'
then
...
fi
^[A-Z]{2}$
表示您的州以长度为 2 的大写字母开始和结束。