Example—The Return Value From A Remote Shell

While I have shown some fairly complex patterns, real patterns are usually pretty simple. In fact, sometimes other issues can make the patterns seem like the easy part of writing scripts.

The rsh program executes a command on a remote host. For example, the following command executes the command "quack twice" on host duck.

% rsh duck quack twice

While quack is a mythical program, you can imagine it shares a trait common to many programs. Namely, quack reports its status by returning an exit value. If quack works correctly, it exits with the value 0 (which by convention means success). Otherwise it exits with the value 1 (failure). From the C-shell, the status of the last command is stored in the variable status. I can demonstrate a successful interactive invocation by interacting directly with the C-shell.

% quack twice
% echo $status
0

Unfortunately, if quack is executed via rsh, the same echo command will not provide the exit status of quack. In fact, rsh does not provide any way of returning the status of a command. Checking the value of status after running rsh tells you only whether rsh itself ran successfully. The status of rsh is not really that useful. It reports problems such as "unknown host" if you give it a bogus host. But if rsh locates the host and executes the command, that is considered a success and 0 is returned no matter what happens inside the command. In fact, the rsh is considered a success even if the command is not ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.