Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Can you give example for a script where user is asked to enter name, surname and age. And then check if name and surname contains only letters and age contains only digits? And if the information is correct it is added to file.

share|improve this question
2  
Is this homework? –  Karlson Mar 24 '12 at 17:48
    
Needless to say.. it is not. –  user646263 Mar 24 '12 at 17:50
2  
I wouldn't necessarily say that it's "needless to say" since it certainly smells like it could be homework. –  Dason Mar 24 '12 at 17:57
    
Can you be sure, that a name like "O'Reilly" has to be excluded, "Mc Intosh", "Müller-Smørebrød"? –  user unknown Mar 24 '12 at 22:44

3 Answers 3

This sounds like homework, so nobody should write the script for you.

Instead, I would suggest learning how to use these two commands:

share|improve this answer
1  
also will be usefull information about tests (tldp.org/LDP/abs/html/tests.html ) in bash. –  rush Mar 24 '12 at 19:29

Use echo to display a message, and the read to read a line of input. These are shell builtins, so consult your shell's manual for more information.

echo "What is your name?"
read -r name
echo "What is your age?"
read -r age

To validate the age, check whether it contains characters other than a digit. The only portable built-in shell construct that allows this is case (some shells have extensions for regexp matching).

case $age in
  *[!0-9]*) echo 1>&2 "Your age must consist of digits only."; exit 2;;
esac

To validate the name, check whether it contains non-printable characters. (Note that names can contain pretty much arbitrary characters; also, not everybody has a “surname”: treat a name as an arbitrary string.)

case $name in
  *[![:print:]]*) echo 1>&2 "Your name must consist of printable characters only."; exit 2;;
esac
share|improve this answer

If it's not homework I would suggest looking into using PERL or Python and simply have the PERL read like this:

$name = <STDIN>;
chomp $name;
($forename, $surname) = split(<regex>, $name);
if($surname =~ /<regex>/) execute something

Python would be similar so to continue just look at PERL regex. Script is not exact but you get the idea.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.