I currently have a text file called student.txt and there are some records in the text file.
The format of the records are StudentID|Name|ContactNumber|EmailAddress and the records are as follows:
21PMR07222|Steven|011-1234567|steven@yahoo.com
21PMR07123|John|012-3456789|john@yahoo.com
21PMR07221|Amy|017-4567890|amy@gmail.com
The code below is to check whether the student ID entered by the user exist or not exist in the student.txt file:
awk -F'|' -v id="$studentID" '
$1 == id { found=1
printf "Name: %s\nContact Number: %s\nEmail Address: %s\n", $2, $3, $4
}
END { if (found==0) {
printf "The student ID does not exist. Please enter again."
}
}
' < student.txt
The user can enter the student ID to search for the student details. How to loop to let the user to reenter the student ID if the student ID entered by the user does not exist in the text file?