ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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...
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.
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):
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!)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.