Home > database >  How to loop to let the user to reenter if the record is not found in the text file in bash?
How to loop to let the user to reenter if the record is not found in the text file in bash?

Time:03-19

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|[email protected]
21PMR07123|John|012-3456789|[email protected]
21PMR07221|Amy|017-4567890|[email protected]

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?

CodePudding user response:

With your shown samples, please try following shell script(save this program as a shell script, with appropriate permissions and then run it). Here my file's name is Input_file from where awk program reads the input.

#!/bin/bash

while true
do
   echo "Please do enter the user id which you want to match from file:"
   read value
   output=$(awk -F'|' -v id="$value" '
      $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."
      }
   }
'  Input_file)
Invalid='The student ID does not exist. Please enter again.'
   if [[ "$output" == "$Invalid" ]]
   then
        echo "NO match found, so re-asking user to enter the value now..."
   else
        echo "$output"
        exit
   fi
done

Explanation: Simple explanation would be, running an infinite while loop in bash, which runs till an exact match is found in Input_file by awk program. So basically in an infinite while loop I am running awk command(posted by OP in question itself) and then getting its output into a variable and checking if output variable is The student ID does not exist. Please enter again. then again it will run the loop and ask user to enter details ELSE it will print output value and exit from program.

CodePudding user response:

Would you please try the following:

awk -F'|' '
NR==FNR {name[$1] = $2; num[$1] = $3; addr[$1] = $4; next}
BEGIN {print "Enter student ID"}
$1 in name {
    printf "Name: %s\nContact Number: %s\nEmail Address: %s\n", name[$1], num[$1], addr[$1]
    next
}
{
     print "The student ID does not exist. Please enter again."
}
' student.txt -

It first reads the student.txt file then asks the user to input a student ID in STDIN one by one.

CodePudding user response:

#!/usr/bin/env bash

prompt='Enter an ID'
result=1
while (( result != 0 )); do
    IFS= read -p "$prompt: " -r studentID
    prompt='The student ID does not exist. Please enter again'

    awk -F'|' -v id="$studentID" '
        $1 == id {
            printf "Name: %s\nContact Number: %s\nEmail Address: %s\n", $2, $3, $4
            found = 1
            exit
        }
        END { exit !found }
    ' student.txt

    result=$?
done
  • Related