Error with Expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error with Expression
# 1  
Old 06-11-2010
Error with Expression

Hi All,

am trying to add two Inputs i.e., $1 and $2,

Code
Code:
#!/bin/sh
$num_sum=`expr $1+$2`
echo "Sum = $num_sum"

am giving statement
Code:
sh arithemetic.sh 20 10

am getting error

Code:
arithemetic.sh: line 2: =20+10: command not found
Sum =

I want Output as

Code:
Sum = 30,

Can any one please help me in this,

Thanks,
Sunil N

Last edited by pludi; 06-11-2010 at 04:59 AM.. Reason: code tags, please...
# 2  
Old 06-11-2010
Code:
$num_sum=`expr $1+$2`

You don't need a $ in front of variable when assigning data to a variable.
# 3  
Old 06-11-2010
Thanks, but am getting output as Sum = 20 + 10,

How to resolve this,

I have given all the below three, but in Vain,

Code:
$num_sum=`expr $1 + $2'
$num_sum="expr $1 + $2"
$num_sum='expr $1 + $2'

Thanks,

Last edited by pludi; 06-11-2010 at 04:59 AM.. Reason: code tags, please...
# 4  
Old 06-11-2010
How about ...

Code:
sum=$(($1+$2))

# 5  
Old 06-11-2010
Yes that's working, how to make it work by using expr, can you please let me know,

Thanks,
# 6  
Old 06-11-2010
Code:
sum=$( expr $1 + $2 )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Observing error :syntax error in expression

#!/bin/bash a1="04:29:39 - System health check failed" i=$1 echo "a $((a$i))" The script above gives the following error $113> sh tryt.sh 1 tryt.sh: line 6: 04:29:39 - System health check failed: syntax error in expression (error token is ":29:39 - System health check failed") It... (1 Reply)
Discussion started by: ashima jain
1 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Shell Programming and Scripting

Getting error in this expression: value of value

HI , I have a variable "TYPE" which may contain different values. and another variable $TYPE"_SOURCE" I am using ${$TYPE"_SOURCE"} to get the value TYPE_SOURCE for eg. TYPE=ABC ABC_SOURCE=/abc/xyz On using above command I am getting error : BAD SUBSTITUTION eval is not installed... (2 Replies)
Discussion started by: aashish.sharma8
2 Replies

4. Shell Programming and Scripting

if: expression syntax error in gawk

I'm pretty new to shell scripting, but I am catching on quick. I did see one of the stickied threads about the csh, and I think this is relevant, but I don't understand enough to make a decision based on it. So as you'll see below, I use the |csh pipe, and if that is not correct, I'm certainly... (2 Replies)
Discussion started by: macman104
2 Replies

5. Shell Programming and Scripting

Error: integer expression expected

root@server01 # df -h | grep /tmp | awk {'print $3}' 252M root@server01 # root@server01 # cat /usr/local/tmpchk.sh #!/bin/sh x=`df -h | grep /tmp | awk {'print $3}'` if ; then rm -fr /tmp/somefolder/ else echo "its small" (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

6. Shell Programming and Scripting

if expression syntax error

#! /bin/csh set umr=UMR foreach i ( `ls`) set file_nm=$i set bh_nm=`echo $file_nm | cut -d"_" -f2` if($bh_nm !=$umr) then { set bh_ext=`echo $file_nm | cut -d"_" -f4` set bh_num_nm="$bh_nm $bh_ext a .txt" mv $file_nm $bh_num_nm } ... (1 Reply)
Discussion started by: jdsignature88
1 Replies

7. Shell Programming and Scripting

One more expression syntax error

HI again, still working on the assignment, which is really hard given we just started unix 4 weeks ago. This script should change the permission for the user depending if its x, w or r, to the opposite. When i try to run it, I am getting expression error. Can you spot where the problem is? I really... (3 Replies)
Discussion started by: morava
3 Replies

8. Shell Programming and Scripting

integer expression expected error

I'm a beginner so I might make beginner mistakes. I want to count the "#define" directives in every .C file I get the following errors: ./lab1.sh: line 5: ndef: command not found ./lab1.sh: line 6: #!/bin/sh for x in *. do ndef = 'grep -c \#define $x' if ; then ... (2 Replies)
Discussion started by: dark_knight
2 Replies

9. UNIX for Advanced & Expert Users

Regular Expression Error in AWK

I have a file "fwcsales_filenames.txt" which has a list of file names that are supposed to be copied to another directory. In addition to that, I am trying to extract the date part and write to the log. I am getting the regular expression error when trying to strip the date part using the "ll"... (1 Reply)
Discussion started by: madhunk
1 Replies

10. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies
Login or Register to Ask a Question