LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-11-2012, 08:02 PM   #1
saurin
Member
 
Registered: Aug 2008
Posts: 49

Rep: Reputation: 0
shell script how to compare long integer


I want to compare long integer in shell script, but when i use
if [ $value -gt 1000000000 ]; then
echo "Warning: xxxxxxx"
fi

this script gives me error while running,
what could be the workaround?
 
Old 09-11-2012, 08:49 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I just tried that code with much larger numbers, and it works fine---what error are you getting?

Note: If you are running it as a script, you'll need the first statement---eg for bash:

#!/bin/bash

Also, how does the variable "value" get set?
 
Old 09-12-2012, 11:33 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.


And yes, please give us some more context for the operation. Are you using bash, or another shell? What is the exact error? What exactly does the $value variable contain? Can you show us more of the code surrounding this?


If you're using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr

Code:
if (( value > 1000000000 )); then
	echo "Warning: xxxxxxx"
fi

Do note also that the shell can only do integer math. Floating point operations need to be handled with an external tool like bc or awk.

See here for too for more on shell arithmetic:
arithmetic expressions
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to find given number is integer or not in shell script kvmreddy Linux - Newbie 2 08-15-2009 09:05 AM
how to get integer part from string in Shell Script sunilvadranapu Programming 4 11-02-2008 05:51 AM
How to Compare Two files using shell script pooppp Linux - Networking 14 08-05-2008 04:35 AM
shell script: compare 2 files anhtt Programming 6 08-29-2007 03:39 AM
Shell-Script check if $xx is of type integer d-fens Programming 5 07-11-2003 09:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration