jeudi 1 décembre 2016

how can i force my code to not accept an interger for the user name section. and viceversa for the age?

I am stuck on the name section. I do not want the user to be able to input numbers when I ask for the name. and same for the age.

not sure how to code that if anyone has any suggestions

#!/bin/bash #User input script

#Variables
again=""
name=""
age=''
occupation=""
y=401K
answer=""



#Begin loop
while [ "$again" != "n" ]
do

#find out the user name
    echo -e "What is your name: "
    read -r name
        if [[ $name == [a-z] ]]
        then
            echo "Ha! A name contains letters, TRY AGAIN!"
        else    
            echo "Hey $name"
            echo "You have ${#name} letters in your name"
        fi
#find out the user age
    echo -e "What is your age: "
    read -r age
    echo "Nice, you are $age years old"
#if they are over 18, Ask if they have a 401K                            
    if  [ $age -lt 18 ]
    then
        echo "Stay in school"
    else
        echo -e "Do you have a 401(k) y or n: "
        read  K401         

        if [ ${K401} ==  "y" ]
        then
            echo "Great"
        else
            echo " you should get one"
        fi
    fi                                               

#find out user occupation
    echo -e "What is your occupation"
    read -r occupation
    echo "Oh cool, you are a $occupation"

#see if they are happy with their job
    echo -e "Do you enjoy being a $occupation, answer y or n: "
    read -r answer
    echo "You said $answer.....That's cool"

#Ask if they want to do it again
    echo -e "Do you want to do it again: y or n: "
    read again
done

exit 0   

Aucun commentaire:

Enregistrer un commentaire