You are not logged in.
I am going mad with a bash script I am trying to finish. The ls command is driving me mad with spaces in path names. This is the portion of my script that is giving me trouble:
HOMEDIR="/home/panos/Web Site"
for file in $(find "$HOMEDIR" -type f)
do
if [ "$(dateDiff -d $(ls -lh "$file" | awk '{ print $6 }') "$(date +%F)")" -gt 30 ];
then echo -e "File $file is $(dateDiff -d $(ls -lh "$file" | awk '{ print $6 }') "$(date +%F)") old\r" >> /home/panos/scripts/temp;
fi
done
The dateDiff() function is defined earlier and the script works fine when I change the HOMEDIR variable to a path where there are no spaces in directory and file names. I have isolated the problem to the ls command, so a simpler code sample that also doesn't work correctly with path names with spaces is this:
#!/bin/bash
HOMEDIR="/home/panos/test dir"
for file in $(find "$HOMEDIR" -type f)
do
ls -lh "$file"
done
TIA
Last edited by panosk (2009-11-08 21:55:31)
Offline
did u try:
ls -b
Offline
It's not ls that's at fault, but your use of $() and find. $() or ``, IIRC, changes all newlines to spaces when capturing output, so there's no way to know where the end of the filename really is.
Try something like
find "$HOMEDIR" -type f | xargs ls -lh
Offline
function doStuff{
if [ "$(dateDiff -d $(ls -lh "$1" | awk '{ print $6 }') "$(date +%F)")" -gt 30 ];
then echo -e "File $1 is $(dateDiff -d $(ls -lh "$file" | awk '{ print $6 }') "$(date +%F)") old\r" >> /home/panos/scripts/temp;
fi
}
find "$HOMEDIR" -type f -exec doStuff {} \;
Untested.
Offline
@scragar: find won't be able to execute the bash function, you would have to use -exec bash -c 'file={} ... rest of code' \;
But it looks like it's just going wrong at the for loop so try IFS=$'\n' at the start of the script.
Offline
But it looks like it's just going wrong at the for loop so try IFS=$'\n' at the start of the script.
Thank you so much. It worked!
Offline
Or just quote that:
for file in "$(find "$HOMEDIR" -type f)"
This silver ladybug at line 28...
Offline
Doesn't that make it try to run on one really long filename?
Offline
Doesn't that make it try to run on one really long filename?
Yes, that's the result. Besides, I had already tried it.
Offline
oops, brain fart. *flushes with embarrassment*
-- Edit --
BTW, for this kind of thing, I usually do something like:
find "$HOMEDIR" -type f | while read file ; do something with "$file" ; done
Or put those in an array:
IFS=$'\n' ; files=($(find "$HOMEDIR" -type f)) ; unset IFS
for file in "${files[@]}" ; do something with "$file" ; done
The later method is useful when elements of "${files[@]}" will be used multiple times across the script.
Last edited by lolilolicon (2009-11-09 08:13:07)
This silver ladybug at line 28...
Offline