zsh for 循环排除

发布于 2024-09-02 23:27:24 字数 473 浏览 7 评论 0原文

这是一个有点简单的问题,但对于我来说,我无法弄清楚如何从 zsh for 循环中排除某些内容。例如,假设我们有这样的情况:

for $package in /home/user/settings/*
do
  # do stuff
done

假设在 /home/user/settings/ 中,有一个我想忽略的特定目录(“os”)。从逻辑上讲,我尝试了以下变体:

for $package in /home/user/settings/^os (works w/ "ls", but not with a foor loop)
for $package in /home/user/settings/*^os
for $package in /home/user/settings/^os*

......但这些似乎都不起作用。有人可以引导我的语法朝正确的方向发展吗?

This is somewhat of a simple question, but for the life of me, I cannot figure out how to exclude something from a zsh for loop. For instance, let's say we have this:

for $package in /home/user/settings/*
do
  # do stuff
done

Let's say that in /home/user/settings/, there is a particular directory ("os") that I want to ignore. Logically, I tried the following variations:

for $package in /home/user/settings/^os (works w/ "ls", but not with a foor loop)
for $package in /home/user/settings/*^os
for $package in /home/user/settings/^os*

...but none of those seem to work. Could someone steer my syntax in the right direction?

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

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

发布评论

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

评论(2

挽袖吟 2024-09-09 23:27:24

在我看来,额外的 $ 可能是导致你悲伤的原因。

试试这个:

for package in /home/user/settings/^os; do
    echo "Doing stuff with ${package}..."
done

如果您想将 ${package} 限制为仅目录,请使用 /home/user/settings/^os(/)

还要确保您设置了 extendedglob (我认为您这样做是因为 ls 适合您):

> set -o  | grep -i extendedglob
extendedglob          on

It looks to me that the extra $ may be what's causing your grief.

Try this:

for package in /home/user/settings/^os; do
    echo "Doing stuff with ${package}..."
done

If you want to limit ${package} to just directories, use /home/user/settings/^os(/).

Also ensure that you have extendedglob set (which I think you do since ls works for you):

> set -o  | grep -i extendedglob
extendedglob          on
怪我闹别瞎闹 2024-09-09 23:27:24

如果我set -o EXTENDED_GLOB(或setopt EXTENDED_GLOB),那么 for 循环对我有用。

That for loop works for me if I set -o EXTENDED_GLOB (or setopt EXTENDED_GLOB).

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