3

这项工作正在测试 virtualbox 机器上完成

在我的 /root 目录中,我创建了以下内容:
“/root/foo”
“/root/bar”
“/root/i have multiple words”

这是我目前拥有的(相关)代码

  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

这就是我使用 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? :(

检查成功完成,但--exclude='i have multiple words'不起作用。

请注意,当我在 shell 中手动键入它时它确实有效:

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

我知道这在使用数组时可以在 bash 中使用,但我希望它是 POSIX。

有针对这个的解决方法吗?

4

2 回答 2

1

考虑这个脚本;('with whitespace' 和 'example.desktop' 是示例文件)

#!/bin/bash

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

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

这与您的输出完全相同;

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

您可以将 IFS 设置为 '\n' 字符以转义文件名上的空格。

#!/bin/bash

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

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

第二个版本的输出应该是;

21:06 ~ $ bash test.sh 
 with whitespace
 examples.desktop
于 2013-01-31T19:32:48.313 回答
0

David the H. from the LinuxQuestions forums steered me in the right direction.

First of all, in my question, I did not make use IFS=: all the way through to the tar command Second of all, I included "set -f" for safety

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's job. Let's unset it!
set +f          # Globbing is back on

I advise against using the TEMPIFS variable, like I did, because that method does not set the IFS back correctly. It's best to unset IFS when you are done with it

于 2013-02-01T19:37:10.177 回答