Modulo calculation in shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Modulo calculation in shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-21-2011
harjinder harjinder is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 23 May 2013, 6:39 AM EDT
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 03-21-2011
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
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 ]

Sponsored Links
    #3  
Old 03-21-2011
theboogymaster's Avatar
theboogymaster theboogymaster is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 14 March 2014, 4:52 AM EDT
Posts: 56
Thanks: 7
Thanked 7 Times in 7 Posts
When you compare values in bash use the (-eq) thing with the integers, and the (==) if you have strings...
    #4  
Old 03-21-2011
frans's Avatar
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
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)
Sponsored Links
Closed 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
shell scripting and programmming harjinder Shell Programming and Scripting 1 03-01-2011 05:27 AM
shell scripting and programmming harjinder Shell Programming and Scripting 1 02-04-2011 12:48 AM
shell scripting and programmming harjinder Shell Programming and Scripting 10 02-02-2011 04:02 AM
Call Shell scripting from Perl Scripting. anupdas Shell Programming and Scripting 2 02-12-2010 07:07 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM



All times are GMT -4. The time now is 03:04 AM.