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

Re: Bash script problem [OT?]



On Sun, Apr 22, 2012 at 10:25 PM, Dom <toyer@rpdom.net> wrote:
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



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: [🔎] 4F945B2C.3090307@rpdom.net" target="_blank">http://lists.debian.org/[🔎] 4F945B2C.3090307@rpdom.net


Thank you everyone for replying, but unfortunately, nothing seems to work for the moment, although all the answers appear to make sense.
First, the array solution appears to work, but when tar gets all the parameters, they become a long string without spaces :), obviously "cannot stat: No such file or directory".

I then tried to add a space char at the BK_LIST increment line, but that didn't work. Enclosing them in quotes also failed.


Also tried Dom's suggestion (thank you Dom!) but then, tar won't be able to stat any file! (and this I actually can't explain to myself why)  :)

So again, thank you very much for your responses, but the only thing in my mind for the moment is that I really have to go back to the abs-guide (or perhaps find another one?) -- bottom line is that there's so many things I've forgot/not used for anything else than to follow a tutorial...

--
Regards,
Catalin Soare


Reply to: