running interactive program from shell script

Jay Moore jaymo at
Thu Feb 3 03:04:04 PST 2005

On Tuesday 01 February 2005 06:22 am, Loren M. Lang 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 commands.
> >
> > #! /bin/sh
> >
> >     (sleep 3;
> >     echo "password";
> >     sleep 3;
> >     echo "ls -la";
> >     sleep 3;
> >     ) | telnet -l user
> 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.

I believe you are correct - thanks. Understanding why this is happening has 
lifted a huge, uncomfortable burden :)

But it still seems that there should be a way to do this using a shell 
script... I will have to think about this some more.

Best Rgds,

More information about the freebsd-questions mailing list