Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-11-2012, 07:02 PM   #1
Registered: Aug 2008
Posts: 50

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"

this script gives me error while running,
what could be the workaround?
Old 09-11-2012, 07:49 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,808

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
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:


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

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013
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.

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

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


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

All times are GMT -5. The time now is 12:20 AM.

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