Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux tar exclude在bash脚本中不起作用_Linux_Bash_Shell_Tar - Fatal编程技术网

Linux tar exclude在bash脚本中不起作用

Linux tar exclude在bash脚本中不起作用,linux,bash,shell,tar,Linux,Bash,Shell,Tar,我正在尝试创建一个文件夹的tar文件,其中有很多文件需要排除。所以我写了一个脚本(mytar): 测试文件夹: test/ notme.txt test.txt test2.txt 如果我执行脚本,它将创建一个tar文件,但不排除我在IGN中列出的文件 显然,命令是: tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test 如果命令直接在shell中执行,那么它工作得非常好。此外,我还

我正在尝试创建一个文件夹的
tar
文件,其中有很多文件需要排除。所以我写了一个脚本(
mytar
):

测试文件夹:

test/
    notme.txt
    test.txt
    test2.txt 
如果我执行脚本,它将创建一个tar文件,但不排除我在
IGN
中列出的文件
显然,命令是:

tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test  
如果命令直接在shell中执行,那么它工作得非常好。此外,我还找到了脚本的解决方法:在脚本文件中使用
bash-c

bash -c "tar --ignore-failed-read $IGN -cvf '$1' '$2'"
我在想并试图弄明白

为什么这个简单的命令在没有
bash-c的情况下不能工作?
为什么要使用
bash-c

输出:
第一次输出不应像以后的输出那样包含
notme.txt
文件


更新1脚本更新

这与bash在其shell中扩展变量的方式有关

设置时:

IGN="--exclude='notme.txt'"
它将扩展为:

tar --ignore-failed-read '--exclude='\''notme.txt'\''' -cvf test1.tar test  
因此,tar将查找一个名为
\''notme.txt'\''
的文件,但找不到该文件

您可以使用:

IGN=--exclude='notme.txt'
在shell扩展之后,将正确地解释它,tar将知道它,但我建议您使用变量仅存储要排除的文件名:

IGN="notme.txt"
tar --exclude="$IGN" -cvf ./test1.tar ./*

这与bash在其shell中扩展变量的方式有关

设置时:

IGN="--exclude='notme.txt'"
它将扩展为:

tar --ignore-failed-read '--exclude='\''notme.txt'\''' -cvf test1.tar test  
因此,tar将查找一个名为
\''notme.txt'\''
的文件,但找不到该文件

您可以使用:

IGN=--exclude='notme.txt'
在shell扩展之后,将正确地解释它,tar将知道它,但我建议您使用变量仅存储要排除的文件名:

IGN="notme.txt"
tar --exclude="$IGN" -cvf ./test1.tar ./*

在下面的命令中,单引号是语法性的(不是文字,文件名参数不是由引号围绕的文字),以防止shell在包含空格或制表符的情况下拆分参数

tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test  
最接近的方法是使用数组而不是字符串变量:

ign=( --exclude='notme.txt' )
tar --ignore-failed-read "${ign[@]}" -cvf test1.tar test  

在下面的命令中,单引号是语法性的(不是文字,文件名参数不是由引号围绕的文字),以防止shell在包含空格或制表符的情况下拆分参数

tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test  
最接近的方法是使用数组而不是字符串变量:

ign=( --exclude='notme.txt' )
tar --ignore-failed-read "${ign[@]}" -cvf test1.tar test  

我希望听到
-1
的原因,可能对我有帮助。try-IGN=--exclude='notme.txt',排除双引号。我希望听到
-1
的原因,可能对我有帮助。try-IGN=--exclude='notme.txt',排除双引号。
IGN=--exclude='notme.txt'
仅在文件名不包含空格或制表符时有效,在这种情况下引号是无用的
IGN=--exclude=notme.txt
也将起作用
IGN=--exclude='notme.txt'
仅在文件名不包含空格或制表符时起作用,在这种情况下,引号是无用的
IGN=--exclude=notme.txt也可以使用