problem with expr command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers problem with expr command
# 1  
Old 12-10-2006
problem with expr command

Smilie
hi Unix gurus,
Pls consider the following piece of code
str='hello'
length=echo $str|wc -c
echo $length
y= ` expr \( 80 - $length \) `
echo $y
Smilie
The last echo stmt is displaying 0 as the result.
If i put direct value like 6 instead of $length in i 3rd stmt it is giving the correct result.
I came to know that $length is not getting accessed inside of expr command.


any help pls.


cheers
Ravi Raj Kumar
# 2  
Old 12-10-2006
You are not assigning value to variable length correctly, try using backquotes there as well
Code:
length=`echo $str|wc -c`

# 3  
Old 12-10-2006
Another way of doing it (bash)

Code:
str='hello'
length=${#str}
y=$(( 80 - $length ))

# 4  
Old 12-11-2006
Smilie
hi Ripat,
Thank u for ur reply.The method u suggested is working fine.
But i want it to be worked in the following way.

str='hello'
length=`echo $str|wc -c`
echo $length
y= ` expr \( 80 - $length \) `
echo $y




cheers
Ravi Raj Kumar
# 5  
Old 12-11-2006
this will work.

str='hello'
length=`echo $str|wc -c`
echo $length
y=`expr 80 - $length`
echo $y
# 6  
Old 12-11-2006
try this
Code:
y=`expr 23 - $length`

# 7  
Old 12-11-2006
Smilie

Fine the above code is working.
But i wonder that ,why its not giving result,if we put parantheses.
we must to put parantheses in arithmetc expressions na.
suppose we have a long expression like 10 + 20 -30 + 40 *2 ,then will the above technique works Smilie



any help pls

cheers
Ravi Raj Kumar Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with expr command in shell script

Hi, I have used expr command to increment the date. for e.g., case 1 : echo $(date -d $(echo `expr 20010101 + 1`)) it returns Tue Jan 2 00:00:00 IST 2001 case 2: echo $(date -d $(echo `expr 20010101 - 1`)) it returns date: invalid date `20010100' please suggest me, how to... (3 Replies)
Discussion started by: nanthagopal
3 Replies

2. Shell Programming and Scripting

expr command help

I'm trying to check if a variable'd string is only one character and use that in an if statement the only way I could find is: $expr "${var}" : . # expr STRING : regrep where the "." is the grep wildcard for any single character. Whats wrong with my code here and is there a... (3 Replies)
Discussion started by: Tewg
3 Replies

3. Shell Programming and Scripting

expr command

Hi Can anyone explain me the usage of this command and the arguments used here and what will be the expected output : v_num=`expr nav_d_20100204_1759 : '*\(*\)'` what will be the value returned in v_num. Thanks in Advance!!! Regards Naveen Purbia (3 Replies)
Discussion started by: trying_myluck
3 Replies

4. Shell Programming and Scripting

Expr problem and other basic problems

Hello, I am new to the Bash scripting language, and was given a tutorial page on how to setup a file. However I am trying to use cygwin to run this file and it is not working. $ vi averagetime.sh # # # echo "Enter Dictorinary File Text " read dict echo "Enter Grid Name" read grid... (13 Replies)
Discussion started by: killerqb
13 Replies

5. Shell Programming and Scripting

Expr strange problem to me

Hi all, Please help me solve below issue. expr 04170000000 + 1 gives me -124967295 and offcourse I want this to be 04170000001 and it happens for some sort of number like some other 02300000000 02600000000 03800000000 I guess after exceeding certain range it is converting it somewhere... (2 Replies)
Discussion started by: Revansing
2 Replies

6. UNIX for Dummies Questions & Answers

using the expr command

Hi friends how can i execute expr $va1 * $var2 provided i m not supposed to use '/' also the nglob variable is turned off. (4 Replies)
Discussion started by: ashishj
4 Replies

7. UNIX for Dummies Questions & Answers

expr problem

Hi, in my ksh script expr 22 / 10 results as 2 but the actual result expected in 2.2. how do i get that result. Please help Thanks, (4 Replies)
Discussion started by: kotasateesh
4 Replies

8. Shell Programming and Scripting

expr problem

Hi, in my ksh script expr 22 / 10 results as 2 but the actual result expected in 2.2. how do i get that result. Please help Thanks, (2 Replies)
Discussion started by: kotasateesh
2 Replies

9. UNIX for Dummies Questions & Answers

expr command

hi guys.... i hava a command expr... where i m adding a value in a loop like Tc=`expr $Tc\+ $l` where Tc is declred as a variable and every time l contains a new vaue if Tc =0 initially and l =2 Tc should be equal to 0+ 2 and then l = 4 Tc = 2+4 and dispaly as 6 but after... (5 Replies)
Discussion started by: madhu_aqua14
5 Replies

10. UNIX for Dummies Questions & Answers

expr command

I am looking for the correct syntax on the expr command in UNIX. I have a script that I am building at the moment. the script is creating file1 that is an actual .sql file that is going inside the oracle database to get some information in there. It take that information, puts it inside another... (2 Replies)
Discussion started by: wolf
2 Replies
Login or Register to Ask a Question