LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-18-2007, 12:16 AM   #1
SBN
Member
 
Registered: Jul 2006
Distribution: UBUNTU, CentOS, FEDORA 8
Posts: 474

Rep: Reputation: 30
Simple Shell Script Error


- hey guys i this script wont work

Quote:
x=20
y=5
z=`expr x / y`
echo $z
- the error is "expr: non-numeric argument"
 
Old 01-18-2007, 12:24 AM   #2
anotherlinuxuser
Member
 
Registered: Jan 2007
Location: Alberta Canada
Distribution: Fedora/Redhat
Posts: 63

Rep: Reputation: 19
The z=`expr x / y` line should be: z=`expr $x / $y`
Shell script vars are set without a '$', as in thisvar="somestring",
and when you use/read/display the var, you add a '$' as in: echo $thisvar
 
Old 01-18-2007, 01:08 AM   #3
SBN
Member
 
Registered: Jul 2006
Distribution: UBUNTU, CentOS, FEDORA 8
Posts: 474

Original Poster
Rep: Reputation: 30
Talking

- why i didnt think of that thank you so much
 
Old 01-18-2007, 08:59 AM   #4
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
in bash, you can also write z=$(($x / $y))
 
Old 03-06-2007, 06:33 PM   #5
anguyendapooh
LQ Newbie
 
Registered: Feb 2007
Posts: 9

Rep: Reputation: 0
let try this, it's should work
let x=20
let y=5
z=`expr $x / $y`
echo $z
 
Old 03-06-2007, 09:54 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by anguyendapooh
let try this, it's should work
let x=20
let y=5
z=`expr $x / $y`
echo $z
Don't use let; it is unnecessary and not portable.

Don't use expr for arithmetic unless you are using a Bourne shell which doesn't have internal arithmetic.
 
Old 03-14-2007, 11:14 PM   #7
tburgess1997
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Rep: Reputation: 0
Simple Shell Script Error



I also had this problem , I use CentOS 4.4 Version
I typed the script , but also had the error: {expr: non-numeric argument} from the test , but test2 which show 20(right)
Result :
expr: non-numeric argument
20

My shell script content :
---------------------------
#!/bin/sh
num1=10
num2=10
test='expr $num1 + $num2'
test2='expr 10 + 10'
$test
$test2
-----------------------------
I had tried , test3='expr $num1 + 1'
It is also error , non-numeric argument
Please help me asap!
Thks!

Last edited by tburgess1997; 03-14-2007 at 11:27 PM.
 
Old 03-15-2007, 04:41 AM   #8
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
shell command escapes

you have to use the "accent grave" aka "backtick" (`)(shift+ key next to backspace, not the single quot. mark) for inline commands (or $(command) in bash/sh/etc.), so write
Code:
#!/bin/sh
num1=10
num2=10
test=`expr $num1 + $num2` # will store the result in $test
#test=$(expr $num1 + $num2) # would be the same as above, just as example
test2='expr 10 + 10'      # will store "expr 10 + 10" in $test2
echo $test # will printout the value of $test
$test2     # will be read by the sh as expr 10 + 10, what will in turn output 20
Spot the difference between command execution in $test and storing the command in $test2 and later executing it.
(test2 is the wrong usage here, just for explanation)
The single quot. mark (', shift+#) will ignore special characters like the $ for variables,
so in your script the contents of $test will be literally "expr $num1 + $num2".
If you use double quot. marks (", shift+2) specaial chars are interpreted and the contents of $test
would be "expr 10 + 10". this would give an result wenn executed...

hope I made it clear now...
Flo


PS: The keyboard hints are for a german layout, i think, the accent key is at the same place in US layout, but not sure...
 
Old 03-15-2007, 11:19 PM   #9
tburgess1997
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Rep: Reputation: 0
Question Simple shell Script Error

Firstly, I am very grateful with "doc.nice" for helping!
Thanks very much!
But I follow your advice to make !
--------------------------------------------
[root@entextile ~]# nl /usr/local/bin/test3
1 #!/bin/sh
2 num1=10
3 num2=10
4 today='date +%Y%m%d'
5 test1=$(expr $num1 + $num2)
6 test2='(expr $num1 + $num2)'
7 test3=$(expr $today - 1)
8 $today
9 echo $test1
10 $test1
11 echo $test2
12 $test2
13 echo $test3
14 $test3
-----------------------------------------------
The result is that.......
[root@entextile ~]# /usr/local/bin/test3
expr: syntax error
20070316
20
/usr/local/bin/test3: line 10: 20: command not found
(expr $num1 + $num2)
/usr/local/bin/test3: line 12: (expr: command not found

In fact, my target mainly is that I can use "$today - 1" , this formula for backup day by day!

Last edited by tburgess1997; 03-15-2007 at 11:26 PM.
 
Old 03-15-2007, 11:35 PM   #10
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by tburgess1997
Firstly, I am very grateful with "doc.nice" for helping!
Thanks very much!
But I follow your advice to make !
--------------------------------------------
[root@entextile ~]# nl /usr/local/bin/test3
1 #!/bin/sh
2 num1=10
3 num2=10
4 today='date +%Y%m%d'
5 test1=$(expr $num1 + $num2)
6 test2='(expr $num1 + $num2)'
7 test3=$(expr $today - 1)
8 $today
9 echo $test1
10 $test1
11 echo $test2
12 $test2
13 echo $test3
14 $test3
-----------------------------------------------
The result is that.......
[root@entextile ~]# /usr/local/bin/test3
expr: syntax error
20070316
20
/usr/local/bin/test3: line 11: 20: command not found
(expr $num1 + $num2)
/usr/local/bin/test3: line 13: (expr: command not found

In fact, my target mainly is that I can use "$today - 1" , this formula for backup day by day!
Then you have to store the output of the command, not the command itself, in $today:
Code:
today=`date +%Y%m%d`
Or:
Code:
today=$(date +%Y%m%d`)
Or you can expand the command inside the assignment:
Code:
 test3=$(expr $( $today ) - 1)
If your shell has the $( ... ) syntax for command substitution, then it also has built-in shell arithmetic, and you don't need to use expr (which will make your script much slower):
Code:
test1=$(( $num1 + $num2 ))
 
Old 03-16-2007, 03:46 AM   #11
tburgess1997
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Rep: Reputation: 0
Simple Shell Script Error Reply to Thread

Thanks for all support and helping!
I will try that, Let me give an indeed grateful of you!
 
Old 03-19-2007, 06:18 PM   #12
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
thanks for the compliments,

oh not to forget a recent thread with the same problem:
if you use gnu date, try this:

today=`date -d "- 1 days" +%Y%m%d`

in my sample script, the lines
Code:
test2='expr 10 + 10'      # will store "expr 10 + 10" in $test2
$test2     # will be read by the sh as expr 10 + 10, what will in turn output 20
where only for demonstration what happend with your old code, the explanations by cfaj are absolutely correct.
(except the line: today=$(date +%Y%m%d`) there is a ` too much!)

HTH,
Flo
 
  


Reply

Tags
bash, command substitution, expression, math, shell script, solution


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't seem to run a simple shell script error command not found each time SimonT Linux - Newbie 5 10-29-2006 06:31 AM
simple shell script sycamorex Linux - Newbie 2 03-16-2006 06:00 PM
simple shell script sharpie Programming 9 06-02-2004 11:47 PM
Very Simple Shell Script cli_man Linux - Software 5 04-24-2003 10:14 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM


All times are GMT -5. The time now is 06:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration