ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_sysShell::isFailedExec Interface Reference

Generate and return failed = .false. if the attempt to run the requested shell command is successful.
Otherwise, return failed = .true. indicating the occurrence of an error. More...

Detailed Description

Generate and return failed = .false. if the attempt to run the requested shell command is successful.
Otherwise, return failed = .true. indicating the occurrence of an error.

This procedure provides a convenient functional interface to the Fortran intrinsic execute_command_line() by automatically interpreting the output error codes and reporting the failure as the function output if it occurs.

Parameters
[in]command: The input scalar character of default kind SK containing the system shell command to run.
[in]wait: The input scalar logical of default kind LK with the same functionality as the wait argument of the Fortran intrinsic execute_command_line().
  • If .true., the procedure will wait for the command to terminate before returning the control to the Fortran program.
  • Otherwise, the process of running the requested shell command will be done asynchronously.
(optional, default = .true.)
[in,out]exitstat: The input/output scalar integer of default kind IK with the same functionality as the exitstat argument of the Fortran intrinsic execute_command_line().
On output, it is assigned the process exit status from the command, only if wait = .true.. If wait = .false., the value of exitstat remains untouched.
The value and meaning of this output argument is system dependent, but a non-zero value typically indicates the occurrence of an error within the execution of the specified command.
(optional. If missing, no exit status will be returned.)
[out]cmdstat: The output scalar integer of default kind IK with the same functionality as the cmdstat argument of the Fortran intrinsic execute_command_line().
On output, it is assigned
  • 0 if the process of running the requested shell command terminates without error,
  • -1 if the processor does not support command execution,
  • -2 if wait = .true. is specified but the processor does not support asynchronous command execution,
  • a positive value if any other error occurs.
(optional. If missing, no cmdstat will be returned.)
[in,out]cmdmsg: The input/output scalar character of default kind SK of arbitrary length type parameter the same functionality as the cmdmsg argument of the Fortran intrinsic execute_command_line().
If present and the output cmdstat is assigned a positive value, then cmdmsg will be assigned an explanatory message describing the nature of the error that has occurred.
If the length of cmdstat is too short for the output error message, the message tail will be clipped as needed.
A length of LEN_IOMSG characters for cmdmsg is likely enough to capture most error messages in full.
(optional. If missing, no error message will be output.)
Returns
failed : The output scalar logical of default kind LK. It is .true. if and only if an error occurs while running the command.
The failure is determined by checking for a non-zero value of cmdstat.
The specific value of exitstat depends on both the processor and the specified value for the input argument wait.
Moreover, the processor may execute the specified command without failure even though exitstat indicates the occurrence of an error within the command.
As such, the output value of exitstat has no impact on the value of the output failed.


Possible calling interfaces

use pm_kind, only: LK
logical(LK) :: failed
failed = isFailedExec(command, wait = wait, exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)
Generate and return failed = .false. if the attempt to run the requested shell command is successful....
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
This module contains procedures and generic interfaces for inferring the runtime system shell type an...
Definition: pm_sysShell.F90:38
Warning
This procedure is prone to abuse by requesting to run system commands with devastating consequences.
Remarks
The procedures under discussion are impure.
The procedures under discussion are elemental.
See also
shell_type


Example usage

