zsh-users
 help / color / mirror / code / Atom feed
* RE: Substitution Cruft
@ 2003-09-10 13:57 Borzenkov Andrey
  0 siblings, 0 replies; 4+ messages in thread
From: Borzenkov Andrey @ 2003-09-10 13:57 UTC (permalink / raw)
  To: 'Nikolai Weibull', 'Zsh Users'


> OK.  This has been annoying me for some time.  I must be doing something
> wrong, or there must be an easier way in any case.  I want to run a
> command with command line options output from a shell substitution:
> 	lprec ${(Q)${(zf)$(sed -n 's/["\$]/\\&/g;s/^\([A-Za-
> z]\+\)=\(.\+\)$/--\L\1\E "\2"/;/^--[a-z-]\+/p' < $tmp)}} &
> what this does is read a file ($tmp) that looks like
> Option1: value1
> Option2: value2
> and turns this into
> --option1 "value1"
> --option2 "value2"
> and this is then passed to the command 'lprec'.
> Is there a simpler way than using ${(Q)${(zf)...}}?


Not sure if it simpler, but (all one line)

bor@itsrm2% echo
${${(f)"$(<foo)"}//(#b)[[:space:]]#([^[:space:]]##):[[:space:]](*)/--$match[
1] \"$match[2]\"}
--option1 "value 1" --option2 "value 2"

bor@itsrm2% cat foo
option1: value 1
option2: value 2

adjust spaces to taste (I do not know if value is separated by just one
space or starts with first non-space).

You need extended globbing turned on.

-andrey


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Substitution Cruft
  2003-09-10 14:31 ` Bart Schaefer
@ 2003-09-10 16:54   ` Nikolai Weibull
  0 siblings, 0 replies; 4+ messages in thread
From: Nikolai Weibull @ 2003-09-10 16:54 UTC (permalink / raw)
  To: Zsh Users

* Bart Schaefer <schaefer@brasslantern.com> [Sep, 10 2003 18:00]:
> Without eliminating the $(sed ...) the best alternative is:
>
> 	eval lprec "$(sed ...)"
yeah, I was using that, but I didn't want to use eval, so I changed
it...
	nikolai

--
::: name: Nikolai Weibull    :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA    :: loc atm: Gothenburg, Sweden    :::
::: page: www.pcppopper.org  :: fun atm: gf,lps,ruby,php,war3  :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Substitution Cruft
  2003-09-10 13:21 Nikolai Weibull
@ 2003-09-10 14:31 ` Bart Schaefer
  2003-09-10 16:54   ` Nikolai Weibull
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2003-09-10 14:31 UTC (permalink / raw)
  To: Zsh Users

On Sep 10,  3:21pm, Nikolai Weibull wrote:
}
} 	lprec ${(Q)${(zf)$(sed -n 's/["\$]/\\&/g;s/^\([A-Za-z]\+\)=\(.\+\)$/--\L\1\E "\2"/;/^--[a-z-]\+/p' < $tmp)}} &

} Is there a simpler way than using ${(Q)${(zf)...}}?

Without eliminating the $(sed ...) the best alternative is:

	eval lprec "$(sed ...)"

(Yes, the double quote in ["\$] is handled correctly even if you wrap the
whole $(...) in double quotes.)

The whole thing could be replaced (assuming default setopts shwordsplit
et al.) with:

	while read option value
	do arguments=( $arguments --${(L)option:s/://} $value )
	done < $tmp

	lprec $arguments &

which while it takes a few more lines is certainly less cryptic and does
not fork a process.  If for some reason you must write it as one line:

while read o v; do a=($a --${(L)o:s/://} $v); done < $tmp; lprec $a &

Even if you stick "local -a a; local o v;" on the front of that, it's
shorter than what you started with.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Substitution Cruft
@ 2003-09-10 13:21 Nikolai Weibull
  2003-09-10 14:31 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Nikolai Weibull @ 2003-09-10 13:21 UTC (permalink / raw)
  To: Zsh Users

OK.  This has been annoying me for some time.  I must be doing something
wrong, or there must be an easier way in any case.  I want to run a
command with command line options output from a shell substitution:
	lprec ${(Q)${(zf)$(sed -n 's/["\$]/\\&/g;s/^\([A-Za-z]\+\)=\(.\+\)$/--\L\1\E "\2"/;/^--[a-z-]\+/p' < $tmp)}} &
what this does is read a file ($tmp) that looks like
Option1: value1
Option2: value2
and turns this into
--option1 "value1"
--option2 "value2"
and this is then passed to the command 'lprec'.
Is there a simpler way than using ${(Q)${(zf)...}}?
	nikolai

--
::: name: Nikolai Weibull    :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA    :: loc atm: Gothenburg, Sweden    :::
::: page: www.pcppopper.org  :: fun atm: gf,lps,ruby,php,war3  :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-09-10 16:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-10 13:57 Substitution Cruft Borzenkov Andrey
  -- strict thread matches above, loose matches on Subject: below --
2003-09-10 13:21 Nikolai Weibull
2003-09-10 14:31 ` Bart Schaefer
2003-09-10 16:54   ` Nikolai Weibull

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).