我有一个包含以下 rm
语句的 fish 函数:
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 有所谓的 Glob Qualifiers 。其中之一,
N
,处理为当前模式设置 NULL_GLOB 选项,它基本上满足我的要求:If a pattern for filename generation has no matches, delete the pattern from the argument list instead of reporting an error.
我知道 fish 没有 ZSH-style glob qualifiers ,但我不清楚如何在我的 fish 函数中处理这种情况。我应该遍历数组吗?看起来真的很冗长。或者有没有更可疑的方法来处理这种情况?
# A one-liner in ZSH becomes this in fish?
set -l arr ~/path/to/dir/*.log
for f in $arr
rm $f
end
最佳答案
fish 不支持丰富的 glob,but count
, set
, and for
are special 因为它们是 nullglob。所以你可以写:
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
关于fish - 我如何处理 fish 的 null_glob 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58720232/