running interactive program from shell script
Loren M. Lang
lorenl at alzatex.com
Tue Feb 1 04:22:31 PST 2005
On Sun, Jan 30, 2005 at 05:33:51AM -0600, Jay Moore wrote:
> I need a shell script that initiates a telnet session to another host. I have
> come up with the following, but unfortunately it terminates when the script
> is finished. What I wanted was for the telnet session to remain "alive" and
> interactive until manually terminated.
> Is there a way to accomplish this in a shell script?
> I've been told that I'll have to use "expect" or similar to accomplish this,
> but it seems to me that I should be able to do this using just Bourne shell
> #! /bin/sh
> (sleep 3;
> echo "password";
> sleep 3;
> echo "ls -la";
> sleep 3;
> ) | telnet -l user 192.168.0.2
When any command is run by default, it's standard input comes from the
same place as it's parent process. When you run this shell script from
your prompt, it inherits a standard input coming from your keyboard and
any command it runs inherit the same input as the shell. The same goes
for a processes standard output which it to your screen. Now when you
run a pipe (|), thing are a little different, the standard input and
output get redirected a little bit. The following command sets up a
pipe between two programs:
leftProgram | rightProgram
leftProgram's standard input is still from the keyboard, but it's output
gets redirected to the standard input of rightProgram and rightPrograms
standard output goes to the screen as usual. Everything rightProgram
reads on standard input comes only from leftProgram's standard output,
it's no longer reading from the keyboard. When leftProgram's done
sending output and exits, rightProgram sees and End Of File (EOF) and,
in general, will exit since there's nothing more to read. In your
example, you set up a sub-shell which runs 5 command, 3 sleeps and 2
echos, once the sub-shell exits, telnet just sees and EOF and exits.
The control charater ^D means EOF, on a blank terminal that you don't
mind logging out of, try hitting Control-D and see what happens. It
will logout usually, that's what happened to telnet.
Now guess what programA | programB | programC does...
programB never writes to the monitor or reads from the keyboard.
Working with pipes is one of the most basic things, albeit most useful
things that has made UNIX famous.
> freebsd-questions at freebsd.org mailing list
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
I sense much NT in you.
NT leads to Bluescreen.
Bluescreen leads to downtime.
Downtime leads to suffering.
NT is the path to the darkside.
Powerful Unix is.
Public Key: ftp://ftp.tallye.com/pub/lorenl_pubkey.asc
Fingerprint: B3B9 D669 69C9 09EC 1BCD 835A FAF3 7A46 E4A3 280C
More information about the freebsd-questions