* RE: Parameter expansion flags question
@ 2006-05-15 9:01 John Cooper
0 siblings, 0 replies; 7+ messages in thread
From: John Cooper @ 2006-05-15 9:01 UTC (permalink / raw)
To: Bart Schaefer, zsh-users
Ah, I see. Thanks again for all the help.
--- John
-----Original Message-----
From: Bart Schaefer [mailto:schaefer@brasslantern.com]
Sent: 12 May 2006 15:45
To: zsh-users@sunsite.dk
Subject: Re: Parameter expansion flags question
On May 12, 12:02pm, John Cooper wrote:
} Subject: RE: Parameter expansion flags question
}
} > Placement of the quotes is important:
} > for site in ${(f)"$($SITEMGR -i)":#}
}
} OK, changing the placement of the quotes to the above fixes it.
There's
} just one thing I'm a little puzzled about - in the case where $SITEMGR
} produces no output, won't "$($SITEMGR -i)" be the empty string? If so,
} why is :# needed? It seems it will just replace an empty string with
} another empty string?
It's the same as the difference between
string=""
array=( word "$string" word )
print $#array
and
string=""
array=( word $string word )
print $#array
An unquoted empty string is discarded; a quoted empty string is
retained.
Effectively the :# construct discards the quoted empty string, leaving
an
unquoted one in its place, which the shell in turn discards when
building
the final array.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Parameter expansion flags question
2006-05-12 11:02 John Cooper
@ 2006-05-12 14:45 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2006-05-12 14:45 UTC (permalink / raw)
To: zsh-users
On May 12, 12:02pm, John Cooper wrote:
} Subject: RE: Parameter expansion flags question
}
} > Placement of the quotes is important:
} > for site in ${(f)"$($SITEMGR -i)":#}
}
} OK, changing the placement of the quotes to the above fixes it. There's
} just one thing I'm a little puzzled about - in the case where $SITEMGR
} produces no output, won't "$($SITEMGR -i)" be the empty string? If so,
} why is :# needed? It seems it will just replace an empty string with
} another empty string?
It's the same as the difference between
string=""
array=( word "$string" word )
print $#array
and
string=""
array=( word $string word )
print $#array
An unquoted empty string is discarded; a quoted empty string is retained.
Effectively the :# construct discards the quoted empty string, leaving an
unquoted one in its place, which the shell in turn discards when building
the final array.
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: Parameter expansion flags question
@ 2006-05-12 11:02 John Cooper
2006-05-12 14:45 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: John Cooper @ 2006-05-12 11:02 UTC (permalink / raw)
To: Bart Schaefer, zsh-users
> Placement of the quotes is important:
> for site in ${(f)"$($SITEMGR -i)":#}
OK, changing the placement of the quotes to the above fixes it. There's
just one thing I'm a little puzzled about - in the case where $SITEMGR
produces no output, won't "$($SITEMGR -i)" be the empty string? If so,
why is :# needed? It seems it will just replace an empty string with
another empty string?
Thanks,
--- John
-----Original Message-----
From: Bart Schaefer [mailto:schaefer@brasslantern.com]
Sent: 11 May 2006 17:39
To: zsh-users@sunsite.dk
Subject: Re: Parameter expansion flags question
[skipping around a bit]
On May 11, 10:59am, John Cooper wrote:
}
} One final thing - when I entered the "read" example into the shell as
} stated I got syntax errors. These were resolved by using "do" and
"done"
} instead of braces - is this to be expected?
Yeah, I messed up. I never use the form with the braces, but you did
in your original "for" sample, so I tried to keep it with "while". I
forgot that for "while" loops it only works when the first condition
is [[ ... ]] or (( ... )).
} Thanks for the detailed reply - it seems using "read" is the most
} straightforward approach. However, in the interests of learning more
} about expansions, I've been trying your suggestions and they don't
seem
} to work as expected.
Oh, duh. I spaced that there were multiple lines of output from the
$SITEMGR program. Obviously you need to process each line separately
before applying the subscript.
(This cold I'm coming down with must be affecting me worse than I
thought.)
} If I add the ":#pattern" operator to the original function, the "for"
} loop is still executed once in the case where $SITEMGR produces no
} output:
} delsites4 () {
} for site in "${(f)$($SITEMGR -i):#}"
Placement of the quotes is important:
for site in ${(f)"$($SITEMGR -i)":#}
If it still happens, I think it's because of Cygwin line termination.
Instead of an empty string when splitting with (f), you're getting a
string having a single carriage-return character.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Parameter expansion flags question
2006-05-11 9:59 John Cooper
@ 2006-05-11 16:38 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2006-05-11 16:38 UTC (permalink / raw)
To: zsh-users
[skipping around a bit]
On May 11, 10:59am, John Cooper wrote:
}
} One final thing - when I entered the "read" example into the shell as
} stated I got syntax errors. These were resolved by using "do" and "done"
} instead of braces - is this to be expected?
Yeah, I messed up. I never use the form with the braces, but you did
in your original "for" sample, so I tried to keep it with "while". I
forgot that for "while" loops it only works when the first condition
is [[ ... ]] or (( ... )).
} Thanks for the detailed reply - it seems using "read" is the most
} straightforward approach. However, in the interests of learning more
} about expansions, I've been trying your suggestions and they don't seem
} to work as expected.
Oh, duh. I spaced that there were multiple lines of output from the
$SITEMGR program. Obviously you need to process each line separately
before applying the subscript.
(This cold I'm coming down with must be affecting me worse than I
thought.)
} If I add the ":#pattern" operator to the original function, the "for"
} loop is still executed once in the case where $SITEMGR produces no
} output:
} delsites4 () {
} for site in "${(f)$($SITEMGR -i):#}"
Placement of the quotes is important:
for site in ${(f)"$($SITEMGR -i)":#}
If it still happens, I think it's because of Cygwin line termination.
Instead of an empty string when splitting with (f), you're getting a
string having a single carriage-return character.
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: Parameter expansion flags question
@ 2006-05-11 9:59 John Cooper
2006-05-11 16:38 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: John Cooper @ 2006-05-11 9:59 UTC (permalink / raw)
To: Bart Schaefer, zsh-users
Bart,
Thanks for the detailed reply - it seems using "read" is the most
straightforward approach. However, in the interests of learning more
about expansions, I've been trying your suggestions and they don't seem
to work as expected.
$ $SITEMGR -i
1:/Citrix/Pres WI http://RA.eng.citrite.net/Citrix/Pres
4.5.5.1159 c:\inetpub\wwwroot\Citrix\Pres
1:/Citrix/Pres2 WI http://RA.eng.citrite.net/Citrix/Pres2
4.5.5.1159 c:\inetpub\wwwroot\Citrix\Pres2
(not sure if Outlook will wrap the above, but the output is actually 2
lines, each starting "1:/Citrix")
My original function correctly finds the first word of both lines:
$ type delsites2
delsites2 () {
for site in "${(f)$($SITEMGR -i)}"
do
sitepath=${${=site}[1]}
echo "sitepath is $sitepath"
done
}
$ delsites2
sitepath is 1:/Citrix/Pres
sitepath is 1:/Citrix/Pres2
$
However, when the expansions are combined, it only finds the first word
of the first line:
$ type delsites3
delsites3 () {
for sitepath in ${${=${(f)"$($SITEMGR -i)"}}[1]}
do
echo "sitepath is $sitepath"
done
}
$ delsites3
sitepath is 1:/Citrix/Pres
$
If I add the ":#pattern" operator to the original function, the "for"
loop is still executed once in the case where $SITEMGR produces no
output:
$ type delsites4
delsites4 () {
for site in "${(f)$($SITEMGR -i):#}"
do
sitepath=${${=site}[1]}
echo sitepath is "$sitepath"
done
}
$ delsites
$ $SITEMGR -i
$
$ delsites4
sitepath is
$
One final thing - when I entered the "read" example into the shell as
stated I got syntax errors. These were resolved by using "do" and "done"
instead of braces - is this to be expected?
$ $SITEMGR -i
1:/Citrix/Pres WI http://RA.eng.citrite.net/Citrix/Pres
4.5.5.1159 c:\inetpub\wwwroot\Citrix\Pres
$ $SITEMGR -i | while read -A site; {
pipe while cursh> echo $site
pipe while cursh> }
pipe while> }
zsh: parse error near `}'
$
$ $SITEMGR -i | while read -A site; do
pipe while> echo $site
pipe while> done
1:/Citrix/Pres WI http://RA.eng.citrite.net/Citrix/Pres 4.5.5.1159
c:inetpubwwwrootCitrixPres
$
I'm using zsh 4.2.6 on cygwin/WinXP.
--- John
-----Original Message-----
From: Bart Schaefer [mailto:schaefer@brasslantern.com]
Sent: 10 May 2006 18:28
To: zsh-users@sunsite.dk
Subject: Re: Parameter expansion flags question
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?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Parameter expansion flags question
2006-05-10 9:22 John Cooper
@ 2006-05-10 17:28 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2006-05-10 17:28 UTC (permalink / raw)
To: zsh-users
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?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Parameter expansion flags question
@ 2006-05-10 9:22 John Cooper
2006-05-10 17:28 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: John Cooper @ 2006-05-10 9:22 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 1477 bytes --]
I've been writing a function to run a command, extract the first word of
each line of the command's output and use that as a parameter to another
command. The command's output looks like this:
1:/Citrix/Pres WI
http://RA.eng.citrite.net/Citrix/Pres 4.5.5.1159
c:\inetpub\wwwroot\Citrix\Pres
1:/Citrix/Pres2 WI
http://RA.eng.citrite.net/Citrix/Pres2 4.5.5.1159
c:\inetpub\wwwroot\Citrix\Pres2
After some trial-and-error I finally have the following function:
SITEMGR=c:/Program\ Files/Citrix/Web\
Interface/4.5/sitemgr.exe
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.
Is there a better (or simpler!) way to do this? 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?
(btw, is there a good set of examples of using parameter expansion
flags? The zsh guide seemed a bit sparse in this area)
Thanks,
--- John
[-- Attachment #2: Type: text/html, Size: 6784 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2006-05-15 9:02 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-15 9:01 Parameter expansion flags question John Cooper
-- strict thread matches above, loose matches on Subject: below --
2006-05-12 11:02 John Cooper
2006-05-12 14:45 ` Bart Schaefer
2006-05-11 9:59 John Cooper
2006-05-11 16:38 ` Bart Schaefer
2006-05-10 9:22 John Cooper
2006-05-10 17:28 ` Bart Schaefer
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).