Operating System - HP-UX
1822229 Members
3896 Online
109642 Solutions
New Discussion юеВ

Re: Using Expect within a Korn Shell script

 
SOLVED
Go to solution
Devon Thomas
Advisor

Using Expect within a Korn Shell script

Hi
I've written a korn shell that works OK, but I've now been told that automation of a telnet session is required. Is it possible to write an Expect function which can be called from within the korn shell?
9 REPLIES 9
curt larson_1
Honored Contributor
Solution

Re: Using Expect within a Korn Shell script

expect and ksh are syntaxally incompatible (on so many levels), so expect scripting can not be a ksh function. What you'd do is write your expect script:

#!/dirpath/expect
.
.
.

then call that script from within your shell script just like any other executable:

#!/usr/bin/ksh
.
.
expectprogram
if [ "$?" != 0 ] ;then
error message
fi
.
.
Devon Thomas
Advisor

Re: Using Expect within a Korn Shell script

Thanks Curt. That solves my immediate requirement, but is there a script/tool that can convert an expect script into a korn script :-). i.e. I'd like to use one script rather than several.
Graham Cameron_1
Honored Contributor

Re: Using Expect within a Korn Shell script

Devon

I think you are out of luck.
Expect can do things that ksh cannot.
That's why it was created.

Don't take my word for it. Visit http://expect.nist.gov/

But if you want to do everything in a single language, maybe Perl ?
There's bound to be a telnet module for Perl out there.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Devon Thomas
Advisor

Re: Using Expect within a Korn Shell script

Thanks Graham, but it'd mean learning PERL, which looks like it'd take up more time.
Think I'l stick to calling expect from from ksh :-).
Paddy_1
Valued Contributor

Re: Using Expect within a Korn Shell script

Expect is a dialect of "Tcl".You might want to think if Tcl/Tk which has many advantages like being interpreted,object oriented and easily fits in any architecture.
Using a Tcl shell(tclsh) should solve your problems.

The sufficiency of my merit is to know that my merit is NOT sufficient
Devon Thomas
Advisor

Re: Using Expect within a Korn Shell script

how easy is it to use expect commands inside a tclsh script...does tcl recognise expect commands or do I have to:
#! /blah/blah/tclsh
tcl commands
expectsctipt (args)
tcl commands
exit
Paddy_1
Valued Contributor

Re: Using Expect within a Korn Shell script

Think of expect as a library of "Tcl".

Structurally, Expect is actually an extension to another programming language called Tcl. This means that Expect adds commands and functionality to the Tcl language. It also means that to build and use Expect, you must also obtain and build Tcl.

Conceptually, Expect is a chat script generalized to the entire Unix universe

The book Exploring Expect, by Don Libes (O'Reilly & Associates) is very helpful
The sufficiency of my merit is to know that my merit is NOT sufficient
Renda Skandier
Frequent Advisor

Re: Using Expect within a Korn Shell script

Can you use a send & receive?

sndline "cd $files"
receive ">"
sndline "ls -l *.LOG"
receive ">"
Devon Thomas
Advisor

Re: Using Expect within a Korn Shell script

K,
Are these korn commands? I'll give them a try....at the moment I'm writing a korn 'main' script with an expect script 'bolted' onto it.
Paddy,
tcl sounds useful for the future, but time contraints are stopping me converting the main script into tcl.
Thanks