1program example
2
3 use pm_kind, only: LK, IK, SK
4 use pm_io, only: display_type
7
8 implicit none
9
10 character(:, SK), allocatable :: file
11 character(255, SK) :: cmdmsg = SK_""
12 integer(IK) :: cmdstat
13 integer(IK) :: exitstat = -huge(0_IK)
14 logical(LK) :: cmdExecFailed
15
16 type(display_type) :: disp
17 disp = display_type(file = "main.out.F90")
18
19 call disp%skip()
20 call disp%show("cmdExecFailed = isFailedExec(SK_'')")
21 cmdExecFailed = isFailedExec(SK_'')
22 call disp%show("cmdExecFailed")
23 call disp%show( cmdExecFailed )
24 call disp%skip()
25
26 call disp%skip()
27 call disp%show("cmdExecFailed = isFailedExec(SK_'ls')")
28 cmdExecFailed = isFailedExec(SK_'ls')
29 call disp%show("cmdExecFailed")
30 call disp%show( cmdExecFailed )
31 call disp%skip()
32
33 call disp%skip()
34 call disp%show("file = getPathNew() ! Create a file name unique in the current directory.")
35 file = getPathNew()
36 call disp%show("file")
37 call disp%show( file , deliml = SK_"""" )
38 call disp%show("isFile(file)")
39 call disp%show( isFile(file) )
40 call disp%show("cmdExecFailed = isFailedExec(SK_'echo 2>'//file) ! Create an empty file in the current directory.")
41 cmdExecFailed = isFailedExec(SK_'echo 2>'//file)
42 call disp%show("cmdExecFailed")
43 call disp%show( cmdExecFailed )
44 call disp%show("isFile(file)")
45 call disp%show( isFile(file) )
46 call disp%skip()
47
48 call disp%skip()
49 call disp%show("cmdExecFailed = isFailedExec(SK_'ls', wait = .false._LK) ! run the Bash command asynchronously.")
50 cmdExecFailed = isFailedExec(SK_'ls', wait = .false._LK)
51 call disp%show("cmdExecFailed")
52 call disp%show( cmdExecFailed )
53 call disp%skip()
54
55 call disp%skip()
56 call disp%show("cmdExecFailed = isFailedExec(SK_'', exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)")
57 cmdExecFailed = isFailedExec(SK_'', exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)
58 call disp%show("cmdExecFailed")
59 call disp%show( cmdExecFailed )
60 call disp%show("exitstat")
61 call disp%show( exitstat )
62 call disp%show("cmdstat")
63 call disp%show( cmdstat )
64 call disp%show("trim(cmdmsg)")
65 call disp%show( trim(cmdmsg) , deliml = SK_"""" )
66 call disp%skip()
67
68 call disp%skip()
69 call disp%show("cmdExecFailed = isFailedExec(SK_'dir', exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)")
70 cmdExecFailed = isFailedExec(SK_'dir', exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)
71 call disp%show("cmdExecFailed")
72 call disp%show( cmdExecFailed )
73 call disp%show("exitstat")
74 call disp%show( exitstat )
75 call disp%show("cmdstat")
76 call disp%show( cmdstat )
77 call disp%show("trim(cmdmsg)")
78 call disp%show( trim(cmdmsg) , deliml = SK_"""" )
79 call disp%skip()
80
81end program example
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Generate and return a unique (directory or file) path name in the specified directory or the default ...
Generate and return .true. is the input path is a file (not a directory), otherwise return ....
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
This module contains classes and procedures for manipulating system file/folder paths.
Definition: pm_sysPath.F90:274
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2cmdExecFailed = isFailedExec(SK_'')
3cmdExecFailed
4F
5
6
7cmdExecFailed = isFailedExec(SK_'ls')
8cmdExecFailed
9F
10
11
12file = getPathNew() ! Create a file name unique in the current directory.
13file
14"new_20241027_224352_102_pid_1"
15isFile(file)
16F
17cmdExecFailed = isFailedExec(SK_'echo 2>'//file) ! Create an empty file in the current directory.
18cmdExecFailed
19F
20isFile(file)
21T
22
23
24cmdExecFailed = isFailedExec(SK_'ls', wait = .false._LK) ! run the Bash command asynchronously.
25cmdExecFailed
26F
27
28
29cmdExecFailed = isFailedExec(SK_'', exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)
30cmdExecFailed
31F
32exitstat
33+0
34cmdstat
35+0
36trim(cmdmsg)
37""
38
39
40cmdExecFailed = isFailedExec(SK_'dir', exitstat = exitstat, cmdstat = cmdstat, cmdmsg = cmdmsg)
41cmdExecFailed
42F
43exitstat
44+0
45cmdstat
46+0
47trim(cmdmsg)
48""
49
50
Test:
test_pm_sysShell
Todo:
Low Priority: The current Fortran standard 202x does not allow passing characters of non-default kind to Fortran intrinsic procedures.
This generic interface should be extended to in the future to support non-default character kinds when they fully supported by the Fortran intrinsic procedures.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, Tuesday March 7, 2017, 3:09 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin

Definition at line 1141 of file pm_sysShell.F90.


The documentation for this interface was generated from the following file: