LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2010, 03:01 AM   #1
testac
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
convert string to int


Hi ,

Iam trying to check a value less than or equals 0.The value iam getting is after doing grep a file . but its throwing error .

Code:
VAL_5Y=`grep "VAL_5Y=" t.txt| cut -d\= -f2`
VAL_5Y=`expr ${VAL_5Y%\%}*1`
if [ "${VAL_5Y}" -eq 0 ]
When i execute this it throws
Code:
line 47: [: 516.23125*1: integer expression expected
please help
 
Old 07-06-2010, 03:51 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
if [ "${VAL_5Y}" -eq 0 ]
Quote:
line 47: [: 516.23125*1: integer expression expected
So what value is in VAL_5Y?? Assuming it is "516.23125*1", then this is clearly not an integer, hence the error message
 
Old 07-06-2010, 04:02 AM   #3
testac
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
The value in VAL_5Y is 516.23125 which iam getting after grep from a file so its stored as a String so my task is to convert the VAL_5Y to integer so that i can use in if loop . How to do that . To convert to int i tried multiplying by 1 which is displayed in error .
 
Old 07-06-2010, 04:54 AM   #4
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Rep: Reputation: 17
Pretty sure that's a floating-point number, not an integer. Difference is the decimal point (.) ... You may also need to convert it from a string but maybe not.

Matter of fact, I don't even know if you can use floats in bash scripts... You are using a bash script correct?

Maybe try it in python or perl...
 
Old 07-06-2010, 04:58 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well as this number is not an integer you would have to either truncate or round as bash doesn't do floating point easily.
My solution was to truncate like so:
Code:
VAL_5Y=516.23125
VAL_TEST=$(echo $VAL_5Y | sed 's/\..*//')

if [[ $VAL_TEST -eq 0 ]]
then
    <do blah>
else
    <do blah2>
fi
Obviously for rounding you would need to construct your test. You also have the choice of using bc but I am not suitably skilled to explain how.
 
Old 07-06-2010, 05:05 AM   #6
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Rep: Reputation: 17
Using bc.
Code:
echo "scale=4; $VAL_5Y" | bc
You might have to adapt that, I just grabbed it off google. See http://www.novell.com/coolsolutions/tools/17043.html for more info.

Also, just cause I can:
Code:
\* C Code *\
#include <stdio.h>

int main() {
  float myFloat = 516.23125 * 1;
  printf("%d", myFloat);
  return 0;
}
And then you could use argv to grab the output of grep, etc,etc.
And really the only relevent part is
Code:
float myFloat = 516.23125 * 1;

Last edited by ryan858; 07-06-2010 at 05:17 AM.
 
Old 07-06-2010, 10:22 AM   #7
testac
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
its a bash script and the number can be floating point as in this case.
my main concern is to check if number is greater than or equal to zero. surely i can try ryan and grail approach.Very high probability that the number will have left part of decimal point so surely can try out truncating post decimal and check for zero condition.
 
Old 07-06-2010, 07:39 PM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
if [ `expr $VAL_5Y / 1` -le 0 ]
..
 
Old 07-06-2010, 10:12 PM   #9
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
Code:
VAL_5Y="516.23125"
IFS='.' read int dec <<< "$VAL_5Y"
if (( dec ))
then echo "VAL_5Y is greater than 0"
elif (( int >= 0 ))
then echo "VAL_5Y is greater than or equal to 0"
else echo "VAL_5Y is NOT greater than o equal to 0"
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert int to string mohtasham1983 Programming 15 03-02-2007 06:18 PM
Convert string (C++) to int FreeDoughnut Programming 10 10-30-2006 03:26 PM
convert string to long/int alaios Programming 10 09-15-2005 09:01 AM
string to int BadTaste Programming 11 08-18-2004 10:23 AM
string to int in C h/w Programming 2 12-05-2003 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration