Call POSIX commands from BS2000
Domain: | PROCEDURE |
Privileges: | STD-PROCESSING |
The BS2000 command EXECUTE-POSIX-CMD provides a way of calling POSIX shell commands from BS2000. This means that the shell no longer needs to be called explicitly to execute commands, and you can start individual commands, entire command sequences or shell scripts in BS2000.
For example, you can use EXECUTE-POSIX-CMD to configure directories in POSIX before a copy process with COPY-POSIX-FILE or to process the copied files further after a copy process.
Knowledge of the syntax of shell commands is a prerequisite for using EXECUTE-POSIX-CMD.
The commands or command sequences can either be entered explicitly or be read and executed from a BS2000 file. When multiple commands/command sequences are entered explicitly these are entered as individual list elements separated by commas. However, no separate check is performed; each list element is forwarded to the shell just as it is.
When entry is via a BS2000 file, it is possible to start commands and shell scripts after they have been copied to BS2000. No parameterization is possible in this case.
The commands/command sequences called explicitly or implicitly can be stored in a log file when EXECUTE-POSIX-CMD is executed. As this file is a BS2000 file, it can, among other things, be used as an input file for a subsequent call of EXECUTE-POSIX-CMD. If, for example, a comprehensive command sequence is to be performed several times, this means that explicit specification is not required.
The command outputs are either displayed on the screen (SYSOUT) or written to a BS2000 file.
The following environment variable is set in the shell which is started with the EXECUTE-POSIX-CMD command:
EXECUTE_POSIX_CMD="YES"
Querying this variable, e.g. in /etc/profile or .profile, enables outputs to be suppressed which are not required for the EXECUTE-POSIX-CMD command.
Format
EXECUTE-POSIX-CMD Alias: ECXCMD | ||||||||||||||||||||
|
Operands
CMD=
Specifies the commands or scripts to be executed.
CMD = <filename 1..54>
The commands/command sequences are read from a BS2000 file.
CMD = list-poss (50): <c-string 1..1500 with-low>
The commands/command sequences are specified explicitly.
If a list of strings is specified, their total length must not exceed approximately 1500, otherwise the SDF error CMD0065 may occur.
INPUT-LOG-FILE =
Specifies whether or not a log file should be written.
INPUT-LOG-FILE =*NONE
No log file is written.
INPUT-LOG-FILE = <filename 1..54 without-generation-version>(...)
Specifies the BS2000 file which is to be the log file.
WRITE-MODE = *REPLACE / *EXTEND
Specifies whether the log file is to be created or extended whenever EXECUTE-POSIX-CMD is called. WRITE-MODE is only relevant when a BS2000 file is specified.
OUTPUT =
Specifies where the command outputs are to appear.
OUTPUT = *SYSOUT
The command outputs are displayed on the screen.
OUTPUT = <filename 1..54>
The command outputs are written to a BS2000 file.
Restrictions
Commands/scripts which are executed with EXECUTE-POSIX-CMD cannot be read from the default input as this is terminated before the command/script is executed. Consequently such commands/scripts receive EOF when they attempt to read from the standard input.
POSIX commands which under some circumstances may read from the standard input:
rm
Query when deleting write-protected files if the -f option was not specified; EOF when reading stdin is treated like yes, i.e. the file is deleted.
mv
Query when overwriting write-protected files if the -f option was not specified; the file is not overwritten and an error is generated.
bs2cp
Query when overwriting BS2000 files if the -f option was not specified; EOF when reading stdin is treated like no, i.e. the BS2000 file is not overwritten.
mailx
Inputs to mailx are not possible, i.e. only the query as to whether messages are present makes sense.
In the case of EXECUTE-POSIX-CMD stdout and stderr are not connected to a terminal but to a pipe. Commands/scripts which require stdout and stderr to be connected to a terminal therefore do not function or do not function correctly. These commands/scripts use the CRTE functions isatty() and ttyname() to ascertain the terminal with which stdout and stderr are connected.
POSIX commands may not function or not function correctly for this reason are:
tty
Returns not a tty with exit status 1 instead of /dev/term/nnnn.
tabs
Does not function on block terminals.
mesg, write, talk
These commands for exchanging messages between terminals only function to a very limited degree on block terminals, and virtually not at all under EXECUTE-POSIX-CMD.
more
The more command under EXECUTE-POSIX-CMD behaves like the cat command.
patch
In the event of queries an empty answer is generated, which can result in endless loops.
pax
Interactive mode (-i option) is not possible.
nohup
The nohup command does not work because stdout is not a terminal.
ls
The ls command outputs the files in several columns only if this is explicitly requested with ls -C.
fg
Returns No Job Control.
bg
Returns No Job Control.
If the shell command exec is executed with EXECUTE-POSIX-CMD, the current shell is unloaded and the mechanisms for forwarding outputs and/or the exit value of forked processes may be disabled.
Example
/begin-block %BEGIN-BLOCK/ecxcmd ('exec who am i') %BEGIN-BLOCK/if-cmd-error %IF-CMD-ERROR/wrtx 'cmd 1 failed' %IF-CMD-ERROR/else %ELSE/wrtx 'cmd 1 ok' %ELSE/end-if %BEGIN-BLOCK/ecxcmd ('exec who ar u') %BEGIN-BLOCK/if-cmd-error %IF-CMD-ERROR/wrtx 'cmd 2 failed' %IF-CMD-ERROR/else %ELSE/wrtx 'cmd 2 ok' %ELSE/end-if %BEGIN-BLOCK/end-block FROEDE sf/002 Nov 12 09:07 cmd 1 ok who: Syntax: who [-mu] -s [-bHlprt] [datei] who [-mTu] [-abdHlprt] [datei] who -q [-n #] [datei] who [am i|am I] cmd 2 ok /
The fc command has no effect on inputs outside the script. It is therefore not suitable for use under /EXECUTE-POSIX-CMD.
The shell commands executed with EXECUTE-POSIX-CMD are not logged in the customary command memory ($HOME/.sh_history) but in a separate, relatively short-term (HISTSIZE=100) command memory under /tmp/.ecxcmd_sh_history_<user-name>.
Command substitutions by ’command’ or $(command) under EXECUTE-POSIX-CMD are always executed in a subshell. The POSIX shell, on the other hand, provides a range of commands which can be substituted within the shell.
The result of this is that individual commands can behave differently from in the POSIX shell if the results are process-specific. Familiar cases here are ftyp and bs2file, and also accesses to variables or functions which have not been exported.
Aliases:
The command sequence to be executed by EXECUTE-POSIX-CMD is executed in the POSIX shell with the dot command. Consequently the alias command is available for defining aliases, but it is of no relevance for command execution in the command sequence. If aliases are to be defined and used in a command sequence, the command sequence must be copied to a (temporary) POSIX file. This file must be assigned the Execute right and be executed (not with the dot command).The following procedures make sense:
The command sequence is created as a BS2000 file and is copied to a temporary POSIX file using COPY-POSIX-FILE. It is then executed.
Example
/EXEC-POSIX-CMD CMD=(’chmod +x scriptfile’, ’scriptfile’, ’rm -f
scriptfile’)
The command sequence itself generates a temporary script file in POSIX from a Here document and executes this.
Example
cat <<***END_OF_SCRIPT >/tmp/my_scriptfile_$$ command1 command2 ... ***END_OF_SCRIPT /tmp/my_scriptfile_$$ rm -f /tmp/my_scriptfile_$$
EXECUTE-POSIX-CMD terminates only when all the background processes started from the command sequence have been terminated. The nohup command cannot be used to force asynchronous processing.
The command sequence called with EXECUTE-POSIX-CMD is executed in a subshell which is generated internally by means of fork. The SYSFILE environment of the calling procedure is not available in this subshell. This can have an effect on the BS2000 commands which are called with bs2cmd, and on the POSIX commands lp, lpstat and cancel.
Example
/begin-block / ecxcmd 'bs2cmd sh-sys-file-ass \*syscmd' /end-block PROCEDURE LEVEL NUMBER 0 SYSCMD : (PRIMARY) /
but cf.:
/begin-block / start-posix-shell /end-block POSIX basic shell ... *bs2cmd sh-sys-file-ass \*syscmd PROCEDURE LEVEL NUMBER 1 SYSCMD : *PRIMARY (DIALOG-BLOCK) *
You must press the K2 key twice in order to abort EXECUTE-POSIX-CMD.
Command return codes
(SC2) | SC1 | Maincode | Meaning |
0 | CMD0001 | No errors | |
1 | CMD0202 | Error when calling procedure ECXCMD (see remark on SDF error CMD0065 above) | |
x | 64 | CCM0999 | The shell command, command sequence or script supplies an exit status with the value x (not equal 0) which can be obtained from the SC2 |