From owner-freebsd-questions@FreeBSD.ORG Tue Feb 1 12:22:31 2005 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E673116A4CE for ; Tue, 1 Feb 2005 12:22:30 +0000 (GMT) Received: from hosea.tallye.com (joel.tallye.com [216.99.199.78]) by mx1.FreeBSD.org (Postfix) with ESMTP id CE5AC43D48 for ; Tue, 1 Feb 2005 12:22:29 +0000 (GMT) (envelope-from lorenl@alzatex.com) Received: from hosea.tallye.com (hosea.tallye.com [127.0.0.1]) by hosea.tallye.com (8.12.8/8.12.10) with ESMTP id j11CMQYs020406 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 1 Feb 2005 04:22:27 -0800 Received: (from sttng359@localhost) by hosea.tallye.com (8.12.8/8.12.10/Submit) id j11CMQ4v020404; Tue, 1 Feb 2005 04:22:26 -0800 X-Authentication-Warning: hosea.tallye.com: sttng359 set sender to lorenl@alzatex.com using -f Date: Tue, 1 Feb 2005 04:22:26 -0800 From: "Loren M. Lang" To: Jay Moore Message-ID: <20050201122226.GM8619@alzatex.com> References: <200501300533.51350.jaymo@cromagnon.cullmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200501300533.51350.jaymo@cromagnon.cullmail.com> User-Agent: Mutt/1.4.1i X-GPG-Key: ftp://ftp.tallye.com/pub/lorenl_pubkey.asc X-GPG-Fingerprint: B3B9 D669 69C9 09EC 1BCD 835A FAF3 7A46 E4A3 280C cc: FreeBSD Mailing List Subject: Re: running interactive program from shell script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Feb 2005 12:22:31 -0000 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 > 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. 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. > > Thanks, > Jay > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@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