LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-15-2005, 01:14 PM   #1
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Rep: Reputation: 15
Question Even numbers


Hi !

I have been trying to write a shell script to print even numbers and odd numbers but I get lost with the modulus and how to write the expression. Is there someone who could demonstrate an example of printing even or odd numbers ?

Thank you in advance
 
Old 05-15-2005, 01:38 PM   #2
juanbobo
Member
 
Registered: Mar 2005
Location: Chicago
Distribution: Gentoo AMD64
Posts: 365

Rep: Reputation: 30
If the (number % 2) is > 0, then the number is odd, else it is even.
 
Old 05-15-2005, 02:42 PM   #3
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Original Poster
Rep: Reputation: 15
The problem I have with this shell script is to write the correct syntax. I know that in order to get my even numbers I have to divide them by % 2 to see if the remaining is zero but then to write it I get lost with bash script.

Help!
 
Old 05-15-2005, 04:28 PM   #4
juanbobo
Member
 
Registered: Mar 2005
Location: Chicago
Distribution: Gentoo AMD64
Posts: 365

Rep: Reputation: 30
This is probably the best tutorial on Bash, you should be able to find any answers there. I'd like to help you more, I don't know what problems you are having in particular.

http://www.tldp.org/LDP/abs/html/
 
Old 05-15-2005, 04:31 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
How about this for syntax:
Code:
rossy - trigger - Sun May 15 21:32:42
~> num=4
rossy - trigger - Sun May 15 21:32:47
~> if [ $num -eq $(($num/2*2)) ];then echo $num is even;else echo $num is odd;fi
4 is even
rossy - trigger - Sun May 15 21:32:48
~> num=3
rossy - trigger - Sun May 15 21:32:50
~> if [ $num -eq $(($num/2*2)) ];then echo $num is even;else echo $num is odd;fi
3 is odd
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer Numbers in C Mercurius Programming 24 10-20-2005 10:54 AM
random numbers deveraux83 Programming 4 05-17-2005 03:26 AM
what do the numbers mean? drisay Slackware 1 12-14-2004 08:30 AM
54 digit numbers Scratchit General 3 06-23-2004 10:24 AM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 10:18 AM


All times are GMT -5. The time now is 06:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration