Home > Archive > Tcl > July 2005 > Return code in expect
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Return code in expect
|
|
| stella_pigeon@yahoo.ca 2005-07-24, 8:59 pm |
| Hi
Can anybody offer me a hint how to capture the return code from the
command executed by expect's "send" and how to return it to the calling
shell?
For example:
(1) I want to ssh to the remote machine and upon receiving a prompt "#
" execute /path/remoteCommand.
(2) remoteCommand returns 0 on success and non-zero on failure
(standard UNIX stuff)
(3) I have a following code:
spawn -noecho ssh [lindex $argv 0]
-- password etc ---
expect "# "
send "/path/remoteCommand\r"
Now, if the return code of remoteCommand can be captured, how can I
exit the expect script with that return code so the caller will have a
result of remoteCommand rather than expect script.
Any help is greatly appreciated.
Cheers
| |
| Patrick Dunnigan 2005-07-24, 8:59 pm |
| probably mostly depends on the shell on the ssh'd to machine.
However, I beleive that on most shells $? will give you return code of the
last command executed. For example:
$ ls -1
file1
file2
file3
$ echo $?
0
$ ls -1 xxx
ls: xxx: No such file or directory
$ echo $?
1
expect won't know the output unless you echo the variable over the ssh
connection.
<stella_pigeon@yahoo.ca> wrote in message
news:1121965843.006846.301240@o13g2000cwo.googlegroups.com...
Hi
Can anybody offer me a hint how to capture the return code from the
command executed by expect's "send" and how to return it to the calling
shell?
For example:
(1) I want to ssh to the remote machine and upon receiving a prompt "#
" execute /path/remoteCommand.
(2) remoteCommand returns 0 on success and non-zero on failure
(standard UNIX stuff)
(3) I have a following code:
spawn -noecho ssh [lindex $argv 0]
-- password etc ---
expect "# "
send "/path/remoteCommand\r"
Now, if the return code of remoteCommand can be captured, how can I
exit the expect script with that return code so the caller will have a
result of remoteCommand rather than expect script.
Any help is greatly appreciated.
Cheers
| |
| stella_pigeon@yahoo.ca 2005-07-24, 8:59 pm |
| Patrick Dunnigan wrote:
> probably mostly depends on the shell on the ssh'd to machine.
>
> However, I beleive that on most shells $? will give you return code of the
> last command executed. For example:
>
> $ ls -1
> file1
> file2
> file3
> $ echo $?
> 0
> $ ls -1 xxx
> ls: xxx: No such file or directory
> $ echo $?
> 1
Hi Patrick
I did exactly that. First "send remoteCommand", then "send echo $?",
finally retrieving expect_out(buffer). The problem is the the latter
contains lots of "garbage", echoed commands, prompts, newlines etc.,
which make a process of extracting the return code rather clumsy.
> expect won't know the output unless you echo the variable over the ssh
> connection.
Is there a clean and elegant way of doing this?
And one more question of a total Tcl ignorant :-):-). Is, for example,
set z1 "0"
and
set z2 0
the same or not? In other words, is
if {$z1==$z2}
true or false? If false, how can I convert z1 into number 0?
| |
| Ronald Fox 2005-07-24, 8:59 pm |
| stella_pigeon@yahoo.ca wrote:
For the first part, what about first expectging the echo $? string to
come echoed back at you and then look at the next line to get the actual
status value?
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321
Marvin: Life. Loathe it or ignore it. You can't like it.
> Patrick Dunnigan wrote:
>
>
>
> Hi Patrick
>
> I did exactly that. First "send remoteCommand", then "send echo $?",
> finally retrieving expect_out(buffer). The problem is the the latter
> contains lots of "garbage", echoed commands, prompts, newlines etc.,
> which make a process of extracting the return code rather clumsy.
>
>
>
>
> Is there a clean and elegant way of doing this?
>
> And one more question of a total Tcl ignorant :-):-). Is, for example,
> set z1 "0"
> and
> set z2 0
> the same or not? In other words, is
> if {$z1==$z2}
> true or false? If false, how can I convert z1 into number 0?
>
| |
| Cameron Laird 2005-07-24, 8:59 pm |
| In article <1122000689.086067.96580@o13g2000cwo.googlegroups.com>,
<stella_pigeon@yahoo.ca> wrote:
| |
| Cameron Laird 2005-07-24, 8:59 pm |
| In article <1122000689.086067.96580@o13g2000cwo.googlegroups.com>,
<stella_pigeon@yahoo.ca> wrote:
|
|
|
|
|