Skip to main content

Sponsors

BASH Problems: Files with Spaces

Previous script xman_dos2unix in xman utility (latest) under GPL has problems on files or directories with space.

Simple work around with double quotes:

for file in "$@" ; do
...
done

Note that all references to $file requires double quotes too.

Another tedious work around with IFS and double quotes:

I use IFS=$'\n', and double quote references to files such as "${file}" to solve the space problems.

OLDIFS=$IFS
IFS=$'\n'
for file in $@ ; do
        # FIXME: I should have put these into a subroutine to reduce code duplications in explore().
        # However, I'm not familiar with subroutines in BASH yet.
        # echo "$file"
        IFS=$OLDIFS
        if [ -d "$file" ] ; then
                echo "Entering $file"
                pushd "$file" > /dev/null
                explore
                echo "Leaving $file"
                popd
        elif [ "`${FILE} \"${file}\" | ${GREP} text`" != "" ] ; then
                $DOS2UNIX "$file"
        fi
        IFS=$'\n'
done
IFS=$OLDIFS

I have seen xarg used in

I have seen xarg used in these cases too.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.