Solved

Simple Even or Odd Script - Bourne Shell

Posted on 2011-02-16
12
388 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

943 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now