LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-17-2009, 05:23 PM   #1
ic0n
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
Am I misunderstanding IF...THEN in bash script?


Hi

I think this script should list the directories that are newer than the date of somefile:

#Check the time against somefile
ORIGINAL_IFS=$IFS
IFS=$'\n'
for i in $(find . -type d); do
newer=$(expr `stat -c %Y "$i"` \> `stat -c %Y somefile`)
if [ $newer ]; then
echo "$i"
echo $newer
fi;
done
IFS=$ORIGINAL_IFS

To my way of thinking, the line starting 'newer=...' should evaluate to 'true' or 'false' (or the numerical equivalent) and then the if statement should only print the name of the directory (and the value of 'newer', included for diagnostic purposes) if 'newer' is 'true'.

Except that it prints *all* the directories, even though the value of 'newer' shows as '1' or '0'.

Obviously some noob howler in there somewhere, but I'm stuck. Can you help?

Cheers
John
 
Old 10-17-2009, 05:42 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I think that your problem is that both strings '0" and '1' are treated by bash as true. Try
Code:
if [[ $newer -eq 0 ]]
Replacing [ <test expression> ] with [[ <test expression> ]] is just "good practice". The latter is the GT-racing, souped-up version of the former, overcoming some deficiencies so is generally preferred. The Advanced Bash Scripting Guide's "Example 7-1. What is truth?" section is relevant to your interests.
 
Old 10-17-2009, 05:50 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Bash also has specific operators to compare the modification date of files, which you may find helpful.

from man bash:
Code:
       file1 -nt file2
              True  if  file1  is  newer (according to modification date) than
              file2, or if file1 exists and file2 does not.
       file1 -ot file2
              True if file1 is older than file2, or if file2 exists and  file1
              does not.
 
Old 10-18-2009, 03:49 PM   #4
ic0n
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks!

I edited the script like this:

#Check the time against somefile
ORIGINAL_IFS=$IFS
IFS=$'\n'
for i in $(find . -type d); do
if [ "$i" -nt somefile ]; then
echo "$i"
fi;
done
IFS=$ORIGINAL_IFS

Which at last produces the result I need. I'll briefly explain what I'm trying to do, just in case anyone has any observations: I'm trying to write a script which I can run against a 'tree' of flac files, which will create a mirror tree into which I can convert the flac files as .ogg, using oggenc. Each time I run it, I only want it to pick up directories and flac files added since the last time the script was run.

I could have converted them all by hand in the time it's taken me to get the code fragment above working, but that's not the point, is it?!

Now on to the next bit...
[edit to add - this board doesn't like me indenting my code - it keeps left aligning it!]
Cheers
John
 
Old 10-18-2009, 04:16 PM   #5
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,497
Blog Entries: 14

Rep: Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328
Just for reference, no need for a script, find has an option for that. You can use

find . -type d -newer somefile.
 
Old 10-18-2009, 05:19 PM   #6
ic0n
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Stone the crows! How does anyone recall all the possible parameters of every command?

Anyhow, after that false start, I now have this doing, apparently, exactly what I want the first part to do in a single line:

find ./ -type d -newer somefile -exec mkdir -p "../ogg/{}" \;

I've tested this by 'touching' somefile, and then creating a directory - I get a matching directory in my 'ogg tree'.

It seems possible that the script might end up being 3 lines long - the one above, another find command with -exec oggenc [something] and then touch somefile.

It couldn't be that easy, could it?

Cheers
John
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Learning Bash, potential bug or total misunderstanding regarding globbing. crispyleif Linux - Newbie 16 02-07-2009 02:32 PM
IPTABLES Misunderstanding davidz Linux - Security 5 08-30-2007 09:39 AM
Misunderstanding Logical Drives chess380 Linux - Newbie 11 01-10-2007 04:06 AM
select - a misunderstanding jhon Programming 3 09-08-2004 12:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:47 AM.

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