Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

    Code:
    #!/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
    can someone help

    thanx

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by mystical dervish View Post
    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

    Code:
    #!/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
    can someone help

    thanx

    You have a space after the "=" sign, try this:

    Code:
    tmp=`expr $p1 / $p2`
    instead of:

    Code:
    tmp= `expr $p1 / $p2`

    Regards

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by Franklin52 View Post
    You have a space after the "=" sign, try this:

    Code:
    tmp=`expr $p1 / $p2`
    instead of:

    Code:
    tmp= `expr $p1 / $p2`

    Regards
    thanx
    I did so , but it doesn't work
    the same error

  4. #4
    Linux 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>


    Regards
    Last edited by Franklin52; 08-12-2007 at 02:28 PM. Reason: PS

  5. #5
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by Franklin52 View Post
    Strange, it should work, what are the values of $p1 and $p2?

    Regards
    $p1=0.0011602
    $p2=0.0006459360
    really its strange !!!!

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by mystical dervish View Post
    $p1=0.0011602
    $p2=0.0006459360
    really its strange !!!!
    Not so strange, Bash can't handle floating point calculations, use bc:

    tmp=`echo $p1 / $p2 | bc -l`

    instead of:

    tmp=`expr $p1 / $p2`

    Regards

  7. #7
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by Franklin52 View Post
    Strange, it should work, what are the values of
    <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>
    THANKS
    you are right, may be I'll put a very small valus instead of zero.
    regards

    but still have a pb of non-numeric argument

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by mystical dervish View Post
    THANKS
    you are right, may be I'll put a very small valus instead of zero.
    regards

    but still have a pb of non-numeric argument
    Have you seen my previous post?

    Regards

  9. #9
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by Franklin52 View Post
    Not so strange, Bash can't handle floating point calculations, use bc:

    tmp=`echo $p1 / $p2 | bc -l`

    instead of:

    tmp=`expr $p1 / $p2`

    Regards
    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 example
    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
    it prints out always "you are here" where it must go to else !
    is there any syntatic pb ? where I tried "$p12" > "1.5" but it doesnt help

    thanx in advance

  10. #10
    Linux 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...