How can I call vi from a script?

How can I call vi from a script?

Post by Chris Boy » Tue, 24 Jul 2001 23:40:02



How can I invoke an interactive editor in a bash script after piping or
redirecting to it?  To illustrate, the script is (eg) vitest:

  #!/bin/bash
  SCRATCH=/tmp/scratch$$
  cat > SCRATCH
  vi SCRATCH

If I do

  bash$ vitest
  Hello
  <EOT>

all is well and I can edit the scratch file.  However, if I attempt to
use redirection, e.g.,

  bash$ echo Hello | vitest

or

  bash$ vitest < some.file.or.other

I get an

  Input read error

How can I get the redirection or piping to work the way I want it to,
without hacking ps and getting vi to connect explicitly to the terminal?
I feel sure I'm missing something really obvious, but none of the man
pages or FAQs seems to elucidate.

--
mv .sig /dev/null

 
 
 

How can I call vi from a script?

Post by Ben.Altma » Wed, 25 Jul 2001 00:55:41



> How can I get the redirection or piping to work the way I want it to,
> without hacking ps and getting vi to connect explicitly to the terminal?

How would hacking ps help anything?

 
 
 

How can I call vi from a script?

Post by J?rgen Perss » Wed, 25 Jul 2001 01:07:07



> How can I invoke an interactive editor in a bash script after piping or
> redirecting to it?  

[snip]

Start vi with ''vi -'' if you want it to read from stdin.

  $ date | vi -

J?rgen

 
 
 

How can I call vi from a script?

Post by Chuck Geigne » Wed, 25 Jul 2001 01:51:34


You might want to try calling ex instead of vi (or even sed).

--
Chuck Geigner ---------------------------------------
AIX Sysop
Milner Library, Illinois State Univ.
"Been borrowing Occam's shaving instrument since 1992
Haven't cut myself yet."_____________#rgvac==mongoose

 
 
 

How can I call vi from a script?

Post by Chris Boy » Wed, 25 Jul 2001 16:08:04


:
:> How can I get the redirection or piping to work the way I want it to,
:> without hacking ps and getting vi to connect explicitly to the terminal?
:
: How would hacking ps help anything?

Because

  #!/bin/bash
  SCRATCH=/tmp/scratch$$
  cat > SCRATCH
  vi SCRATCH

doesn't work, but the above with (e.g.)

  vi SCRATCH < /dev/pts/45

does work (on SunOS at least).  The relevant tty data is grepped from
ps.

Does no-one know the answer?  The other two respondents have missed the
point, as the script is a bare-bones one: in the real application, I
want the input data to be manipulated extensively by the script both
before and after calling the interactive editor.

--
mv .sig /dev/null

 
 
 

How can I call vi from a script?

Post by J?rgen Perss » Wed, 25 Jul 2001 17:11:40





>:
>:> How can I get the redirection or piping to work the way I want it to,
>:> without hacking ps and getting vi to connect explicitly to the terminal?
>:
>: How would hacking ps help anything?

> Because

>   #!/bin/bash
>   SCRATCH=/tmp/scratch$$
>   cat > SCRATCH
>   vi SCRATCH

> doesn't work, but the above with (e.g.)

>   vi SCRATCH < /dev/pts/45

> does work (on SunOS at least).  The relevant tty data is grepped from
> ps.

> Does no-one know the answer?

try comp.editors

Quote:> The other two respondents have missed the point, as the script is a
> bare-bones one: in the real application, I want the input data to be
> manipulated extensively by the script both before and after calling
> the interactive editor.

Well... I didn't quite get your question -- besides, what implementation
of vi are you using?

Try closing stderr before starting vi, eg:

#!/bin/bash
SCRATCH=/tmp/scratch$$
cat > SCRATCH
exec <&2
vi SCRATCH

J?rgen

 
 
 

How can I call vi from a script?

Post by Chris Boy » Wed, 25 Jul 2001 17:51:35



:>:
:>:> How can I get the redirection or piping to work the way I want it to,
:>:> without hacking ps and getting vi to connect explicitly to the terminal?
(snip)
: Well... I didn't quite get your question -- besides, what implementation
: of vi are you using?

The problem is not vi-specific and affects other interactive editors.  I
used vi in the example because it gave an error message.  In reality I
use an incarnation of emacs or (ahem) ecce.

: Try closing stderr before starting vi, eg:
:
: #!/bin/bash
: SCRATCH=/tmp/scratch$$
: cat > SCRATCH
: exec <&2
: vi SCRATCH

Thanks, that works (but why?).  I tried lots of manipulations with stdin
and stdout, but not stderr.

(Of course the last two SCRATCHes in all my example scripts should have
been $SCRATCHes!)

--
mv .sig /dev/null

 
 
 

How can I call vi from a script?

Post by Russe » Wed, 25 Jul 2001 18:39:11






+ >:
+ >:> How can I get the redirection or piping to work the way I want it to,
+ >:> without hacking ps and getting vi to connect explicitly to the terminal?

why don't you just learn ed?

r.

 
 
 

How can I call vi from a script?

Post by J?rgen Perss » Wed, 25 Jul 2001 19:51:14




>: Try closing stderr before starting vi, eg:
>:
>: #!/bin/bash
>: SCRATCH=/tmp/scratch$$
>: cat > SCRATCH
>: exec <&2
>: vi SCRATCH

> Thanks, that works (but why?).  I tried lots of manipulations with stdin
> and stdout, but not stderr.

Sorry -- I belive I was wrong... I'm no I/O redirection guru but
remembered on old solution and wrote that other article too fast without
a proper sanity check.

Child processes inherit open file descriptors (which explains pipes) and
stdin is still open. exec <&2 (or ''exec 0<&2'') doesn't close stderr
but links stdin with stderr. You ought to be able to solve it by linking
it to stdout as well.

J?rgen

 
 
 

How can I call vi from a script?

Post by Villy Kru » Wed, 25 Jul 2001 20:49:46


On 24 Jul 2001 10:51:14 GMT,



>>: Try closing stderr before starting vi, eg:
>>:
>>: #!/bin/bash
>>: SCRATCH=/tmp/scratch$$
>>: cat > SCRATCH
>>: exec <&2
>>: vi SCRATCH

>> Thanks, that works (but why?).  I tried lots of manipulations with stdin
>> and stdout, but not stderr.

>Sorry -- I belive I was wrong... I'm no I/O redirection guru but
>remembered on old solution and wrote that other article too fast without
>a proper sanity check.

>Child processes inherit open file descriptors (which explains pipes) and
>stdin is still open. exec <&2 (or ''exec 0<&2'') doesn't close stderr
>but links stdin with stderr. You ought to be able to solve it by linking
>it to stdout as well.

The point is that stdin was redirected when calling the script and
its contents consumed by the cat command, so any other commands in
the script would see an immidiate EOF on stdin (plus it no longer
looks like a tty device).  The "exec <&2" will define a new stdin to
be the same as the original stderr, and when you loggin in original
the stdin, stdout, and stderr streams are all dupped from the same
tty device.  Thus "exec <&2" restores the original stdin to what
it was at login time, unless you have redirected stderr as well.
"exec </dev/tty >/dev/tty 2>&1" ought to work as well.

Villy

 
 
 

How can I call vi from a script?

Post by Bill Marcu » Thu, 26 Jul 2001 06:09:56



>  vi SCRATCH

>doesn't work, but the above with (e.g.)

>  vi SCRATCH < /dev/pts/45

>does work (on SunOS at least).  The relevant tty data is grepped from
>ps.

You can use /dev/tty unless you want to run vi on a different terminal.