Operating System - HP-UX
1830932 Members
2261 Online
110017 Solutions
New Discussion

Unix Shell Script - Input Trouble when trying to act as keyboard

 
SOLVED
Go to solution
Bob Insley
New Member

Unix Shell Script - Input Trouble when trying to act as keyboard

What I'm trying to do is open vi, type some text, save and quit all using a script. The problem is, one I open vi, I cannot get any text typed into vi, it all ends up coming out after I manually go an close vi.
Essentially, when I would like the script to do is enter keystrokes. If you could tell me how to do this, even if I have to use something else like Perl, I would be grateful.
-Bob Insley

current code:
#! /bin/ksh
vi temp
# what now?
10 REPLIES 10
harry d brown jr
Honored Contributor

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

#!/usr/bin/ksh
vi /tmp/temp << EOF
i hello world^[:wq!
EOF

You have to think like you are in the editor, but to go from input mode to command you use CONTROL-V ESC thus the ^[

live free or die
harry
Live Free or Die
Abdul Rahiman
Esteemed Contributor

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

why not use
echo "Hello world" > temp ?
Is there a reason why to use the vi specifically ??

Abdul.
No unix, no fun
Patrick Wallek
Honored Contributor

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

Why do you want to open vi do get text into a file? The are lots and lots of much easier ways to do it.

Invoking vi, in my opinion, is not a good idea.

I would rather use the echo command to get info into a file.

echo "hello world" > tempfile

harry d brown jr
Honored Contributor

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

#!/usr/bin/ksh
rm /tmp/temp
vi /tmp/temp << EOF
ihello world^[:1
:s/$/, this is great/
Gowow a line at the bottom^[:1
Oa line on top^[:2
oAfter hello world^[:wq!
EOF

more fun

live free or die
harry
Live Free or Die
Bob Insley
New Member

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

ok, that all makes sense. One question though, and that is why control-v? Is that how you designate a non-alphanumeric character or what?
And while we're at it, is there any way I can access obscure ascii characters?
Bob Insley
New Member

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

Ok, nm, scratch the ascii question. I still would like to know about the control-v though.
Thanks again,
bob
Mark Grant
Honored Contributor
Solution

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

In order to enter a control character such as ^K or ESC (^[) in "vi" you hit "^v" and then the control character you want
Never preceed any demonstration with anything more predictive than "watch this"
Bob Insley
New Member

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

The reason I'm using vi in this way, and not piping directly into a file, is that I was trying to figure out how to answer command line prompts using a script, and I knew trying to type in vi would be a similar problem, so I thought I'd tackle that first. Not that typing in vi would be any easier, but it just was where I wanted to start.
The ultimate reason, if you care, is that I'm trying to write a program that will, among other things, open up netscape and navigate through an online document tracking system as instructed. I'm a long way, but thanks for helping me here.
harry d brown jr
Honored Contributor

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

control-v ESC (escape) or control-v control-m (return) imbeds control characters into your script.

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Unix Shell Script - Input Trouble when trying to act as keyboard

Bob,

That won't work for all other processes, which is why we use:

EXPECT & TCL & TK:

http://hpux.cs.utah.edu/hppd/hpux/Tcl/expect-5.41/
http://hpux.cs.utah.edu/hppd/hpux/Tcl/tcltk-8.4.6/

live free or die
harry
Live Free or Die