Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Modulo calculation in shell script

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-21-2011
harjinder harjinder is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 9 February 2015, 12:23 AM EST
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 Unix and Linux 03-21-2011
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 15 June 2015, 2:01 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 Unix and Linux 03-21-2011
theboogymaster's Unix or Linux Image
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 Unix and Linux 03-21-2011
frans's Unix or Linux Image
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:31 PM.