fish - 我如何处理 fish 的 null_glob 结果?

标签 fish

我有一个包含以下 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/

相关文章:

fish - 如何将环境变量设置为fish中的文件内容

macos - 重新启动 OSX 后未保存 ssh key

git - 缺少平衡此 if 语句的结尾

shell - 如何将文本与扩展变量组合成 Fish 中的变量扩展

shell - 使用fish-shell在提示时输出奇怪的文本

fish - 如何在fish shell脚本中设置环境变量

linux - 在 Fish Shell 中定义 Image Magick 的别名

fish - 在 fish shell 的命令中使用存储在一个变量中的多个参数

shell - 如何在shell内运行fish文件

function - 如何从fishshell中删除函数而不直接删除函数文件?