【发布时间】:2020-10-03 21:42:12
【问题描述】:
我想在图片文件夹中找到一些文件类型,我在/home/user/pictures 文件夹中创建了以下 bash 脚本:
for i in *.pdf *.sh *.txt;
do
echo 'all file types with extension' $i;
find /home/user/pictures -type f -iname $i;
done
但是,当我执行 bash 脚本时,对于位于 基本目录 /home/user/pictures 上的文件,它无法按预期工作。该命令不是 echo 'All File types with Extension *.sh',而是解释基目录的变量:
all file types with extension file1.sh
/home/user/pictures/file1.sh
all file types with extension file2.sh
/home/user/pictures/file2.sh
all file types with extension file3.sh
/home/user/pictures/file3.sh
我想知道为什么 echo - 命令不打印“所有扩展名为 *.sh 的文件类型”。
【问题讨论】:
-
因为 $i 更改了文件名而不是扩展名,所以您使用了 glob (*) 字符,shell 会查找所有 .sh 文件。
-
即
*.pdf在循环开始迭代之前被扩展为一个PDF文件列表;i未设置为字符串*.pdf。