zsh-users
 help / color / mirror / code / Atom feed
* Redirection and Variables
@ 2004-10-19 12:02 Nikolai Weibull
  2004-10-19 13:04 ` Peter Stephenson
  2004-10-21 13:57 ` Oliver Kiddle
  0 siblings, 2 replies; 7+ messages in thread
From: Nikolai Weibull @ 2004-10-19 12:02 UTC (permalink / raw)
  To: zsh-users

A couple of questions:

Is this the simplest way to do the following?

local -A http_programs https_programs mailto_programs ftp_programs

http_programs=(xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
               xt "/usr/bin/elinks '%s'")
http_programs=(xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
               xt "/usr/bin/elinks '%s'")
mailto_programs=(xt "/usr/bin/mutt '%s'")
ftp_programs=(xt "/usr/bin/lftp")

lookup () {
  echo "${(P)$(echo ${1}_programs\[xw\])}"
}

lookup http

If I have the following in a script that reads input from stdin:

sed -n "s/$REGEX/\1\n/gp" <&0 | sed "/$REGEX/!d" > $TMP
if [[ ! -s $TMP ]]; then
  rm -f $TMP
  exit 1
fi
${EDITOR:-vi} $TMP

my $EDITOR (vim) will complain that 
"Vim: Warning: Input is not from a terminal".  Is there a simple way to
get around this?
	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,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


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

* Re: Redirection and Variables
  2004-10-19 12:02 Redirection and Variables Nikolai Weibull
@ 2004-10-19 13:04 ` Peter Stephenson
  2004-10-19 13:51   ` Nikolai Weibull
  2004-10-21 13:57 ` Oliver Kiddle
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2004-10-19 13:04 UTC (permalink / raw)
  To: Nikolai Weibull; +Cc: zsh-users

Nikolai Weibull wrote:
> A couple of questions:
> 
> Is this the simplest way to do the following?
> 
> local -A http_programs https_programs mailto_programs ftp_programs
> 
> http_programs=(xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
>                xt "/usr/bin/elinks '%s'")
> http_programs=(xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
>                xt "/usr/bin/elinks '%s'")
> mailto_programs=(xt "/usr/bin/mutt '%s'")
> ftp_programs=(xt "/usr/bin/lftp")
> 
> lookup () {
>   echo "${(P)$(echo ${1}_programs\[xw\])}"
> }

Using eval would be nicer than the $(...), but actually there's a hack
to avoid even that:

lookup() {
  echo ${(P)${:-$1_programs[xw]}}
}

The empty nested substitution always returns the string after the :-,
which undergoes expansion, so the (P) flag can operate on that.

> If I have the following in a script that reads input from stdin:
> 
> sed -n "s/$REGEX/\1\n/gp" <&0 | sed "/$REGEX/!d" > $TMP
> if [[ ! -s $TMP ]]; then
>   rm -f $TMP
>   exit 1
> fi
> ${EDITOR:-vi} $TMP
> 
> my $EDITOR (vim) will complain that 
> "Vim: Warning: Input is not from a terminal".  Is there a simple way to
> get around this?

Are you saying the early part is important, i.e. if you just run vi in
the script without the earlier bit it works?  That sounds unlikely,
although I haven't investigated in detail.  If that's not the case, you
probably need to do some direction to force vi to use /dev/tty.

I'm not sure why you have "<&0", that simply redirects standard input
from standard input.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070



**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: Redirection and Variables
  2004-10-19 13:04 ` Peter Stephenson
@ 2004-10-19 13:51   ` Nikolai Weibull
  2004-10-19 14:01     ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Nikolai Weibull @ 2004-10-19 13:51 UTC (permalink / raw)
  To: zsh-users

* Peter Stephenson <pws@csr.com> [Oct 19, 2004 15:10]:
> > lookup () {
> >   echo "${(P)$(echo ${1}_programs\[xw\])}"
> > }

> Using eval would be nicer than the $(...), but actually there's a hack
> to avoid even that:

> lookup() {
>   echo ${(P)${:-$1_programs[xw]}}
> }

Ah, sweet.  Using eval always feels like a hack either way.  Maybe I
should stop thinking that.

> The empty nested substitution always returns the string after the :-,
> which undergoes expansion, so the (P) flag can operate on that.

> > If I have the following in a script that reads input from stdin:

> > sed -n "s/$REGEX/\1\n/gp" <&0 | sed "/$REGEX/!d" > $TMP
> > if [[ ! -s $TMP ]]; then
> >   rm -f $TMP
> >   exit 1
> > fi
> > ${EDITOR:-vi} $TMP

> > my $EDITOR (vim) will complain that
> > "Vim: Warning: Input is not from a terminal".  Is there a simple way to
> > get around this?

> Are you saying the early part is important, i.e. if you just run vi in
> the script without the earlier bit it works?  That sounds unlikely,
> although I haven't investigated in detail.  If that's not the case, you
> probably need to do some direction to force vi to use /dev/tty.

Well, the script is run like

url-extract <mail

and then sed processes the input from stdin and stores the result in
$TMP, then $EDITOR should be launched to edit the file.  The problem is,
since the script has stdin set, vim thinks that it's input isn't coming
from a terminal.  Any suggestions on how to solve that?  Vim can be told
to process stdin with - as the input file, but that doesn't really help
in this situation.  Anyway, the solution is to run

exec </dev/tty

before running $EDITOR, or is there perhaps a better one?

> I'm not sure why you have "<&0", that simply redirects standard input
> from standard input.

Oops, how very silly of me,
	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,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


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

* Re: Redirection and Variables
  2004-10-19 13:51   ` Nikolai Weibull
