Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Feb 2005 05:02:28 -0600
From:      Jay Moore <jaymo@cromagnon.cullmail.com>
To:        "Loren M. Lang" <lorenl@alzatex.com>
Cc:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: running interactive program from shell script
Message-ID:  <200502030502.28281.jaymo@cromagnon.cullmail.com>
In-Reply-To: <20050201122226.GM8619@alzatex.com>
References:  <200501300533.51350.jaymo@cromagnon.cullmail.com> <20050201122226.GM8619@alzatex.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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 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.

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,
Jay



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200502030502.28281.jaymo>