Operating System - Linux
1753863 Members
7373 Online
108809 Solutions
New Discussion юеВ

Re: entering charcter or numerical value then checking

 
lcpdl87
Occasional Advisor

entering charcter or numerical value then checking

Write a shell script to find even or odd, and print out the result.

This should include 2 portions:

Ask user to input a numerical number, then the script should check if it is a character or numerical, ask user re-enter a numerical number, if character found.

If it is an numerical digit, find out even or odd, and print out the result


this is what I got:


#!bin/bash
echo -e "Please enter a numerical number --> \c"
read NUMBER

mychar=1
if echo -e "$mychar | grep [a-z]"
then
echo -e "Please re-enter a numerical number --> \c"
read NUMBER

if [ $(($number % 2)) -eq 0 ]
then
echo -e "$number is even even number"
else
echo -e "$number is an odd number"
fi


... am i doing something wrong. I'm assuming so. Any help would be awesome!
21 REPLIES 21
Mel Burslan
Honored Contributor

Re: entering charcter or numerical value then checking

your script is missing some stuff.

do you want the user to be forced to enter a numeric value or do you want him to exit the script with a non-zero return value, if he/she doesn't enter a numeric ?

depending on the answer, you either will need an endless loop to check until a numeric value was entered around the first if block, something like:

numeric_flag=0
while [ ${numeric_flag} -eq 0 ]
do
# check for alphabetic characters here
# if numeric value entered set numeric_flag to 1
done

otherwise,

if non numeric value was found

exit 1

This should be a starting point for your debugging
________________________________
UNIX because I majored in cryptology...
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

I want the user to enter a "numerical number" like it says in the directions and if they enter an "a-z" character like a charcater in the alphabetic, then it should go down to "please re-enter another number" and then if someone enters 0-9.. it reads whether the number is odd or even.

And obviously if the person enters a "0-9" on the first try, the programs read sthat and figures out if its odd or even right away.
Mel Burslan
Honored Contributor

Re: entering charcter or numerical value then checking

actually, let me re-phrase it, it has a lot of mistakes, like variables being assigned in upper case whereas getting checked in lower case. You are not using a mainframe or DOS or an archaic high level language which is case insensitive. In unix $NUMBER is not equal to $number which is not equal to $NuMbEr.

here is the script you are looking for:

r=0
while [ $r -eq 0 ]
do
clear
echo "enter numeric value"
read num
echo $num | grep -q [a-z]
r=${?}
done

result=$(($num % 2))
if [ $result -eq 0 ]
then
echo "\n\nthe number you entered was $num and it is an even number\n\n"
else
echo "\n\nthe number you entered was $num and it is an odd number\n\n"
fi
________________________________
UNIX because I majored in cryptology...
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

well,

the "result=$(($num %2))
if[$result -eq 0]

is giving me an error..

syntax error: operand expected (error token is "%2")
then..
if [-eq 0]: command not found
syntax error near unexpected then
Steven Schweda
Honored Contributor

Re: entering charcter or numerical value then checking

> Write a shell script to find even or odd,
> and print out the result.

Sure sounds like homework to me.
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

No, its extra credit, and I believe I have most of it now but I'm still getting errors.
Mel Burslan
Honored Contributor

Re: entering charcter or numerical value then checking

extra credit is still homework or course study of some sort and instead of copying someone else's work, you should be doing your own investigation. If I were to know/think about this, I would not be writing the script for you previously. I know the script above works on a hpux 11.11 machine. I can see where you are going wrong but will not tell you :) It would otherwise take the whole fun out.

Good luck
________________________________
UNIX because I majored in cryptology...
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

Well, if I wasn't stuck, I wouldn't even bother checking this site for help. It's a forum, you post your problem and people help you out. That's how a forum works doesn't it? And I've been looking through the book I was given and on the internet and I'm not finding any accurate results that could help me out in the slightest bit. All I wanted was some guidance.
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

And I'm working with Linux 10, not whatever you're talking about.