Top
Systemwalker Operation Manager V17.0.1 Reference Guide

15.2.7 exec (Invoke Subprocess(es))

Description

Invokes subprocess(es) to run program commands.

Synopsis

[Windows]

exec cmd /c cmdname [args ...]

Note: The exec command must be used with cmd /c.

[UNIX]

exec cmdname [args ...]

Options

cmdname

Specify thei name of a command file to invoke. The file names on Windows can also be separated by a slash (/) in addition to the backslash sign (\). When using a backslash sign, the entire file name must be enclosed in braces ({}) so as not to be treated as an escape character.

args ...

Specify the argument to be passed to the command to invoke.

If the last args is "&", the command is executed in background. Generally, the exec command is executed in foreground and waits for the cmdname process to terminate. However, when executed in background, it returns without doing so.

In addition, it is possible to specify a flow control symbol in args.

For Windows, when giving a file by specifying its path, the directory must be separated by backslash sign (\) and the entire file name must be enclosed in braces ({}).

Flow control

Writing the following flow control symbols to args allows manipulation of standard inputs/outputs of commands invoked or information exchange through activation of multiple commands.

Symbol and form

Meaning

Example

|

Separates individual commands in the pipeline. The standard output of the preceding command will be piped into the standard input of the subsequent command. Three or more commands can be piped in the same way.

If "|&" is used in place of "|", the standard output as well as standard error output will be piped into the standard input of the subsequent command.

[Windows]
exec cmd /c dir |cmd /c sort

[UNIX]
exec ls -1 | grep root

< filename

The content of file specified by filename is read as the standard input of cmdname. (Note 1)

[Windows]
exec cmd /c sort < data.txt

[UNIX]
exec sort < data.txt

<< value

The value (string) specified by value is read as the standard input of cmdname. (Note 1)

[Windows]
exec cmd /c sort << $data

[UNIX]
exec sort << $data

> filename

The standard output of cmdname is overwritten to the file specified by filename. (Note 1)

Using ">>" in place of ">" causes it to be appended rather than being overwritten.

[Windows]
exec cmd /c dir > data

[UNIX]
exec ls > data

>& filename

The standard output of cmdname and standard error output are overwritten to the file specified by filename. (Note 1)

Using ">>&" in place of ">&" causes it to be appended rather than being overwritten.

[Windows]
exec cmd /c dir data >& out

[UNIX]
exec rm -f data >& out

2> filename

The standard error output of cmdname is overwritten to the file specified by filename. (Note 2)

Using "2>>" in place of "2>" causes it to be appended rather than being overwritten.

[Windows]
exec cmd /c dir data 2> out

[UNIX]

exec rm -f data 2> out

Note 1:

If cmdname comprises two or more commands through |, the first command will be used for reading and the last one for writing.

Note 2:

If cmdname comprises two or more commands through |, the standard error output of all commands will be stored in the file specified by filename.

Return Values

When the command is executed in foreground:

The standard output of the cmdname command is returned. The trailing line break code in the output text will be deleted.

When multiple commands are executed through flow control, the standard output of the last command will be returned.

When the standard output is redirected through flow control, a blank string will be returned.

When the command is executed in background:

The process ID of the command invoked is returned.

When multiple commands are executed through flow control, all process IDs separated by blanks will be returned. Due to this, the standard output that is output by an application cannot be obtained as the return value of the exec command.

Other Input/Output Information

Reference

catch

Cautions

Examples

[Windows]

The following example runs the dir command and outputs the command's standard output/standard error output to script's standard output.

if {[catch {
          set outmsg [exec cmd /c dir {c:\tmp\data}]

          # Output dir's standard output (return value from exec) to script's standard
             output.
          puts stdout $outmsg
      }]} {
    # Occurrence of an exception. 
    # Store $errorInfo as initial value in outmsg.
    set outmsg $errorInfo

    # Output dir's output information (except for trailing two lines of errorInfo) to the
       script's standard output.
    regexp {(.*)\n.* \n.*} $errorInfo all outmsg
    puts stdout $outmsg
}

[UNIX]

The following example runs the ls command and outputs the command's standard output/standard error output to script's standard output.

if {[catch {
          set outmsg [exec ls -l /var/tmp/data]

          # Output ls's standard output (return value from exec) to script's standard
             output.
          puts stdout $outmsg
      }]} {
    # Occurrence of an exception. 
    # Store $errorInfo as initial value in outmsg.
    set outmsg $errorInfo

    # Output ls's output information (except for trailing two lines of errorInfo) to the
       script's standard output.
    regexp {(.*)\n.* \n.*} $errorInfo all outmsg
    puts stdout $outmsg
}

Execution Results/Output Format

The result of command execution is displayed on the standard output as shown below.

[Windows]

[UNIX]