我的tar命令可从命令行运行,但不能在bash脚本中运行

时间:2018-08-11 15:13:36

标签: linux bash tar

我正在尝试从bash脚本运行tar命令,但似乎无法正常工作。特别是,我试图从tar命令中排除一些文件/目录,但出现错误。当我从命令行运行单个tar命令时,它起作用了!

我的脚本:

#!/bin/bash

EXCLUDE_ITEMS=("file2.txt" "dir1/dir2" "dir 4/file 5.txt")
LOGS="/home/mark/Personal/Mark/2018/Debbie/log.txt"
# Create the list of excluded files and directories
for item in "${EXCLUDE_ITEMS[@]}"
do
    EXCLUDE="$EXCLUDE --exclude=\"$item\""
done
# Remove the old log file
rm "$LOGS"
# Set up the destination dir
rm -r target_debbie
mkdir target_debbie
# Start the transfer
cd source_debbie
{
echo "starting in "; pwd
echo "tar vvcf - .${EXCLUDE} | (cd /home/mark/Personal/Mark/2018/Debbie/target_debbie; tar xpf -)"
tar vvcf - .${EXCLUDE} | pv | (cd /home/mark/Personal/Mark/2018/Debbie/target_debbie; tar xpf -)
} >>${LOGS} 2>>${LOGS}

我用于测试的源树:

source_debbie/
├── dir1
│   └── dir2
│       └── file3.txt
├── dir3
│   └── file4.txt
├── dir 4
│   ├── file 4.txt
│   └── file 5.txt
├── file1.txt
└── file2.txt

运行脚本后,目标树:

target_debbie/
├── dir1
│   └── dir2
│       └── file3.txt
├── dir3
│   └── file4.txt
├── dir 4
│   ├── file 4.txt
│   └── file 5.txt
├── file1.txt
└── file2.txt

但是,应该排除以下文件/目录:“ file2.txt”,“ dir1 / dir2”和“ dir 4 / file 5.txt”。

tar的输出(即我的日志文件):

starting in 
/home/mark/Personal/Mark/2018/Debbie/source_debbie
tar vvcf - . --exclude="file2.txt" --exclude="dir1/dir2" --exclude="dir 4/file 5.txt" | (cd /home/mark/Personal/Mark/2018/Debbie/target_debbie; tar xpf -)
drwxrwxr-x mark/mark         0 2018-08-11 07:29 ./
-rw-rw-r-- mark/mark         0 2018-08-11 07:29 ./file2.txt
drwxrwxr-x mark/mark         0 2018-08-11 07:29 ./dir3/
-rw-rw-r-- mark/mark         0 2018-08-11 07:29 ./dir3/file4.txt
-rw-rw-r-- mark/mark         0 2018-08-11 07:29 ./file1.txt
drwxrwxr-x mark/mark         0 2018-08-11 07:29 ./dir 4/
-rw-rw-r-- mark/mark         0 2018-08-11 07:29 ./dir 4/file 5.txt
-rw-rw-r-- mark/mark         0 2018-08-11 07:29 ./dir 4/file 4.txt
drwxrwxr-x mark/mark         0 2018-08-11 07:29 ./dir1/
drwxrwxr-x mark/mark         0 2018-08-11 07:29 ./dir1/dir2/
-rw-rw-r-- mark/mark         0 2018-08-11 07:29 ./dir1/dir2/file3.txt
tar: 4/file: Cannot stat: No such file or directory
tar: 5.txt": Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

如果我从命令行单独运行此命令:

tar vvcf - . --exclude="file2.txt" --exclude="dir1/dir2" --exclude="dir 4/file 5.txt" | (cd /home/mark/Personal/Mark/2018/Debbie/target_debbie; tar xpf -)

我在target_debbie中得到此输出,这是正确的:

target_debbie/
├── dir1
├── dir3
│   └── file4.txt
├── dir 4
│   └── file 4.txt
└── file1.txt

我的脚本中缺少什么?

谢谢

标记

0 个答案:

没有答案