Results 1 to 10 of 10
Hello,
By Shell script, I get a substring from string, where the substring is surely a number, but I can't do mathematical expression on those variables that have the substring
...
- 08-12-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 15
convert string into number by shell
Hello,
By Shell script, I get a substring from string, where the substring is surely a number, but I can't do mathematical expression on those variables that have the substring
the code is
can someone helpCode:#!/bin/sh p1=`grep -w lala lev_prob.txt | awk '{z=$2}END{if($z=="") print "0"; else print z}'` p2=`grep -w lala irq_prob.txt | awk '{z=$2}END{if($z=="") print "0"; else print z}'` echo $p1$p2 // it prints well echo "------------" tmp= `expr $p1 / $p2` // gives error says expr: non-numeric argument echo $tmp // no out
thanx
- 08-12-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 08-12-2007 #3Just Joined!
- Join Date
- May 2007
- Posts
- 15
- 08-12-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Strange, it should work, what are the values of $p1 and $p2?
<EDIT>
PS:
p2=`grep -w lala irq_prob.txt | awk '{z=$2}END{if($z=="") print "0"; else print z}'`
If this is a zero, you'll divide $p1 by zero........
</EDIT>
RegardsLast edited by Franklin52; 08-12-2007 at 02:28 PM. Reason: PS
- 08-12-2007 #5Just Joined!
- Join Date
- May 2007
- Posts
- 15
- 08-12-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 08-12-2007 #7Just Joined!
- Join Date
- May 2007
- Posts
- 15
- 08-12-2007 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 08-12-2007 #9Just Joined!
- Join Date
- May 2007
- Posts
- 15
cheer, it works well, thank you.But I have got another problem that I don't know if its related to what you have solved
when I check the value of tmp in a condition it's always true whereas its not
for exampleit prints out always "you are here" where it must go to else !Code:p1=$(grep -w $line lev_prob.txt | awk '{z=$2}END{if($z=="") print "0.000001"; else print z}') p2=$(grep -w $line irq_prob.txt | awk '{x=$2}END{if($x=="") print "0.000001"; else print x}') p3=$(grep -w $line eyr_prob.txt ......so on p12=`echo $p1 / $p2 | bc -l` p13=`echo $p1 / $p3 | bc -l` echo $p12" " $p13" // prints out 1.315218339 .88775928139 if [ $p12 > 1.5 -a $p13 > 1.5 ] then echo "you are here" else echo "not true" fi
is there any syntatic pb ? where I tried "$p12" > "1.5" but it doesnt help
thanx in advance
- 08-12-2007 #10Linux User
- Join Date
- Jun 2007
- Posts
- 318
Check you current directory, you should find a file named 1.5. That's what the '> 1.5' is doing. To get it to work, change the if statement to:
if [[ $p12 > 1.5 ]] && [[ $p13 > 1.5 ]]
But that won't always work because the if statement is comparing strings, not numbers. You'd have to use the bc utility to do the test.
Also put a 'set -vx' at the beginning of the script and it'll show what it's doing.
Vic


Reply With Quote
