You are not logged in.

#1 2009-11-08 20:21:39

panosk
Member
From: Athens, Greece
Registered: 2008-10-29
Posts: 241

[SOLVED] problem with spaces and ls command in bash script

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

#2 2009-11-08 20:31:12

daf666
Member
Registered: 2007-04-08
Posts: 470
Website

Re: [SOLVED] problem with spaces and ls command in bash script

did u try:
ls -b

Offline

#3 2009-11-08 20:41:49

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] problem with spaces and ls command in bash script

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

#4 2009-11-08 20:47:51

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: [SOLVED] problem with spaces and ls command in bash script

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

#5 2009-11-08 20:57:55

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] problem with spaces and ls command in bash script

@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

#6 2009-11-08 21:07:59

panosk
Member
From: Athens, Greece
Registered: 2008-10-29
Posts: 241

Re: [SOLVED] problem with spaces and ls command in bash script

Procyon wrote:

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

#7 2009-11-09 03:06:33

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] problem with spaces and ls command in bash script

Or just quote that:
for file in "$(find "$HOMEDIR" -type f)"


This silver ladybug at line 28...

Offline

#8 2009-11-09 07:48:15

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] problem with spaces and ls command in bash script

Doesn't that make it try to run on one really long filename?

Offline

#9 2009-11-09 08:01:55

panosk
Member
From: Athens, Greece
Registered: 2008-10-29
Posts: 241

Re: [SOLVED] problem with spaces and ls command in bash script

Procyon wrote:

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

#10 2009-11-09 08:06:03

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] problem with spaces and ls command in bash script

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

Board footer

Powered by FluxBB