Solved

Simple Even or Odd Script - Bourne Shell

Posted on 2011-02-16
12
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert OverPunch 25 67
Changing $PATH variable - OS X 2 80
Psexec - a script to use Psexec to copy a folder from one server to multiple PC's? 7 169
EXCHANGE 2007, SCRIPTS 67 88
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

733 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