Generate and return failed = .false.
if the output of the input system command is successfully retrieved in the allocatable
argument output
.
Otherwise, return failed = .true.
indicating the occurrence of an error.
To do so, the procedures under this generic interface call the system to run the requested command while redirecting its output to a temporary file for subsequent retrieval and return to the user.
- Warning
- Use this functionality responsibly and with caution.
Due to their simplicity, the procedures under this generic interface have high potential of abuse by passing system commands that can wreak havoc.
The responsible usage of this functionality must be currently guaranteed by the user.
- Parameters
-
[in] | command | : The input scalar character of default kind SK containing the system command whose output must be retrieved. |
[out] | output | : The output allocatable scalar character of default kind SK containing the output from the system command call.
The output may contain the text redirected to the standard error if the specified command fails, even though the processor calls it and returns successfully.
This can be verified by testing whether the optional output argument exitstat is non-zero (implying the failure of the specified command to return successfully).
The allocation status of output remains undefined if the procedure returns failed set to .true. .
Always check the returned value of failed before using output .
This behavior is sensible because when the procedure fails, the contents of output is garbage and should not be subsequently used.
|
[in,out] | errmsg | : The input/output scalar character of default kind SK of arbitrary length type parameter.
If an error occurs, errmsg will be set to a descriptive message about the nature of the runtime error.
A length of LEN_IOMSG characters is likely sufficient to capture most error messages in full.
(optional. If missing, no error message will be output if an error occurs.) |
[out] | exitstat | : The output scalar integer of default kind IK, representing the exit code returned by the input command .
For more information, see the corresponding argument of isFailedExec.
The specific returned value of exitstat is processor and application dependent.
However, a zero exitstat frequently implies the specified command returned the control successfully to the processor, while non-zero exitstat implies command failure.
See also the corresponding output argument of isFailedExec.
(optional, If missing, user may find it difficult to tell whether the contents of output are from stdout or strerr.) |
- Returns
failed
: The output scalar logical
of default kind LK. It is .true.
if and only if the process of retrieving the command output fails at any point.
Possible calling interfaces ⛓
Generate and return failed = .false. if the output of the input system command is successfully retrie...
This module contains procedures and generic interfaces for inferring the runtime system shell type an...
- Warning
- User must always verify the function output
failed
is .false.
before using the output
.
Example usage ⛓
10 character(
255, SK) :: errmsg
= SK_
""
11 character(:, SK),
allocatable :: output
13 type(display_type) :: disp
17 call disp%show(
"failed = isFailedGetOutput(SK_'ls', output) ! List all files in the current directory (works on POSIX-compliant systems).")
19 call disp%show(
"failed ! Check if any error has occurred.")
21 call disp%show(
.not."if ( failed) call disp%show( output , deliml = SK_'''' )")
22 if (
.not. failed)
call disp%show( output , deliml
= SK_
'''' )
26 call disp%show(
"failed = isFailedGetOutput(SK_'dir', output) ! List all files in the current directory (works on Windows-compliant systems).")
28 call disp%show(
"failed ! Check if any error has occurred.")
30 call disp%show(
.not."if ( failed) call disp%show( output , deliml = SK_'''' )")
31 if (
.not. failed)
call disp%show( output , deliml
= SK_
'''' )
35 call disp%show(
"failed = isFailedGetOutput(SK_'if shell syntax error happens, output remains unallocated.', output, errmsg).")
36 failed
= isFailedGetOutput(SK_
'if shell syntax error happens, output remains unallocated.', output, errmsg)
37 call disp%show(
"failed ! Check if any error has occurred.")
39 call disp%show(
"if (failed) then; call disp%show( trim(errmsg) , deliml = SK_'''' ); else; call disp%show( output , deliml = SK_'''' ); end if")
40 if (failed)
then;
call disp%show(
trim(errmsg) , deliml
= SK_
'''' );
else;
call disp%show( output , deliml
= SK_
'''' );
end if
44 call disp%show(
"failed = isFailedGetOutput(SK_'ls', output)")
46 call disp%show(
"failed ! Check if any error has occurred.")
48 call disp%show(
.not."if ( failed) call disp%show( output , deliml = SK_'''' )")
49 if (
.not. failed)
call disp%show( output , deliml
= SK_
'''' )
53 call disp%show(
"failed = isFailedGetOutput(SK_'ls -lhtr', output, errmsg) ! Gracefully handle runtime errors and capture the error message.")
55 call disp%show(
"failed ! Check if any error has occurred.")
57 call disp%show(
"if (failed) then; call disp%show( trim(errmsg) , deliml = SK_'''' ); else; call disp%show( output , deliml = SK_'''' ); end if")
58 if (failed)
then;
call disp%show(
trim(errmsg) , deliml
= SK_
'''' );
else;
call disp%show( output , deliml
= SK_
'''' );
end if
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
5if (
.not. failed)
call disp%show( output , deliml
= SK_
'''' )
20if (
.not. failed)
call disp%show( output , deliml
= SK_
'''' )
21'CMakeCache.txt CMakeLists.txt build.sh main.F90 main.out.F90
22CMakeFiles Makefile cmake_install.cmake main.exe'
25failed
= isFailedGetOutput(SK_
'if shell syntax error happens, output remains unallocated.', output, errmsg).
28if (failed)
then;
call disp%show(
trim(errmsg) , deliml
= SK_
'''' );
else;
call disp%show( output , deliml
= SK_
'''' );
end if
29'@pm_sysShell@isFailedGetOutputMsg()@line(241): Failed to fetch command output. Cannot open file '/tmp
/isFailedGetOutputMsg.tmp.gthaqhufxilpycbsswoh
': No such file or directory'
35if (
.not. failed)
call disp%show( output , deliml
= SK_
'''' )
50if (failed)
then;
call disp%show(
trim(errmsg) , deliml
= SK_
'''' );
else;
call disp%show( output , deliml
= SK_
'''' );
end if
52-rw-r--r-- 1 amir amir 3.1K Mar 14 2024 main.F90
53-rw-r--r-- 1 amir amir 1.9K Oct 27 21:18 cmake_install.cmake
54-rw-r--r-- 1 amir amir 13K Oct 27 21:18 CMakeCache.txt
55-rwxr-xr-x 1 amir amir 76 Oct 27 22:30 build.sh
56-rwxr-xr-x 1 amir amir 1.3K Oct 27 22:30 CMakeLists.txt
57-rw-r--r-- 1 amir amir 6.1K Oct 27 22:43 Makefile
58-rwxr-xr-x 1 amir amir 18K Oct 27 22:43 main.exe
59drwxr-xr-x 7 amir amir 4.0K Oct 27 22:43 CMakeFiles
60-rw-r--r-- 1 amir amir 1.6K Oct 27 22:43 main.out.F90'
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.
-
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.
-
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.
- Copyright
- Computational Data Science Lab
- Todo:
- Low Priority: This generic interface should be extended to support character
output
of non-default kind once non-default characters is support by all relevant Fortran compilers.
- Author:
- Amir Shahmoradi, Tuesday March 7, 2017, 3:50 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin
Definition at line 586 of file pm_sysShell.F90.