Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh for loop exclusion

Tags:

for-loop

zsh

glob

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?

like image 827
ABach Avatar asked May 23 '10 04:05

ABach


2 Answers

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
like image 196
Johnsyweb Avatar answered Sep 21 '22 10:09

Johnsyweb


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

like image 38
Dennis Williamson Avatar answered Sep 18 '22 10:09

Dennis Williamson