LinuxQuestions.org
Review your favorite Linux distribution.
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 02-19-2010, 02:00 PM   #1
erclinux
LQ Newbie
 
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4

Rep: Reputation: 0
Cannot test variable within Range in Bash using CentOS release 5.3 (Final)


Hello All,
For the past few months I have been working on my first linux project. I have been googling and searching forum after forum for all my questions and have almost always found the best answers here... so now that I am officially stumped, my first question is the following: (I will try to be as detailed as possible)

In this particular script I am trying to search a directory and create a report. The files in this directory, each begin with a timestamp followed by a name and separated with a "." (for example: 1263358800.name.ext)

Here is a snippet of the problem area:

allfiles="`find /path/to/directory -maxdepth 1 -type f`"
for file in $allfiles
do
allfilesarray[$A]=${file##/*/}
let "A = $A + 1"
done

for file in ${allfilesarray[*]}
do
filedate="`echo "$file" | cut -d'.' -f1`"
if [ "$filedate" -ge "$STARTDATE" ] && [ "$filedate" -le "$ENDDATE" ]
then
withinrange[$B]=${file}
let "B = $B + 1"
else
echo "File out of Range"
fi
done



If I echo the $filedate, $STARTDATE and $ENDDATE they are all correct. However when I run the script all files are shown as "File out of Range", meaning that the script doesnt recognize the comparisons in my "if" statement. The variables are integers so I am using "-ge" and "-le". but for some reason I cant shake the "File out of Range" phenomenon.

Can anyone help?
 
Old 02-19-2010, 02:11 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Why have you quoted the variables ? Surely it will treat anything between quotes as a string and obviously $filedate is not an integer and neither is $STARTDATE. So it does what it can and drops through to the else clause.
But I'm no bash magician ...
 
Old 02-19-2010, 03:48 PM   #3
erclinux
LQ Newbie
 
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for your quick reply smoker, I have tried without quotations and i get the same result. I have actually read that variables "Should" generally always be in quotes. Is this wrong?

I have also tried curly's such as ${filedate} or ${STARTDATE}. These are all things I have tried from reading other posts from this forum as well as others, but script still doesn't seem to work...

Any other ideas??

Last edited by erclinux; 02-19-2010 at 03:50 PM. Reason: mispelling..
 
Old 02-19-2010, 04:13 PM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
The quoting you use should not be a problem. Variables inside double quotes are replaced with their stored value. Single quoted strings do not behave the same. So, '$var' would echo $var, but "$var" would echo the value stored in the var variable. Using double quotes ensures that stored values containing spaces will be treated as a single value, preventing word splitting. Curly braces are for when your variable names are used without white space surrounding them. For instance, $ab. If 'a' is the variable name, bash will not see it, unless you enclose the variable within curly braces: ${a}b. Referencing array values also requires curly braces, as your code above shows. Your quoted variables in your if test should not be a problem, so long as they contain integer values. The test still works with or without the quotes.
 
Old 02-19-2010, 04:21 PM   #5
erclinux
LQ Newbie
 
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4

Original Poster
Rep: Reputation: 0
penguiniator, thanks for that great reply! That was very informative and i will keep it under my belt from now on

Unfortunately it doesn't solve my problem though

I just cant figure out why the script wont see that "$filedate" is between "$STARTDATE" and "$ENDDATE"
 
Old 02-19-2010, 05:08 PM   #6
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
To see what is going on in your script, you may want to echo $STARTDATE, $ENDDATE and $filedate.
 
Old 02-19-2010, 05:14 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Run the script with sh -x yourscript.

whatever the problem is, it will become obvious.

jlinkels
 
Old 02-19-2010, 06:24 PM   #8
erclinux
LQ Newbie
 
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4

Original Poster
Rep: Reputation: 0
jlinkels,

I dont know if words can explain what just happenned! It felt like the universe opened up and shone pillars of light on me annointing me with the knowledge of time or something...

For the record, the snippet i posted here works great. Believe it or not its another part of the script that is the problem..

Thanks jlinkels for the great tip! This forum is the best.

cheers,
E
 
  


Reply

Tags
bash, centos, range, variable



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 - Test variable against a range spartiati Programming 3 01-29-2010 01:35 AM
Problems running ant on CentOS release 5.2 (Final) paulstanyer Linux - Newbie 0 12-19-2008 10:57 AM
install opensuse release candidate upgrade path to final release newbuyer17 Linux - General 2 06-05-2008 08:23 AM
CentOS release 4.6 (Final) - Kernel Oops harry edwards Linux - Server 2 03-17-2008 05:11 PM
BASH : how to act based on test against a range of numbers hollywoodb Programming 4 07-06-2006 06:09 PM

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

All times are GMT -5. The time now is 03:01 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
Open Source Consulting | Domain Registration