[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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

0
nocturn4l
Asked:
nocturn4l
  • 8
  • 3
1 Solution
 
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
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
imaki06Commented:
Are those quotes on line 4 back quotes, or normal?

They should be ` not '
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now