@ 2004-10-19 14:01     ` Peter Stephenson
  2004-10-19 14:18       ` Nikolai Weibull
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2004-10-19 14:01 UTC (permalink / raw)
  To: zsh-users

Nikolai Weibull wrote:
> Well, the script is run like
> 
> url-extract <mail
>
> Anyway, the solution is to run
> 
> exec </dev/tty
> 
> before running $EDITOR, or is there perhaps a better one?

You could pass the file "mail" as an argument to url-extract, then only
redirect from it internally for the chunk which needs it.  That avoids
any dependence on /dev/tty.  Remember you can do things like:

  {
    # stuff with mail as input
  } <mail

  # stuff with normal input

without having to create subshells.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: Redirection and Variables
  2004-10-19 14:01     ` Peter Stephenson
@ 2004-10-19 14:18       ` Nikolai Weibull
  0 siblings, 0 replies; 7+ messages in thread
From: Nikolai Weibull @ 2004-10-19 14:18 UTC (permalink / raw)
  To: zsh-users

* Peter Stephenson <pws@csr.com> [Oct 19, 2004 16:10]:
> > Well, the script is run like

> > url-extract <mail

> You could pass the file "mail" as an argument to url-extract, then only
> redirect from it internally for the chunk which needs it.  That avoids
> any dependence on /dev/tty.  Remember you can do things like:

>   {
>     # stuff with mail as input
>   } <mail

>   # stuff with normal input

> without having to create subshells.

Yeah, that would be fine, but I can't take the file as an argument.
This script is used from mutt and piping of messages is what you have at
your disposal in that case.
	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,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


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

* Re: Redirection and Variables
  2004-10-19 12:02 Redirection and Variables Nikolai Weibull
  2004-10-19 13:04 ` Peter Stephenson
@ 2004-10-21 13:57 ` Oliver Kiddle
  2004-10-21 14:41   ` Nikolai Weibull
  1 sibling, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2004-10-21 13:57 UTC (permalink / raw)
  To: Nikolai Weibull; +Cc: zsh-users

Nikolai Weibull wrote:
> 
> Is this the simplest way to do the following?
> 
> local -A http_programs https_programs mailto_programs ftp_programs
> 
> http_programs=(xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
>                xt "/usr/bin/elinks '%s'")
> mailto_programs=(xt "/usr/bin/mutt '%s'")

You could use one associative array, including more than one piece of
information in the key. That avoids the need for the (P) flag:

programs=(
  http_xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
  mailto_xt "/usr/bin/mutt '%s'"
)

>   echo "${(P)$(echo ${1}_programs\[xw\])}"

That could then be:
    echo "$programs[$1_xw]"

Oliver


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

* Re: Redirection and Variables
  2004-10-21 13:57 ` Oliver Kiddle
@ 2004-10-21 14:41   ` Nikolai Weibull
  0 siblings, 0 replies; 7+ messages in thread
From: Nikolai Weibull @ 2004-10-21 14:41 UTC (permalink / raw)
  To: zsh-users

* Oliver Kiddle <okiddle@yahoo.co.uk> [Oct 21, 2004 16:00]:
> > Is this the simplest way to do the following?
> >
> > local -A http_programs https_programs mailto_programs ftp_programs
> >
> > http_programs=(xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
> >                xt "/usr/bin/elinks '%s'")
> > mailto_programs=(xt "/usr/bin/mutt '%s'")

> You could use one associative array, including more than one piece of
> information in the key. That avoids the need for the (P) flag:

> programs=(
>   http_xw "/usr/bin/firefox -remote 'openURL(%s, new-tab)'"
>   mailto_xt "/usr/bin/mutt '%s'"
> )

> >   echo "${(P)$(echo ${1}_programs\[xw\])}"

> That could then be:
>     echo "$programs[$1_xw]"

How very true.  It seems that I split the problem along the wrong axis.
It's kind of hard thinking without "hashes-of-hashes",
	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,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


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

end of thread, other threads:[~2004-10-21 14:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-19 12:02 Redirection and Variables Nikolai Weibull
2004-10-19 13:04 ` Peter Stephenson
2004-10-19 13:51   ` Nikolai Weibull
2004-10-19 14:01     ` Peter Stephenson
2004-10-19 14:18       ` Nikolai Weibull
2004-10-21 13:57 ` Oliver Kiddle
2004-10-21 14:41   ` 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).