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 06: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 05:36 AM
How to write config shell script to pass variables in master shell script? dba1981 Shell Programming and Scripting 1 04-15-2015 07:14 AM
How to write Config shell script to pass variables in master shell script? dba1981 UNIX for Dummies Questions & Answers 1 04-15-2015 03:18 AM
Unable to pass shell script variable to awk command in same shell script Ashunayak Shell Programming and Scripting 2 01-27-2014 01:04 PM
How to do simple date (time) calculation in shell script? nbsparks Shell Programming and Scripting 7 09-16-2013 06:59 PM
Numeric calculation in shell scripting Jewel Shell Programming and Scripting 23 04-21-2013 05:21 PM
Math calculation over shell jamie_123 Shell Programming and Scripting 5 05-21-2012 04:37 AM
time calculation in ksh script Bperl1967 Shell Programming and Scripting 5 09-14-2011 05:20 PM
Date calculation script impish Shell Programming and Scripting 7 03-23-2011 12:55 PM
KSH SHELL: problem calculation number of lines inside compressed file GERMANICO Shell Programming and Scripting 3 01-27-2011 01:03 PM
calculation using awk or shell script in between the numbers cdfd123 Shell Programming and Scripting 1 07-12-2010 01:53 PM
Arithmetic calculation on real numbers in Bourne Shell Script Tirmazi Shell Programming and Scripting 5 07-07-2010 05:01 PM
Script math calculation superHonda123 Shell Programming and Scripting 12 06-22-2010 12:23 PM
Partitioning script for rescue mode (disk size calculation) pug123 Shell Programming and Scripting 1 07-29-2008 03:05 PM
script execution time calculation johnsonbryce Shell Programming and Scripting 9 02-25-2006 01:33 AM


All times are GMT -4. The time now is 09:17 PM.

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