Link to home
Start Free TrialLog in
Avatar of nocturn4l
nocturn4l

asked on

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

Avatar of dmeeren
dmeeren
Flag of Netherlands image

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

Avatar of nocturn4l
nocturn4l

ASKER

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 =[
There seems to be space around equal sign on line 4.

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

And try again..
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'
ooh that took away the line error on line 4

but i'm still gettin gthe

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

if [ "$calc" -eq 0 ]
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

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
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

ASKER CERTIFIED SOLUTION
Avatar of imaki06
imaki06

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OMG IT WORKS

thank you so much
took the time to figure out my problem, was patient and great!