Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
如何处理fish中的null_glob结果?_Fish - Fatal编程技术网

如何处理fish中的null_glob结果?

如何处理fish中的null_glob结果?,fish,Fish,我有一个fish函数,它包含以下rm语句: rm ~/path/to/dir/*.log 如果该路径中有*.log文件,则该语句可以正常工作,但如果没有*.log文件,则该语句将失败。错误是: ~/.config/fish/functions/myfunc.fish (line 5): No matches for wildcard '~/path/to/dir/*.log'. See `help expand`. rm ~/path/to/dir/*.log ^ in

我有一个fish函数,它包含以下
rm
语句:

rm ~/path/to/dir/*.log
如果该路径中有*.log文件,则该语句可以正常工作,但如果没有*.log文件,则该语句将失败。错误是:

~/.config/fish/functions/myfunc.fish (line 5): No matches for wildcard '~/path/to/dir/*.log'. See `help expand`.
    rm ~/path/to/dir/*.log
       ^
in function 'myfunc'
        called on standard input
ZSH拥有我们所说的。其中一个,
N
,负责为当前模式设置NULL\u GLOB选项,这基本上满足了我的要求:

如果文件名生成模式没有匹配项,请删除 模式,而不是报告错误

我知道fish没有,但我不清楚如何在我的fish函数中处理这种情况。我应该在数组中循环吗?这似乎很冗长。还是有一种更可疑的方法来处理这种情况

# A one-liner in ZSH becomes this in fish?
set -l arr ~/path/to/dir/*.log
for f in $arr
    rm $f
end

鱼不支持丰富的球状体,因为它们使球状体无效。所以你可以写:

set files ~/path/to/dir/*.log; rm -f $files
(之所以需要
-f
,是因为
rm
会在传递零个参数时进行投诉。)

count
也可以工作:

count ~/path/to/dir/*.log >/dev/null && rm ~/path/to/dir/*.log
为了完整性,循环:

for file in ~/path/to/dir/*.log ; rm $file; end

count~/path/to/dir/*.log>/dev/null;rm~/path/to/dir/*.log
似乎是一个不错的选择,不需要循环或额外变量。效果很好。非常感谢。