Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Parameter expansion flags question
- X-seq: zsh-users 10239
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: <zsh-users@xxxxxxxxxx>
- Subject: Re: Parameter expansion flags question
- Date: Wed, 10 May 2006 10:28:18 -0700
- In-reply-to: <DD74FBB8EE28D441903D56487861CD9D0971728B@xxxxxxxxxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <DD74FBB8EE28D441903D56487861CD9D0971728B@xxxxxxxxxxxxxxxxxxxxxx>
On May 10, 10:22am, John Cooper wrote:
}
} delsites() {
} for site in "${(f)$($SITEMGR -i)}" ; {
} sitepath=${${=site}[1]}
} [[ -n "$sitepath" ]] && $SITEMGR -r "WICurrent=$sitepath"
} }
} }
}
} I'm new to the zsh parameter expansion flags, but I've gathered the
} ${(f) will take the command's output a line at a time, and the ${=site}
} will then split each line into words, allowing me to grab the first word
} of each line.
Right so far.
} Is there a better (or simpler!) way to do this?
You might consider using the "read" builtin:
$SITEMGR -i | while read -A site; {
$SITEMGR -r "WICurrent=${site[1]}"
}
Or:
$SITEMGR -i | while read sitepath stuff_to_discard; {
$SITEMGR -r "WICurrent=$sitepath"
}
If you still want to use expansion, you can combine the expansions:
for sitepath in ${${=${(f)"$($SITEMGR -i)"}}[1]}
Be warned, however, that ${=var} is not guaranteed to produce an array;
if any line in the $SITEMGR output has only one word in it, the result
of the above will be the first *character* of the line, not the first
*word*. The solutions with "read" do not have this problem.
} When the command has no output, the `for' loop is still executed once
} (seemingly because the command is within double-quotes) and is the
} reason for checking that the length of $sitepath is non-zero. Is there
} a way to avoid the loop being entered when the command has no output
} and avoid the need for this check?
Change the expansion to have it discard all empty array elements by
using the ":#pattern" operator with an empty pattern:
for sitepath in ${${=${(f)"$($SITEMGR -i)":#}}[1]}
} (btw, is there a good set of examples of using parameter expansion
} flags? The zsh guide seemed a bit sparse in this area)
Other than in the archives of this list, not that I'm aware of. Did
you look around on zshwiki.org?
Messages sorted by:
Reverse Date,
Date,
Thread,
Author