Simple Even or Odd Script - Bourne Shell

I can't for the life of me figure this out.. when I run it, I get this

line 4: calc: command not found
line 5: [: -eq: unary operator expected
negative

help please!
#!/bin/sh
echo 'enter a number:'
read number
calc = 'expr $number % 2'
if [ $calc -eq 0 ]
then
 echo even
else
 echo negative
fi

Open in new window

nocturn4lAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
imaki06Connect With a Mentor Commented:
Are those quotes on line 4 back quotes, or normal?

They should be ` not '
0
 
dmeerenCommented:
You need to put an $ in front of calc, like this:

#!/bin/sh
echo 'enter a number:'
read number
$calc = 'expr $number % 2'
if [ $calc -eq 0 ]
then
 echo even
else
 echo negative
fi

Open in new window

0
 
nocturn4lAuthor Commented:
ah that was a simple typing mistake, i should've just copied and pasted

but I did copy and paste what you wrote and i'm getting the exact same error still =[
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
imaki06Commented:
There seems to be space around equal sign on line 4.

Change that to be this:
calc='expr $number % 2'

And try again..
0
 
nocturn4lAuthor Commented:
could it be that expr doesn't work?

which I dont understand why it shouldn't.... in my terminal if i write

$ x=1
$ x='expr $x + 1'
echo $x

i don't get '2'

i get 'expr $x + 1'
0
 
nocturn4lAuthor Commented:
ooh that took away the line error on line 4

but i'm still gettin gthe

line 5: [: too many arguments
0
 
imaki06Commented:
Line 5 should be like: ( missing " ")

if [ "$calc" -eq 0 ]
0
 
nocturn4lAuthor Commented:
says

line 5: [: expr $number % 2: integer expression expected
#!/bin/sh
echo 'enter a number:'
read number
calc='expr $number % 2'
if [ "$calc" -eq 0 ]
then
 echo even
else
 echo negative
fi

Open in new window

0
 
nocturn4lAuthor Commented:
if i make line 4

$calc='expr $number % 2'          (with the $ sign)

i get an additional msg that says

line 4: =expr $number % 2: command not found
0
 
nocturn4lAuthor Commented:
ok so if i keep it simple and run it like this.. i get

enter a number:
24
expr $number % 2



as if it's not doing the calculation....if i put a $ before the $calc = 'expr ....." line it jus gives me more errors too

is my expr broken?

sigh
#!/bin/sh
echo 'enter a number:'
read number
calc='expr $number % 2'
echo $calc

Open in new window

0
 
nocturn4lAuthor Commented:
OMG IT WORKS

thank you so much
0
 
nocturn4lAuthor Commented:
took the time to figure out my problem, was patient and great!
0
All Courses

From novice to tech pro — start learning today.