munish
munish

Reputation: 4644

what does this statement mean for expect or for bash?

Exploring expect page 219 :

#!/bin/bash
set kludge { ${1+"$@"}
shift
shift
exec expect -f $0 ${1+"$@"}
}
# rest of script follows

i am able to execute my expect script within this bash interpreter,even i can pass command line arguments like: ./scriptname arg1 arg2 arg3 and so on but when i tried sh -x ./scriptname arg1 arg2 arg3 it threw some error. please clarify the above statements and does it really help to put expect scripts inside bash

Upvotes: 1

Views: 322

Answers (1)

glenn jackman
glenn jackman

Reputation: 246827

Looking at it from Expect first, it's pretty simple. The set command, when given 2 arguments, sets the value of a variable. Here, the variable name is kludge and the value is the multi-line string between the {braces}. Note that in Tcl (and Expect) braces suppress expansion (variable substitution, command expansion, etc), in the same way that single quotes act in a shell. Assuming that the variable kludge is never used, this is a completely innocuous statement.

A bit trickier from the shell. The shell's set command in this context will set the positional parameters. After this command:

$1 = "kludge"
$2 = "{"
$3, $4, ... <= will be the original command line parameters

Then there are 2 shift commands to pop "kludge" and "{" off the "stack" of parameters, restoring the original positional parameters. Then the exec command will launch expect, replacing the currently running shell process. If exec was not used, expect would run and (presumably) terminate, and then the shell would report a syntax error on the next line: }

Overall, this is an overly clever way to launch an expect program. Replace everything before "# rest of script follows" with

#!/usr/bin/expect -f

Or if there's much more stuff happening in the shell portion, use this:

#!/bin/bash
do whatever shell initialization required
expect -c << 'END_OF_EXPECT'
# rest of expect script follows
# ...
END_OF_EXPECT

To pass options in this case:

expect - "$@" << 'END_OF_EXPECT'
# ...

The single - tells expect that commands will come on stdin.

Upvotes: 1

Related Questions