LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 07:02 PM   #1
saurin
Member
 
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"
fi

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

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 10:33 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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



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

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

All times are GMT -5. The time now is 09:15 AM.

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