LinuxQuestions.org
Help answer threads with 0 replies.
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 03-22-2014, 04:58 PM   #1
hilltownboy
Member
 
Registered: Jan 2008
Location: Ashfield, MA
Distribution: Debian 8 "Jessie", Arch, PCLinux
Posts: 104

Rep: Reputation: 15
Bash script to compare months


For a novice something simple can be hard. In a shell script I have introduced a variable MONTH="date +%m" which prints as the number of the present month, for example 03 because we are in March. I can't match it to another number to determine whether it is equal to or not equal to in an if/else comparison.

That's because the 03 is apparently not a number but a string. Or it is a number that won't work because it begins with a zero.

Is there a way around this?
 
Old 03-22-2014, 05:30 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,412

Rep: Reputation: Disabled
There are probably lots of ways to handle that scenario. You could try using expr to get rid of the leading zero:
Code:
newvar=$(expr 0 + $oldvar)
 
Old 03-22-2014, 07:11 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
it would be a good idea to show us your full code..

Bur you do indeed have a string

Code:
echo $MONTH
Now try this

Code:
MONTH=$(date +%m)

Edit;
Try to avoid FULLCAPs in variables
Full caps are normally for system environment variables

Then the echo..

http://mywiki.wooledge.org/BashGuide
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-1.html

Last edited by Firerat; 03-22-2014 at 07:13 PM.
 
Old 03-23-2014, 08:34 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
It is likely that you are using a string comparison instead of numeric. For instance:
Code:
if [ 03 == 3 ]; then echo true; else echo false; fi
is a string comparison. A correct comparison for numbers is:

Code:
if [ 03 -eq 3 ]; then echo true; else echo false; fi
 
Old 03-23-2014, 02:10 PM   #5
hilltownboy
Member
 
Registered: Jan 2008
Location: Ashfield, MA
Distribution: Debian 8 "Jessie", Arch, PCLinux
Posts: 104

Original Poster
Rep: Reputation: 15
[SOLVED} Bash script to compare months

My problem turns out to be embarrassingly stupid.
It appeared to me that jpollard's code for a string comparison "doesn't work". Seemingly comparison of two 3s reports as false. But that is because one is 03 and the other is 3, i.e., a string is compared to a number.
With the second 3 written as 03 same as the first one, the comparison "works", reporting as true.

Thanks for your help and patience.
 
Old 03-23-2014, 07:43 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Nearly everybody does it... even experienced users.

What is worse is that during basic testing it will work most of the time... Only when leading 0s get into it does it fail immediately. And if the code has worked for some days/weeks, it takes a while to track down the bug in a script that may be a thousand lines long...

That and getting quoting right was what drove me to write scripts in Perl - especially when it was longer than about 30-50 lines.
 
  


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
[SOLVED] How to compare a variabale with 5 to less than itself in bash script massy Programming 4 03-03-2014 03:47 AM
Bash script compare numbers in a file hayko Programming 11 09-18-2012 05:56 PM
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 07:14 PM
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 06:31 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