我正在创建一个sh文件,用户在其中输入卷号,名称,主题名称和标记。我将它存储在一个文本文件中。 现在我不希望用户再次输入相同的卷号。如果他输入相同的卷号,它应该抛出一条消息。 这就是我所做的:
read -p "Enter the roll: " roll
read -p "Enter the name: " name
read -p "Enter the Subject: " sub
read -p "Enter the mark: " mark
echo "$roll $name $sub $mark" >> asd.txt
如何防止输入重复的卷号。
答案 0 :(得分:0)
touch asd.txt
rollExists=1
while [ $rollExists != 0 ]
do
read -p "Enter the roll: " roll
rollExists=`grep -e "^${roll} " asd.txt | wc -l`
if [[ "$rollExists" != "0" ]]
then
echo "Roll already exists"
fi
done
read -p "Enter the name: " name
read -p "Enter the Subject: " sub
read -p "Enter the mark: " mark
echo "$roll $name $sub $mark" >> asd.txt