<<<<<<<<<<<<<<<<<

Shell(unix) LYMB Shell(unix)

NAME

shell - an interface to a UNIX shell

DESCRIPTION

The shell class provides and interface to the user's UNIX shell. It allows the specification of a command string (like you would type at the shell prompt) and an optional standard input string (which the shell will receive as standard input). The command is then executed and the standard output can be trapped in a string instance variable for use inside scripts. Shells for which output is not trapped may be run asynchronously.

SUPERCLASS

object

INSTANCE VARIABLES

command The command to be executed.

std_input The (optional) standard input to be sent to the command.

std_output The standard output produced by the command (see trap_ouput).

trap_output Flag controlling whether the command's standard output is trapped or not.

async Flag controlling whether command shell is run asynchronously. If set, a "do!" message will return immediately after spawning the child process. Async cannot be on if trap_output is on.

MESSAGES

command[=?] Set/get the command to be executed.

std_input[=?] Set/get the standard input for the command.

std_output? Get the standard output produced by executing the command.

do! Execute the command. As a convenience, this message also returns the standard output of the command allowing easy chaining of commands (see example below). Further do! messages to this instance are blocked until the asynchronous process terminates and the pid is reset to 0 by "wait!" or

"check_status!".

pid? Returns the background process id of the shell.

status? Returns the status of the background process. 0=terminated, 1=running (set by a do! message), 2=stopped (set by check_status! if the waitpid call returns because the process has stopped).

kill! Kills the background process being run.

trap_output[=?] Set/get the trap_output flag.

trap_output_on! Set standard output trapping on. Has the side effect of turning async off.

trap_output_off! Set standard output trapping off.

async[=?] Set/get the async flag.

async_on! Set asynchronous mode on. Has the side effect of turning trap_output off.

async_off! Set asynchronous mode off.

wait! Perform waitpid(2) system call to wait for shell process to terminate, or to clear defunct shell process id from process table. Updates status and pid. If the process terminated, its end actions are executed. Could be used after termination of an asynchronous shell if "check_status!" is not done.

check_status! Does a non-blocking waitpid on the asynchronous shell process. Updates status and pid. If the process terminated, its end actions are executed. If this message is sent to the class, it is forwarded to all of the instances.

end_actions[=+?] Modify the list of actions that are performed upon shell termination. Analogous to "tick_actions[=+?]" for an actions object.

EXAMPLES

The following LYMB script uses the shell to run the UNIX spline command, producing points along a spline path. It

implements the command string: spline -a < "10 40 20 50 30" | sed '2,$s/^/,' | sed 's/ /,/g'

plot_data new: data;

shell new: sh command="rsh snowbird 'spline -a'" std_input="10 40 20 50 30" ;

sh std_input=[sh do!] -- execute command and return standard output command="sed '2,$s/^/,/'" ;

sh std_input=[sh do!] command="sed 's/ /,/g'" ;

string new: s = "data xy_data=(" + [sh do!] + ");" ;

parser parse: s;

NOTES

Asynchronous mode is incompatible with output trapping. (Deadlock could result if the shell process writes too much data to the pipe and the LYMB process isn't reading it.) Also, it is the user's responsibility to clear defunct process id's using the "wait!" or "check_status!" messages after an asynchronous shell process terminates. Here is a simple example showing how this is done: /* * Create signal handling action for SIGCHLD signal */ actions new:sigchld_handler tick_actions=` shell check_status!; ';

/* * Install the handler */ signal_handler install:("SIGCHLD",sigchld_handler);

/* * For illustrative purposes, we can set the signal_handler to * act immediately rather than wait for the next msg_send, since, * when this script terminates, lymb will be idle waiting for user * input. Normally, we would keep deferred mode set. */ signal_handler set_immediate:`SIGCHLD';

shell new:sh1 command=`sleep 10' async_on! end_actions=`! sh1 done'

new:sh2 command=`sleep 11' end_actions=`! sh2 done' new:sh3 command=`sleep 12' end_actions=`! sh3 done' new:sh4 command=`sleep 13' end_actions=`! sh4 done' new:sh5 command=`sleep 14' end_actions=`! sh5 done' ;

sh1 do!; sh2 do!; sh3 do!; sh4 do!; sh5 do!;

SEE ALSO

sh(1), csh(1), ksh(Common), wait(2), signal_handler, actions


Please send comments and suggestions to
consult@rpi.edu