- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: entering charcter or numerical value then chec...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 07:22 AM
тАО06-02-2009 07:22 AM
entering charcter or numerical value then checking
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 07:29 AM
тАО06-02-2009 07:29 AM
Re: entering charcter or numerical value then checking
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 07:45 AM
тАО06-02-2009 07:45 AM
Re: entering charcter or numerical value then checking
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 07:45 AM
тАО06-02-2009 07:45 AM
Re: entering charcter or numerical value then checking
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 07:59 AM
тАО06-02-2009 07:59 AM
Re: entering charcter or numerical value then checking
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:06 AM
тАО06-02-2009 08:06 AM
Re: entering charcter or numerical value then checking
> and print out the result.
Sure sounds like homework to me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:09 AM
тАО06-02-2009 08:09 AM
Re: entering charcter or numerical value then checking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:15 AM
тАО06-02-2009 08:15 AM
Re: entering charcter or numerical value then checking
Good luck
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:19 AM
тАО06-02-2009 08:19 AM
Re: entering charcter or numerical value then checking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:20 AM
тАО06-02-2009 08:20 AM
Re: entering charcter or numerical value then checking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:23 AM
тАО06-02-2009 08:23 AM
Re: entering charcter or numerical value then checking
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:25 AM
тАО06-02-2009 08:25 AM
Re: entering charcter or numerical value then checking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:27 AM
тАО06-02-2009 08:27 AM
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.
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:31 AM
тАО06-02-2009 08:31 AM
Re: entering charcter or numerical value then checking
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]
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 08:42 AM
тАО06-02-2009 08:42 AM
Re: entering charcter or numerical value then checking
I meant to say "Yes, in principle". You see the _principal_ users here are professionals with professional needs, not students doing homework.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 09:02 AM
тАО06-02-2009 09:02 AM
Re: entering charcter or numerical value then checking
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 09:03 AM
тАО06-02-2009 09:03 AM
Re: entering charcter or numerical value then checking
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2009 10:46 PM
тАО06-02-2009 10:46 PM
Re: entering charcter or numerical value then checking
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"
}
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2009 08:23 AM
тАО06-03-2009 08:23 AM
Re: entering charcter or numerical value then checking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2009 06:35 PM
тАО06-03-2009 06:35 PM
Re: entering charcter or numerical value then checking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2009 06:37 PM
тАО06-03-2009 06:37 PM
Re: entering charcter or numerical value then checking
getline number < "/dev/stdin"
to
getline number < "-"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2009 07:39 PM
тАО06-03-2009 07:39 PM