Operating System - Linux
1822552 Members
2941 Online
109642 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.
Mel Burslan
Honored Contributor

Re: entering charcter or numerical value then checking

Then maybe, instead of blindly posting questions in other UNIX platforms' forums, like HPUX here, you might want to try some linux forum. Huh ?
________________________________
UNIX because I majored in cryptology...
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

and extra credit is not homework. it's something you choose to do if you more points to your grade and its OPTIONAL. it's not actual homework. homework is required to get the grade. I'm just trying to do this to boost my grade up alittle.
James R. Ferguson
Acclaimed Contributor

Re: entering charcter or numerical value then checking

Hi:

> 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.

Yes, in principal. However, this community is intended to be for professionals dealing with professional problems, not for homework questions. You are more than welcome to lurk and learn.

Regards!


...JRF...
OldSchool
Honored Contributor

Re: entering charcter or numerical value then checking

"No, its extra credit, ..." that's homework

the version Mel posted works just fine w/ ksh and sh (after adding quotes around the []), has a few issues w/ echo in bash and dash

it appears that you missed some whitespace around the "%", but its hard to tell in the posts

original was:
...
result=$(($num % 2))
if [ $result -eq 0 ]
....


you posted:
...
the "result=$(($num %2))
if[$result -eq 0]
...
James R. Ferguson
Acclaimed Contributor

Re: entering charcter or numerical value then checking

Hi:

I meant to say "Yes, in principle". You see the _principal_ users here are professionals with professional needs, not students doing homework.

...JRF...

OldSchool
Honored Contributor

Re: entering charcter or numerical value then checking

there are a variety of shell tutorials available. parker's site is useful, as is shelldorado.

one thing I would caution you about is learning scripting on linux (ie. bash) and then attempting to use unix. the "linuxy" gnu extensions that you will come to expect simply won't be there.

ksh tends to port well from unix to linux, and visa versa.
Dennis Handly
Acclaimed Contributor

Re: entering charcter or numerical value then checking

>I'm working with Linux 10

(I'll ask the moderators to move it there with your other thread.)

>OldSchool: original was:
>result=$(($num % 2))

There is no need to use "$" within (( )) or $(( )).
(( result = num % 2 ))

In fact, you can replace the original by:
if (( number % 2 == 0 )); then

ghostdog74
Occasional Advisor

Re: entering charcter or numerical value then checking

#!/bin/bash
awk 'BEGIN{
while(1){
printf "Enter a numerical number: "
getline number < "/dev/stdin"
if (number+0==number){
print "yes, a number"
break
}else{
print "no, not a number. Enter again"
}
}
print "number is " number
if ( number %2 == 0 ){
print "Number "number" is even"
}else{
print "Number "number" is odd"
}
}'
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

I tested that. That makes it say there's no file or directory for the file.
ghostdog74
Occasional Advisor

Re: entering charcter or numerical value then checking

it works fine for me.
ghostdog74
Occasional Advisor

Re: entering charcter or numerical value then checking

try changing

getline number < "/dev/stdin"

to

getline number < "-"
lcpdl87
Occasional Advisor

Re: entering charcter or numerical value then checking

Thank you SO MUCH! That was what was wrong with it. I changed the wording a bit so suit it for myself but other than that it works great. Thank you again!