【发布时间】:2019-07-16 06:40:24
【问题描述】:
我在这里阅读了answer,但仍然有错误。
在我的文件夹中,我只想处理*.gz 文件,Windows 10.tar.gz 文件名中有空间。
假设文件夹包含:
Windows 10.tar.gz Windows7.tar.gz otherfile
这是我的shell脚本,我尝试了所有用“”引用的东西,仍然无法得到我想要的。 crypt_import_xml.sh
#/bin/sh
rule_dir=/root/demo/rule
function crypt_import_xml()
{
rule=$1
# list the file in absoulte path
for file in `ls ${rule}/*.gz`; do
echo "${file}"
#tar -xf *.gz
#mv a b.xml to ab.xml
done
}
crypt_import_xml ${rule_dir}
这是我得到的:
root@localhost.localdomain:[/root/demo]./crypt_import_xml.sh
/root/demo/rule/Windows
10.tar.gz
/root/demo/rule/Windows7.tar.gz
tar xf *.gz 文件后,xml 文件名仍然包含空格。处理文件名包含空格对我来说是一场噩梦。
【问题讨论】:
-
您可以使用:
for file in "${rule}"/*.gz; do echo "${file}" #tar -xf *.gz #mv a b.xml to ab.xml done并告诉我它是否有效吗?你首先不需要ls命令 -
@Allan 使用 ls 命令仍然得到相同的结果。
-
不应有任何
ls命令 -
(OT: @J.Doe 你的代码有一些问题(坏掉的 shebang,
function是一个 bashism 等),我可以推荐shellcheck.net 吗?)