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!
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
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 =[
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..
Change that to be this:
calc='expr $number % 2'
And try again..
ASKER
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'
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'
ASKER
ooh that took away the line error on line 4
but i'm still gettin gthe
line 5: [: too many arguments
but i'm still gettin gthe
line 5: [: too many arguments
Line 5 should be like: ( missing " ")
if [ "$calc" -eq 0 ]
if [ "$calc" -eq 0 ]
ASKER
says
line 5: [: expr $number % 2: integer expression expected
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
ASKER
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
$calc='expr $number % 2' (with the $ sign)
i get an additional msg that says
line 4: =expr $number % 2: command not found
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OMG IT WORKS
thank you so much
thank you so much
ASKER
took the time to figure out my problem, was patient and great!
Open in new window