LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 03-02-2014, 05:28 PM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 186
Blog Entries: 1

Rep: Reputation: Disabled
How to compare a variabale with 5 to less than itself in bash script


Code:
if [ $min -lt $min-5 ] || [ $min -gt $min+5 ]; then
   ...
fi
What change is necessary to correct it?

Last edited by massy; 03-02-2014 at 05:29 PM.
 
Old 03-02-2014, 05:49 PM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 65
I assume you must be testing for rollover, otherwise $min would never be less than $min-5.

You can replace $min-5 with $((min-5)).
 
1 members found this post helpful.
Old 03-02-2014, 08:43 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,770

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Maybe just use the correct construct:
Code:
if (( min < ( min - 5) || min > ( min + 5 ) )); then
Although as bash can only deal with integers, I fail to see how this test would ever be true?
 
1 members found this post helpful.
Old 03-02-2014, 09:04 PM   #4
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 65
Quote:
Originally Posted by grail View Post
..., I fail to see how this test would ever be true?
If you have a 64-bit machine, try this (with 15 zeros after the '8'):
Code:
declare -i min
min=0x8000000000000000
echo "min = $min,  min-5 = $((min-5))"
This is what I referred to as "rollover".
 
Old 03-03-2014, 03:47 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,770

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
hmmm ... I will concede that this example works as depicted, but would suggest it is unlikely this is what the user was testing. Also if working that near the edge of the max possible value, I
would suggest that bash is the wrong choice and you should be using a language with better number handling skills
 
  


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
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 07:14 PM
[SOLVED] Bash script to compare 2 files owner ph88 Programming 3 11-17-2011 08:37 PM
bash script: compare two group list Dr_Death_UAE Linux - General 7 09-03-2009 08:32 AM
Want to compare strings in bash script IsharaComix Programming 6 10-28-2008 09:49 PM
need help with bash script to compare file sizes rimvydazas Programming 3 04-03-2008 07:18 AM


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