【发布时间】:2019-03-18 17:53:12
【问题描述】:
我有 bash 脚本 my_tar.sh,它在 3 个文件上调用 tar czf output.tgz,文件名空间从数组传递:file、file 2 和 file 3。
#!/bin/bash
declare -a files_to_zip
files_to_zip+=(\'file\')
files_to_zip+=(\'file 2\')
files_to_zip+=(\'file 3\')
echo "tar czf output.tgz "${files_to_zip[*]}""
tar czf output.tgz "${files_to_zip[*]}" || echo "ERROR"
虽然存在三个文件,但当在脚本中运行tar 时,它会以错误结束。但是,当我在 bash 控制台中运行 echo 输出(与 my_tar.sh 的下一个命令相同)时,tar 运行正常:
$ ls
file file 2 file 3 my_tar.sh
$ ./my_tar.sh
tar czf output.tgz 'file' 'file 2' 'file 3'
tar: 'file' 'file 2' 'file 3': Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
ERROR
$ tar czf output.tgz 'file' 'file 2' 'file 3'
$
有什么想法吗?
【问题讨论】:
-
看看
declare -p files_to_zip -
@Cyrus 哦,我明白了。这:@ and * 解释了我的困惑