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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-19-2010, 02:00 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4
Rep:
|
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?
|
|
|
|
02-19-2010, 02:11 PM
|
#2
|
|
Senior Member
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279
|
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 ...
|
|
|
|
02-19-2010, 03:48 PM
|
#3
|
|
LQ Newbie
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4
Original Poster
Rep:
|
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..
|
|
|
|
02-19-2010, 04:13 PM
|
#4
|
|
Member
Registered: Feb 2004
Location: Milton, WA
Distribution: Fedora
Posts: 436
Rep:
|
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.
|
|
|
|
02-19-2010, 04:21 PM
|
#5
|
|
LQ Newbie
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4
Original Poster
Rep:
|
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"
|
|
|
|
02-19-2010, 05:08 PM
|
#6
|
|
Member
Registered: Feb 2004
Location: Milton, WA
Distribution: Fedora
Posts: 436
Rep:
|
To see what is going on in your script, you may want to echo $STARTDATE, $ENDDATE and $filedate.
|
|
|
|
02-19-2010, 05:14 PM
|
#7
|
|
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,796
|
Run the script with sh -x yourscript.
whatever the problem is, it will become obvious.
jlinkels
|
|
|
|
02-19-2010, 06:24 PM
|
#8
|
|
LQ Newbie
Registered: Feb 2010
Distribution: CentOS 5.3
Posts: 4
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:09 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|