Cannot test variable within Range in Bash using CentOS release 5.3 (Final)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ...
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..
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.