problem with spaces in filename

I have written a script to run ddrescue on a list of files.

#!/bin/bash
#
# A script to rescue data recursively using ddrescue.

srcDir=/damaged/hdd/movies/              #the source directory
desDir=/new/hdd/movies/                 #the destination directory
workDir=/new/hdd/movies/ddrescue        #location to store logs and working files

#preamble: I have already run rsync to copy files, but some failed part way through.

`rsync -rvn --size-only $srcDir* $desDir | sed "/speedup/d" | sed "/bytes\/sec/d" | sed "/sending\ incremental/d" | sed "/^$/d" > $workDir/work` #use rsync to generate list of partially transferred files.

workLength=`cat $workDir/work | wc -l`        #Find number of files in list.  For use with incrementing.
bookmark=0                    #Begin at first entry in list.

while [ $bookmark -lt $workLength ]
do
    bookmark=`expr $bookmark + 1`        #increment through the list
    currJob=`cat $workDir/work | sed -n ''$bookmark'p' | sed 's/\ /\\\ /g'`    #extract nth filename from list, replace spaces with "\ "
    echo "Start Rescue on " $currJob >> $workDir/runlog    #

    from=`echo $srcDir$currJob`        #creates full filename for source file,
    to=`echo $desDir$currJob`        #destination file,
    log=`echo $workDir/ddlog-$bookmark`    #and ddrescue log file.
    
    `ddrescue $from $to $log > $workDir/ddoutput-$bookmark` #run ddrescue, dumping output to file.
    cat $workDir/ddoutput-$bookmark | grep -B 2 Finished | grep errors >> $workDir/runlog    #read last 3 lines from ddrescue output and record it in runlog, so user can check which files are missing bits.
    echo -e "\n"
done
exit

My problem is that ddrescue does not like the filenames I am giving it. If the filename has spaces in it it says

ddrescue: too many files

but if it has no spaces I get

ddrescue: cannot open input file: No such file or directory

I have tried using "quotes" instead of \'ing every space, but no luck. If I replace the command 'ddrescue' with 'echo', run the script, and then manually copy the given filenames into ddrescue in another terminal it works fine. But this rather defeats the purpose..

I have only been learning scripting for a little while, so I feel like I must be missing something obvious, I just can't figure out what!

Hi always put double quotes around variable references. For example:

ddrescue "$from" "$to" "$log"

As an aside, there are some superfluous cats and echoes:
For example instead of

from=`echo $srcDir$currJob`

one could write:

from=$srcDir$currJob

or instead of:

cat $workDir/work | wc -l
wc -l < "$workDir/work"
1 Like

Hi Scrutinizer. Thanks for your reply, it really helped! My script is working now. I post the working script here as an example for anyone with a similar problem. I have implemented Scrutinizers suggestions and made some other changes as well.

#!/bin/bash
#
# A script to rescue data recursively using ddrescue.

srcDir=/damaged/hdd/movies/              #the source directory
desDir=/new/hdd/movies/                 #the destination directory
workDir=/new/hdd/movies/ddrescue        #location to store logs and working files

#preamble: I have already run rsync to copy files, but some failed part way through.

`rsync -rvn --size-only $srcDir* $desDir | sed "/speedup/d" | sed "/bytes\/sec/d" | sed "/sending\ incremental/d" | sed "/^$/d" > $workDir/work` #cut rsync's list to filenames only.

workLength=`wc -l < "$workDir/work"`     #Find number of files in list.  For use with incrementing.
bookmark=0                #Begin at first entry in list.

while [ $bookmark -lt $workLength ]
do
    bookmark=`expr $bookmark + 1`        #increment through the list
    currJob=`cat $workDir/work | sed -n ''$bookmark'p'`    #extract filename to work on
    FnScurrJob=`cat $workDir/work | sed -n ''$bookmark'p' | sed "s/\//--/g" | sed "s/\ /-/g"`    #creates a version of the file path suitable for use as a file name by replacing / with --
    echo "Start Rescue on " $currJob >> $workDir/runlog    

    from=$srcDir$currJob        
    to=$desDir$currJob
    log=$workDir/ddlog-$bookmark--$FnScurrJob.txt
    
    ddrescue "$from" "$to" "$log" | tee $workDir/ddoutput-$bookmark--$FnScurrJob.txt
    echo `cat $workDir/ddoutput-$bookmark--$FnScurrJob.txt | grep -B 2 Finished | grep errors` >> $workDir/runlog
done
exit