Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Modulo calculation in shell script

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-21-2011
Modulo calculation in shell script

hi
i am writing a progrm to print the even numbers and the code which i am following is as follows

Code:
#!/usr/bin/bash
echo "enter a number a"
read a
if [$a % 2 == 0]
then
  echo "the number is even"
else
  echo "the number is odd"
fi

~
what is the mistake i am doing ...please tell me

Last edited by pludi; 03-21-2011 at 05:28 AM.. Reason: Please use code tags
# 2  
Old 03-21-2011
Can you try as below.
Note: Please use code tags [c ode] ... . . [/code] when posting codes,script,data etc
Code:
if [ $a%2 -eq 0 ]

# 3  
Old 03-21-2011
When you compare values in bash use the (-eq) thing with the integers, and the (==) if you have strings...
# 4  
Old 03-21-2011
Could be written (with arithmetic evaluation and array)
bash code:
  1. #!/usr/bin/bash
  2. parity=(even odd) # parity[0]='even' parity[1]='odd'
  3. read -p "enter a number: " a
  4. echo "the number is ${parity[$((a%2))]}
The Following User Says Thank You to frans For This Useful Post:
fpmurphy (03-21-2011)
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Newbie question: modulo operator with negative operand, bug or feature? FleetFoot Shell Programming and Scripting 11 08-12-2015 04:36 AM
How to do simple date (time) calculation in shell script? nbsparks Shell Programming and Scripting 7 09-16-2013 05:59 PM
Numeric calculation in shell scripting Jewel Shell Programming and Scripting 23 04-21-2013 04:21 PM
Math calculation over shell jamie_123 Shell Programming and Scripting 5 05-21-2012 03:37 AM
time calculation in ksh script Bperl1967 Shell Programming and Scripting 5 09-14-2011 04:20 PM
Date calculation script impish Shell Programming and Scripting 7 03-23-2011 11:55 AM
KSH SHELL: problem calculation number of lines inside compressed file GERMANICO Shell Programming and Scripting 3 01-27-2011 12:03 PM
calculation using awk or shell script in between the numbers cdfd123 Shell Programming and Scripting 1 07-12-2010 12:53 PM
Arithmetic calculation on real numbers in Bourne Shell Script Tirmazi Shell Programming and Scripting 5 07-07-2010 04:01 PM
Script math calculation superHonda123 Shell Programming and Scripting 12 06-22-2010 11:23 AM


All times are GMT -4. The time now is 04:34 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password