在鱼壳中,如何使用通配符和变量遍历文件?



如果我运行:

for f in *1000.png
echo $f
end

我得到

1000.png
11000.png
21000.png

我想做这样的事情:

for i in 1 2 3
for f in *$i000.png
echo $f
end
end

要得到

1000.png
11000.png
21000.png
2000.png
12000.png
22000.png
3000.png
13000.png
23000.png

相反,它不输出任何内容。

我也试过:

for i in 1 2
set name "*"$i"000.png"
for f in (ls $name)
echo $f
end
end

输出:

ls: *1000.png: No such file or directory
ls: *2000.png: No such file or directory

为了避免尝试扩展变量$i000,你可以这样做

for i in 1 2 3
for f in *{$i}000.png
echo $f
end
end

或者,完全避免外部循环:

for f in *{1,2,3}000.png

当你尝试引用*$i000.png中的i变量时,你的 shell 认为$i000意味着你正在尝试引用i000变量,而不是像你想要的那样i后跟三个零。

使用{$var_name}访问 fish 中的变量,通常最好始终以这种方式引用贝壳变量。

所以你就是你的情况,在第二行使用:

for f in *{$i}000.png

最新更新