[#!/bin/sh] directory with white spaces

My script doesn’t work if the directory ($bron) is with white spaces
how can i prevent this?

the goal is to copy all files of a flash card to computer over usbmass

used code:


bron='/Harddisk/Data/disk 1'

n=0
for img in `find "$bron/DCIM" -name '*.*'`
do
filename=$(basename "$img")
n=$((n+1))
qdbus $qdbusRef setLabelText "Bezig met "$filename" ($n van $numfiles)"
qdbus $qdbusRef Set org.kde.kdialog.ProgressDialog value $n
cp -a -u "$img" "$doel/$project"
done

chmod -R 777 "$doel/$project"
qdbus $qdbusRef close
exiftool -d %Y-%m-%d_%f.%%e "-filename<CreateDate" "$doel/$project"

output from terminal:

+ for img in '`find "$bron/DCIM" -name '\''*.*'\''`'
++ basename /Harddisk/Data/disk
+ filename=disk
+ n=1
+ qdbus org.kde.kdialog-4453 /ProgressDialog setLabelText 'Bezig met disk (1 van 4)'

+ qdbus org.kde.kdialog-4453 /ProgressDialog Set org.kde.kdialog.ProgressDialog value 1

+ cp -a -u /Harddisk/Data/disk '/Harddisk/Data/disk 2/2013-04-09'
cp: kan status van ‘/Harddisk/Data/disk’ niet opvragen: Bestand of map bestaat niet

bron=’/Harddisk/Data/disk\ 1’

Good luck.

No luck :frowning:

+ bron='/Harddisk/Data/disk\ 1'
+ cd '/Harddisk/Data/disk\ 1'
/usr/local/bin/usbcamera: regel 73: cd: /Harddisk/Data/disk\ 1: Bestand of map bestaat niet
+ n=0
++ find '/Harddisk/Data/disk\ 1/DCIM' -name '*.*'
find: `/Harddisk/Data/disk\\ 1/DCIM': Bestand of map bestaat niet

I’m pretty sure that the problem is with

 `find ... ` 

which won’t quote names with whitespace.

Your best chance of success is to use something like:


find [various arguments] -exec script '{}' \;

where you replace “script” by the path to your own script that does what you want done to each file.

On Tue, 09 Apr 2013 16:26:02 +0000, heelstraf wrote:

>> bron=’/Harddisk/Data/disk\ 1’
>>
>> Good luck.
>
> No luck :frowning:

In single quotes, you don’t need to escape the space (ie, don’t precede
it with “”, just "bron=’/Harddisk/Data/disk 1’

Jim

Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

The reason there is a problem isn’t because of the ‘find’ command (which
was what I incorrectly tried to fix) but because the ‘for’ loop is taking
output from ‘find’ delimited by spaces (escapes not counted). I think
nrickert’s response is probably closer to the fix than mine was.

Good luck.

On Tue, 09 Apr 2013 17:13:32 +0000, ab wrote:

> The reason there is a problem isn’t because of the ‘find’ command (which
> was what I incorrectly tried to fix) but because the ‘for’ loop is
> taking output from ‘find’ delimited by spaces (escapes not counted). I
> think nrickert’s response is probably closer to the fix than mine was.
>
> Good luck.

Ah, that’s what I get for not reading quite as closely as I thought I
had. :wink:

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Me too. :slight_smile:

Good luck.


for dir in `ls -d /Harddisk/Data/disk*`do
cd $dir
for img in `find . -name "*.*"
[INDENT=2]do[/INDENT]
[INDENT=2].....[/INDENT]
done
done

Doing this since I saw a “disk 2” in the output, but it works fine if only “disk 1” exists.

thx to everyone, found a solution with the help from here :slight_smile:


gebruiker=`whoami`
bron='/var/run/media/$gebruiker/NIKON D5000'
cd "$bron"

for img in `ls -d DCIM/*/*`
do
filename=$(basename "$img")
n=$((n+1))
qdbus $qdbusRef setLabelText "Bezig met "$filename" (Foto $n)"
qdbus $qdbusRef Set org.kde.kdialog.ProgressDialog value $n
cp -a -u "$img" "$doel/$project"
done

This does not work if name contains white spaces.