[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash script problem [OT?]



On 22/04/12 08:34, Cam Hutchison wrote:
Soare Catalin<lolinux.soare@gmail.com>  writes:

The script will take files or dirs as parameters and will back them up in a
presefined location, using tar. Problems arise when it will encounter files
or directories which contain spaces in their names.

then #is it an existing directory?
BK_LIST="$BK_LIST ${PARAM}"

here...

else
if [ -f "$PARAM" ]; then #is it an existing file?
BK_LIST="$BK_LIST ${PARAM}"

.... and here.

As you build up BK_LIST, you lose the ability to tell which spaces are
the ones you added, or were already in $PARAM. You end up treating all
spaces as word separators.

To fix this, you want to make BK_LIST an array:

BK_LIST=()
# or declare -a BK_LIST, but I prefer the former

Append to the array with +=

   BK_LIST+="${PARAM}"

Expand the array, preserving spaces:

   tar -cjf $BK_FULLPATH "${BK_LIST[@]}"



Or just quote the entries like

BK_LIST="$BK_LIST \"${PARAM}\""

(apologies if thi mail goes through twice, I got a bounce on my earlier attempt)

--
Dom


Reply to: