Solved

Simple Even or Odd Script - Bourne Shell

Posted on 2011-02-16
12
391 Views
Last Modified: 2012-05-11
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
Comment
Question by:nocturn4l
  • 8
  • 3
12 Comments
 
LVL 8

Expert Comment

by:dmeeren
ID: 34913643
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
 

Author Comment

by:nocturn4l
ID: 34913673
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
 
LVL 3

Expert Comment

by:imaki06
ID: 34913691
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.

 

Author Comment

by:nocturn4l
ID: 34913694
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
 

Author Comment

by:nocturn4l
ID: 34913702
ooh that took away the line error on line 4

but i'm still gettin gthe

line 5: [: too many arguments
0
 
LVL 3

Expert Comment

by:imaki06
ID: 34913723
Line 5 should be like: ( missing " ")

if [ "$calc" -eq 0 ]
0
 

Author Comment

by:nocturn4l
ID: 34913734
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
 

Author Comment

by:nocturn4l
ID: 34913742
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
 

Author Comment

by:nocturn4l
ID: 34913755
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
 
LVL 3

Accepted Solution

by:
imaki06 earned 500 total points
ID: 34913770
Are those quotes on line 4 back quotes, or normal?

They should be ` not '
0
 

Author Comment

by:nocturn4l
ID: 34913774
OMG IT WORKS

thank you so much
0
 

Author Closing Comment

by:nocturn4l
ID: 34913776
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question