是否有相当于“ shopt -s nullglob”的ZSH?

发布于 2025-01-18 19:32:00 字数 505 浏览 1 评论 0原文

我目前正在处理一个从桌面上删除所有PNG文件的脚本。我想创建一个文件路径的数组,然后在每个文件上使用rm命令。

这是相关的代码:

#!/usr/bin/env bash

shopt -s nullglob

files=("$HOME"/Desktop/*.png)
files_found="${#files[@]}"

shopt -u nullglob

建议在没有匹配文件的情况下使用shopt

但是,我在MacOS上,刚刚发现shopt不适合ZSH。当我运行脚本时,找不到命令:shopt

我发现ZSH具有一个称为setopt的等效物“>文档我无法完全弄清楚哪个选项是正确使用的选项。我似乎也找不到任何例子。

谁能向我指向正确的方向?

I'm currently working on a script that deletes all the PNG files from my Desktop. I want to create an array of file paths then use the rm command on each one.

This is the relevant bit of code:

#!/usr/bin/env bash

shopt -s nullglob

files=("$HOME"/Desktop/*.png)
files_found="${#files[@]}"

shopt -u nullglob

It has been recommend that I use shopt in case of no matching files.

However I'm on MacOS and just discovered that shopt is not available for ZSH. When I run the script I get command not found: shopt.

I've found the ZSH has an equivalent called setopt however after reading through the documentation I can't quite figure out which option is the correct one to use in the case. I can't seem to find any examples either.

Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南…巷孤猫 2025-01-25 19:32:00

zsh中的相应选项是csh_null_glob(在中记录在man zshoptions中)。B

 setopt CSH_NULL_GLOB

(据我所知,模式消失的想法消失了而不是从字面上看待来自csh

The corresponding option in zsh is CSH_NULL_GLOB (documented in man zshoptions).b

 setopt CSH_NULL_GLOB

(As far as I can tell, the idea of a pattern disappearing rather than being treated literally comes from csh.)

春风十里 2025-01-25 19:32:00

更像 zsh 的方法不是将其设置为通用选项(如 chepner 给出的答案中所建议的那样),而是决定每个模式,是否想要具有 nullglob 效果。例如,

for f in x*y*(N)
do
  echo $f
done

如果没有与该模式匹配的文件,则简单地跳过循环。

The more zsh-like approach is not to set this as a general option (as suggested in the answer given by chepner), but to decide on each pattern, whether or you want to have the nullglob effect. For example,

for f in x*y*(N)
do
  echo $f
done

simply skips the loop if there are no files matching the pattern.

人生百味 2025-01-25 19:32:00

只需意识到shopt找不到的问题是我自动加载文件作为ZSH函数所致。

当我像这样运行时,该脚本运行得很好:

bash ./tidy-desktop

以前我只是使用命令tidy-desktop

而不是我的zsh_aliases

tidy-desktop="~/.zshfn/tidy-desktop"

感谢@Charles Duffy帮助我弄清楚那里发生了什么!

Just come to the realisation that the issue of shopt not being found was due to me auto-loading the file as a ZSH function.

The script worked perfectly when I ran it like so:

bash ./tidy-desktop

Previously I had been running it just with the command tidy-desktop

Instead I now have this in my zsh_aliases:

tidy-desktop="~/.zshfn/tidy-desktop"

Thanks to @Charles Duffy for helping me figure out what was going on there!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文