【发布时间】:2021-01-22 00:02:00
【问题描述】:
##对不起我的英语不好
您好,我正在编写一个脚本,要求员工按顺序在选项 3 处添加记录:电话号码、姓氏、名字、部门编号、职位等。 我的问题是,当我输入正确的电话号码时,脚本不会询问我的姓氏或其他任何内容并退出.. 我希望此代码在验证正确的电话号码后询问我的姓氏、名字......开。
代码如下
#!/bin/bash
# Define the menu list here
while :
do
echo "1 - Print All Current Records"
echo "2 - Search for Specific Record(s)"
echo "3 - Add New Records"
echo "4 – Delete Records"
echo "Q – Quit"
echo ""
read -p "Your selection: " option
# What to do if any of the above is selected.
case $option in
1)
cat records
;;
2)
read -p "Enter keyword: " keyword
if grep -i $keyword records >> .rec
then
cat .rec
elif [ -z $keyword ]
then
echo "Keyword not entered"
else
echo $keyword not found
fi
;;
3)
printf "\nAdd New Employee Record"
while :
do
read -p "Phone number (xxxxxxxx): " PhoneNumber
# Validate phone number
if [ -z "$PhoneNumber" ]
then
echo "Phone number not entered"
elif ! [ "$PhoneNumber" -eq "$PhoneNumber" ] 2> /dev/null
then
echo "Sorry integers only"
elif [ ! ${#PhoneNumber} -eq 8 ] || [[ ! ${PhoneNumber:0:1} == "9" ]]
then
echo "Invalid Phone number"
elif grep -q $PhoneNumber records
then
echo "Phone number exists"
else
echo $PhoneNumber >> records
fi
done
while :
do
read -p "Family Name: " FamilyName
# Validate Family Name
if [ -z $FamilyName ] || echo "$FamilyName" | grep -i -q '^[a-z/ ]*$'
then
echo $FamilyName >> records
else
echo "Family name can contain only alphabetic characters and spaces"
fi
done
while :
do
read -p "Given Name: " GivenName
# Validate Given Name
if [ -z $GivenName ] || echo "$GivenName" | grep -i -q '^[a-z/ ]*$'
then
echo $GivenName >> records
else
echo "Given name can contain only alphabetic characters and spaces"
fi
done
while :
do
read -p "Department Number: " DptNum
# Validate Department Number
if [ -z $DptNum ] || [ ! ${#DptNum} -eq 2 ] || ! [ "$DptNum" -eq "$DptNum" ] 2> /dev/null
then
echo "A valid department number contains 2 digits"
else
echo $DptNum >> records
fi
done
while :
do
read -p "Job Title: " JobTitle
# Validate Job Title
if [ -z $JobTitle ] || echo "$JobTitle" | grep -i -q '^[a-z/ ]*$'
then
echo $JobTitle >> records
else
echo "Job title can contain only alphabetic characters and spaces"
fi
done
printf "Adding new employee record to the records file ...\nNew record saved.\n"
while true
do
read -p "Add another? (y)es or (n)o: " choice
case $choice in
[Yy] ) echo ok; break;;
[Nn] ) exit;;
* ) echo "Please press y or n";;
esac
done
;;
4)
echo "Delete Employee Record"; echo /n
read -p "Enter a Phone number: " fon
# Validate phone number
if [ -z "$fon" ] || ! [ "$fon" -eq "$fon" ] 2> /dev/null || [ ! ${#fon} -eq 8 ] || [[ ! ${fon:0:1} == "9" ]]
then
echo "Invalid Phone Number"
elif ! grep -q $fon records
then
echo "Phone number not found"
else
grep $fon records
fi
while true
do
read -p "Confirm deletion: (y)es or (n)o: " answer
case $answer in
[Yy] ) grep -v "$answer" records >tempfile && rm records && mv tempfile records
echo "Record deleted."; break;;
[Nn] ) break;;
[Qq] ) exit 0;;
* ) echo "Please press y or n or q for exit.";;
esac
done
;;
Q)
break
;;
*)
echo Invalid selection
;;
esac
done
【问题讨论】:
-
在寻求人工帮助之前尝试shellcheck.net,并尝试将您的问题减少到minimal reproducible example。完全不清楚代码的哪一部分没有按应有的方式工作,很可能部分或全部问题是由于简单的引用错误造成的,Shellcheck 很乐意指出。另见When to wrap quotes around a shell variable
-
它是退出,还是一遍又一遍地询问电话号码?
-
@GordonDavisson 对不起我的错误,它一直在问电话号码。
标签: bash loops while-loop