是否有相当于“ shopt -s nullglob”的ZSH?
我目前正在处理一个从桌面上删除所有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
zsh
中的相应选项是csh_null_glob
(在中记录在
man zshoptions
中)。B(据我所知,模式消失的想法消失了而不是从字面上看待来自
csh
。The corresponding option in
zsh
isCSH_NULL_GLOB
(documented inman zshoptions
).b(As far as I can tell, the idea of a pattern disappearing rather than being treated literally comes from
csh
.)更像 zsh 的方法不是将其设置为通用选项(如 chepner 给出的答案中所建议的那样),而是决定每个模式,是否想要具有 nullglob 效果。例如,
如果没有与该模式匹配的文件,则简单地跳过循环。
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,
simply skips the loop if there are no files matching the pattern.
只需意识到
shopt
找不到的问题是我自动加载文件作为ZSH函数所致。当我像这样运行时,该脚本运行得很好:
bash ./tidy-desktop
以前我只是使用命令
tidy-desktop
而不是我的
zsh_aliases
:感谢@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
:Thanks to @Charles Duffy for helping me figure out what was going on there!