Variable dash expansion doesn't work in some cases

This work is done on a test virtual machine

In my / root directory I created the following:
"/ Root / Foo"
"/ Root / bar"
"/ root / i has multiple words"

Here is the (relevant) code I currently have

  if [ ! -z "$BACKUP_EXCLUDE_LIST" ]
  then
    TEMPIFS=$IFS
    IFS=:
    for dir in $BACKUP_EXCLUDE_LIST
    do
      if [ -e "$3/$dir" ] # $3 is the backup source
      then
          BACKUP_EXCLUDE_PARAMS="$BACKUP_EXCLUDE_PARAMS --exclude='$dir'"
      fi    
    done
    IFS=$TEMPIFS
  fi


  tar $BACKUP_EXCLUDE_PARAMS -cpzf  $BACKUP_PATH/$BACKUP_BASENAME.tar.gz -C $BACKUP_SOURCE_DIR $BACKUP_SOURCE_TARGET

      

This is what happens when I run my script with sh -x

+ IFS=:
+ [ -e /root/foo ]
+ BACKUP_EXCLUDE_PARAMS= --exclude='foo'
+ [ -e /root/bar ]
+ BACKUP_EXCLUDE_PARAMS= --exclude='foo' --exclude='bar'
+ [ -e /root/i have multiple words ]
+ BACKUP_EXCLUDE_PARAMS= --exclude='foo' --exclude='bar' --exclude='i have multiple words'
+ IFS=  

# So far so good

+ tar --exclude='foo' --exclude='bar' --exclude='i have multiple words' -cpzf /backup/root/daily/root_20130131.071056.tar.gz -C / root
tar: have: Cannot stat: No such file or directory
tar: multiple: Cannot stat: No such file or directory
tar: words': Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

# WHY? :(

      

Verification succeeds but --exclude='i have multiple words'

does not work.

Keep in mind that it works when I manually type it in my shell:

tar --exclude='i have multiple words' -cf /somefile.tar.gz /root

      

I know this will work in bash when using arrays, but I want it to be POSIX.

Is there a solution for this?

+3


source to share


2 answers


Let's consider these scripts; ("with spaces" and "example.desktop" are sample files)

#!/bin/bash

arr=("with whitespace" "examples.desktop")

for file in ${arr[@]}
do
    ls $file
done

      

It outputs exactly the same as yours;

21:06 ~ $ bash test.sh 
 ls: cannot access with: No such file or directory
 ls: cannot access whitespace: No such file or directory
 examples.desktop

      



You can set IFS with '\ n' to avoid spaces in filenames.

#!/bin/bash

arr=("with whitespace" "examples.desktop")

(IFS=$'\n';
    for file in ${arr[@]}
    do
        ls $file
    done
)

      

the output of the second version should be:

21:06 ~ $ bash test.sh 
 with whitespace
 examples.desktop

      

+1


source


David H. of the LinuxQuestions forums pointed me in the right direction.

First of all, in my question, I did not use IFS =: until the end until the tar command Second, I included "set -f" for security



BACKUP_EXCLUDE_LIST="foo:bar:i have multiple words"

# Grouping our parameters
if [ ! -z "$BACKUP_EXCLUDE_LIST" ]
then
  IFS=:         # Here we set our temp $IFS
  set -f        # Disable globbing
  for dir in $BACKUP_EXCLUDE_LIST
  do
    if [ -e "$3/$dir" ]  # $3 is the directory that contains the directories defined in $BACKUP_EXCLUDE_LIST
    then
      BACKUP_EXCLUDE_PARAMS="$BACKUP_EXCLUDE_PARAMS:--exclude=$dir"
    fi    
  done
fi

# We are ready to tar

tar $BACKUP_EXCLUDE_PARAMS \
  -cpzf  "$BACKUP_PATH/$BACKUP_BASENAME.tar.gz" \
  -C "$BACKUP_SOURCE_DIR" \
  "$BACKUP_SOURCE_TARGET"
unset IFS       # our custom IFS has done it job. Let unset it!
set +f          # Globbing is back on

      

I do not recommend using the TEMPIFS variable as I did, because this method does not set IFS correctly. Your best bet is to disable IFS when you're done with it.

0


source







All Articles