0

##Sorry for my bad English

Hello I am writing a script which ask for employee to add records at option 3 in a sequence: Phone number, Family name, given name, department number, job title etc.. My question is When I enter the correct phone number the script does not ask me family name or anything else and exits.. I want this code to ask for my family name, given name..... after validating correct phone number and so on.

the code is given below

#!/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
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Try http://shellcheck.net/ before asking for human assistance, and try to reduce your problem to a [mre]. It's not at all clear which part of the code isn't working like it should, and it's quite likely that some or all of the problems are due to simple quoting errors which Shellcheck will be happy to point out. See also [When to wrap quotes around a shell variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Oct 07 '20 at 04:48
  • Does it exit, or does it keep asking for a phone number over and over? – Gordon Davisson Oct 07 '20 at 04:59
  • @GordonDavisson sorry my mistake it keep asking phone number over and over. – user3260225 Oct 08 '20 at 07:24

2 Answers2

0

The easiest solution that i see is to add a counter in each while loop.

-- Code --

cnt=0
while [ $cnt -lt 1]:
do
  #all-the-code-for-adding-record
  cnt=(( $cnt + 1 ))
done

or secondly, add all the execution statements into a function definition and call them from the main loop,

-- Improved --

function my_func() {
  # all-executions
}

function other_func() {
  # other-executions
}

read -p "please-input-option" option
case $option in
1)
  my_func()
;;
2)
  other_func()
;;
echo "invalid case"
esac
Cpreet
  • 148
  • 1
  • 8
0

The problem is that the prompt for a phone number is in a loop that has no exit condition:

while :
do
    read -p "Phone number (xxxxxxxx): " PhoneNumber
# Validate phone number
    if [ -z "$PhoneNumber" ]
    ......
    else
    echo $PhoneNumber >> records
    fi
done

The while : means the loop condition will never be false, meaning the loop will only exit if break (or something similar) is run inside it. I think you want to add break after that echo, so the loop exits after a phone number is successfully recorded. You have essentially the same problem in a number of other loops.

You also have a sort-of-opposite problem in the deletion section: it checks whether the given phone number is valid and exists, but proceeds to the "Confirm deletion" loop whether or not it was valid.

Also, it looks like rather than creating a new entry by writing a single line with all the new employee's info, it writes each piece of info on a separate line. I think you want to accumulate all the info about a new employee, then use a single echo (or printf might be better -- but it's more complicated to use correctly) to write the employee's info as a single line.

I'd also recommend replacing most/all of the format checking with regular expression tests. For example, you could replace this:

if [ -z "$fon" ] || ! [ "$fon" -eq "$fon" ] 2> /dev/null || [ ! ${#fon} -eq 8 ] || [[ ! ${fon:0:1} == "9" ]]

with

phoneformat='^9[0-9]{8}$'    # Regex for a "9" followed by 8 more digits
if [[ "$fon" =~ $phoneformat ]]

Finally, there are a bunch of places were you don't have variable references properly quoted. I recommend shellcheck.net to point out this and other common mistakes.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151