[Solved] String integer comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] String integer comparison
# 1  
Old 01-31-2014
[Solved] String integer comparison

I am trying to execute something like this

Code:
file=/tmp/test.txt
firstline=$(head -n 1 $file)
value=`echo $firstline | cut -d'=' -f2`

if [ "$value" -gt "2" ]
then
echo true
fi

i read the first line of a file, cut to the numeric value in the first line and check if it greater than 2

but for some reason the "value" variable doesnt store it as integer.

I get : integer expression expected at the if loop.

cant seem to understand why? If i store a number in value like

value=3 then it works fine. How do i make sure when i do a cut it stores it as integer.

I tried

typeset -i value=`echo $firstline | cut -d'=' -f2`

but that gave me arithmatic operator error.

please advise. Thanks.

---------- Post updated at 10:16 PM ---------- Previous update was at 10:13 PM ----------

i also tried

Code:
if [ $value -gt 2 ]  without the quotes

still same error integer expression expected
# 2  
Old 01-31-2014
Can you please post a sample of the input file, so we can see why it's behaving badly?
# 3  
Old 01-31-2014
thanks for taking the time,

the file has only two lines

Code:
LoggingLevel=0
UseLogFile=No


Last edited by Scott; 01-31-2014 at 11:27 PM.. Reason: Code tags
# 4  
Old 01-31-2014
Code:
$ value=$(sed -n "/^LoggingLevel=/{s/.*=//;p;q;}" file)
$ echo $value
0

If you really must get the value from the first line of the file:
Code:
read value < file
value=${value##*=}

# 5  
Old 01-31-2014
when i do

value=$(sed -n "/^LoggingLevel=/{s/.*=//;p;q;}" file)

it does echo the value 0 from the file.

but the following if statement is still throwing the integer expression expected error

Code:
if [ "$value" -gt "2" ]
then
echo true
fi


Last edited by Scott; 01-31-2014 at 11:37 PM.. Reason: Use code tags, please...
# 6  
Old 01-31-2014
Of course, because I didn't give you anything except an alternative way to do what you were already doing! Duh! Sorry!

What shell and OS are you using (please don't say SH in Solaris, because I have no Solaris system to play with!)?
# 7  
Old 01-31-2014
i am on Linux

using korn shell

Code:
echo $SHELL
/usr/bin/ksh

my script begins with

Code:
#!/bin/sh

should it maybe change to #!/bin/ksh ? not sure if that matters.

Last edited by Scott; 01-31-2014 at 11:47 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] How to extract integer out of a string?

Input: XXX:1,XXX:3,XXX:3 Output: A=1 B=3 C=3 Below code is what i do currently. However it doesn't work out for all cases. As some times the last digit extracted is giving me error. It cant be used for arithmetic computation. Any better methods out there? If possible, can you... (9 Replies)
Discussion started by: bananamen
9 Replies

2. Shell Programming and Scripting

[Solved] String Comparison

Hi I am beginner in writing shell scripting please tell me how to compare a string in Unix shell. i have two variables in a shell script, var1="00101 00201 00301 303 401 405" var2="101 201 301" i want to compare var1 with var2 . for example if 101 from Var1 present in Var2 or not. similarly... (5 Replies)
Discussion started by: nikesh29
5 Replies

3. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

4. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

5. Programming

[Solved] how to send an integer via message queue?

how can i send an integer via message queue? i try this, but it doesn't work, child process receive 13345943 instead of 5 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/msg.h> #include <sys/ipc.h> #include <sys/sem.h> #include <errno.h> #include <unistd.h>... (2 Replies)
Discussion started by: tafazzi87
2 Replies

6. Shell Programming and Scripting

Doubt in integer comparison

Hi What is the difference between following commands Command1 length=1 if ] ; then echo "Hellow world" fi Command 2 length=1 if ] ; then echo "Hellow world" fi which is correct usage from this (2 Replies)
Discussion started by: morbid_angel
2 Replies

7. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

8. Programming

warning: comparison between pointer and integer

Hi guys :D I am still playing with my C handbook and yes, as you can see I have small problem as always :cool: I wrote a C code #include <stdio.h> #define MESSAGE 100 int main(void) { char input_mes - Pastebin.com And when I try to compile it I get following errors from gcc ... (1 Reply)
Discussion started by: solaris_user
1 Replies

9. Shell Programming and Scripting

integer comparison in ksh

Hi, I am just trying to compare integer in ksh. can you please tell me what's wrong with this code... or give me suggestions on alternative. sample code: i=0; if ; then echo inside if fi Thanks in advance! (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

10. Programming

comparison between pointer and integer

I received a warning when I tried to compile my program that said: warning: comparison between pointer and integer Could you please explain to me what this means, and in what ways I could possibly fix this? Thanks for your help! (2 Replies)
Discussion started by: sjung10
2 Replies
Login or Register to Ask a Question