This section describes Systemwalker Scripts using two examples.
Example 1
The following example determines whether a certain file exists and, if it exists, starts an application. If it does not exist, the script does not start the application.
Input
Specify the name of the file to determine its existence as an argument.
Output
Exit code | Meaning |
---|---|
0 | Normal termination |
4 | Argument is not specified. |
8 | The specified file does not exist. |
16 | Application exits with a non-zero code, or application outputs a message to the standard error output. |
[Windows]
# # This script is sample. # # check argument # check the number of arguments. if { $argc == 0 } { # If the number of arguments is zero, an error message is output. puts stderr "file name was not specified." # In this case, the script exits with exit code 4. exit 4 } # set file name # Set the string given by the argument to the variable "filename." set filename $argv # check file # Set to trap exceptions. if {[catch { # Open the file. set file1 [open $filename r] }]} { # Output an error message if an exception is trapped. puts stderr "file not found." # In this case, the script exits with exit code 8. exit 8 } # Close the opened file. close $file1 # Set to trap exceptions. if {[catch { # Invoke the application. Store the standard output in the variable "outmsg." set outmsg [exec cmd /c {c:\usr\bin\appl001.exe}] # Output the data output to the standard output by the application to the job's standard. output. puts stdout $outmsg }]} { # Output process ID and exit code if an exception is trapped. puts stderr $errorCode # Output an error message if an exception is trapped. puts stderr $errorInfo # In this case, the script exits with exit code 16. exit 16 } # The script exits with exit code 0 when the application has been successfully executed. exit 0
[UNIX]
# # This script is sample. # # check argument # check the number of arguments. if { $argc == 0 } { # If the number of arguments is zero, an error message is output. puts stderr "file name was not specified." # In this case, the script exits with exit code 4. exit 4 } # set file name # Set the string given by the argument to the variable "filename." set filename $argv # check file # Set to trap exceptions. if {[catch { # Open the file. set file1 [open $filename r] }]} { # Output an error message if an exception is trapped. puts stderr "file not found." # In this case, the script exits with exit code 8. exit 8 } # Close the opened file. close $file1 # Set to trap exceptions. if {[catch { # Invoke the application. Store the standard output in the variable "outmsg." set outmsg [exec /usr/bin/appl001] # Output the data output to the standard output by the application to the job's standard. output. puts stdout $outmsg }]} { # Output process ID and exit code if an exception is trapped. puts stderr $errorCode # Output an error message if an exception is trapped. puts stderr $errorInfo # In this case, the script exits with exit code 16. exit 16 } } # The script exits with exit code 0 when the application has been successfully executed. exit 0
Note
Standard output of commands running in a script
The standard output is not inherited to the commands run in a script. Thus, if you change the point at which to invoke the application in example 1 as shown below, the standard output of the application will not be output as the job's standard output.
Note that, if a Systemwalker Script is executed as a scheduled job, you cannot check processing result messages output by the application run in the script on the [Previous log] sheet.
[Windows]
if {[catch { # Invoke the application. exec cmd /c {c:\usr\bin\appl001.exe} }]} { puts stderr $errorCode puts stderr $errorInfo exit 16 } exit 0
[UNIX]
if {[catch { # Invoke the application. exec /usr/bin/appl001 }]} { puts stderr $errorCode puts stderr $errorInfo exit 16 } exit 0
Example 2
The following example reads a file in which a list of names of files to be processed by an application and invokes the application for all filenames written in the file. The script passes a filename as an argument when invoking the application.
Input
Specify the name of the file with which the list of file names to be processed as arguments has been confirmed.
Output
Exit code | Meaning |
---|---|
0 | Normal termination |
4 | Argument is not specified |
8 | The specified file does not exist. |
16 | Application exits with a non-zero code, or application outputs a message to the standard error output. |
[Windows]
# # This script is sample. # # check argument # check the number of arguments. if { $argc == 0 } { # If the number of arguments is zero, an error message is output. puts stderr "file name was not specified." # In this case, the script exits with exit code 4. exit 4 } # set file name # Set the string given by the argument to the variable "filename." set filename $argv # check file # Set to trap exceptions. if {[catch { # Open the file. set file1 [open $filename r] }]} { # Output an error message if an exception is trapped. puts stderr "file not found." # In this case, the script exits with exit code 8. exit 8 } # Load a single line and execute loop until the end of the file is reached. while { [gets $file1 param] != -1 } { # Set to trap exceptions. if {[catch { # Invoke the application. Pass the loaded strings at this time. # Store the standard output in the variable "outmsg." set outmsg [exec cmd /c {c:\usr\bin\appl002.exe} $param] # Output the data output to the standard output by the application to the job's standard output. puts stdout $outmsg }]} { # Output process ID and exit code if an exception is trapped. puts stderr $errorCode # Output an error message if an exception is trapped. puts stderr $errorInfo close $file1 # In this case, the script exits with exit code 16. exit 16 } } #Close the file. close $file1 # The script exits with exit code 0 when the application has been successfully executed. exit 0
[UNIX]
# # This script is sample. # # check argument # check the number of arguments. if { $argc == 0 } { # If the number of arguments is zero, an error message is output. puts stderr "file name was not specified." # In this case, the script exits with exit code 4. exit 4 } # set file name # Set the string given by the argument to the variable "filename." set filename $argv # check file # Set to trap exceptions. if {[catch { # Open the file. set file1 [open $filename r] }]} { # Output an error message if an exception is trapped. puts stderr "file not found." # In this case, the script exits with exit code 8. exit 8 } # Load a single line and execute loop until the end of the file is reached. while { [gets $file1 param] != -1 } { # Set to trap exceptions. if {[catch { # Invoke the application. Pass the loaded strings at this time. # Store the standard output in the variable "outmsg." set outmsg [exec /usr/bin/appl002 $param] # Output the data output to the standard output by the application to the job's standard output. puts stdout $outmsg }]} { # Output process ID and exit code if an exception is trapped. puts stderr $errorCode # Output an error message if an exception is trapped. puts stderr $errorInfo close $file1 # In this case, the script exits with exit code 16. exit 16 } } #Close the file. close $file1 # The script exits with exit code 0 when the application has been successfully executed. exit 0