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

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. More...

Detailed Description

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

failed = isFailedGetOutput(command, output, exitstat = exitstat)
failed = isFailedGetOutput(command, output, errmsg, exitstat = exitstat)
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...
Definition: pm_sysShell.F90:38
Warning
User must always verify the function output failed is .false. before using the output.


Example usage

1program example
2
3 use pm_kind, only: LK, IK, SK
4 use pm_io, only: display_type
6
7 implicit none
8
9 logical(LK) :: failed
10 character(255, SK) :: errmsg = SK_""
11 character(:, SK), allocatable :: output
12
13 type(display_type) :: disp
14 disp = display_type(file = "main.out.F90")
15
16 call disp%skip()
17 call disp%show("failed = isFailedGetOutput(SK_'ls', output) ! List all files in the current directory (works on POSIX-compliant systems).")
18 failed = isFailedGetOutput(SK_'ls', output)
19 call disp%show("failed ! Check if any error has occurred.")
20 call disp%show( failed )
21 call disp%show(.not."if ( failed) call disp%show( output , deliml = SK_'''' )")
22 if (.not. failed) call disp%show( output , deliml = SK_'''' )
23 call disp%skip()
24
25 call disp%skip()
26 call disp%show("failed = isFailedGetOutput(SK_'dir', output) ! List all files in the current directory (works on Windows-compliant systems).")
27 failed = isFailedGetOutput(SK_'dir', output)
28 call disp%show("failed ! Check if any error has occurred.")
29 call disp%show( failed )
30 call disp%show(.not."if ( failed) call disp%show( output , deliml = SK_'''' )")
31 if (.not. failed) call disp%show( output , deliml = SK_'''' )
32 call disp%skip()
33
34 call disp%skip()
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.")
38 call disp%show( failed )
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
41 call disp%skip()
42
43 call disp%skip()
44 call disp%show("failed = isFailedGetOutput(SK_'ls', output)")
45 failed = isFailedGetOutput(SK_'ls', output)
46 call disp%show("failed ! Check if any error has occurred.")
47 call disp%show( failed )
48 call disp%show(.not."if ( failed) call disp%show( output , deliml = SK_'''' )")
49 if (.not. failed) call disp%show( output , deliml = SK_'''' )
50 call disp%skip()
51
52 call disp%skip()
53 call disp%show("failed = isFailedGetOutput(SK_'ls -lhtr', output, errmsg) ! Gracefully handle runtime errors and capture the error message.")
54 failed = isFailedGetOutput(SK_'ls -lhtr', output, errmsg)
55 call disp%show("failed ! Check if any error has occurred.")
56 call disp%show( failed )
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
59 call disp%skip()
60
61end 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
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
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
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
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
2failed = isFailedGetOutput(SK_'ls', output) ! List all files in the current directory (works on POSIX-compliant systems).
3failed ! Check if any error has occurred.
4F
5if (.not. failed) call disp%show( output , deliml = SK_'''' )
6'CMakeCache.txt
7CMakeFiles
8CMakeLists.txt
9Makefile
10build.sh
11cmake_install.cmake
12main.F90
13main.exe
14main.out.F90'
15
16
17failed = isFailedGetOutput(SK_'dir', output) ! List all files in the current directory (works on Windows-compliant systems).
18failed ! Check if any error has occurred.
19F
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'
23
24
25failed = isFailedGetOutput(SK_'if shell syntax error happens, output remains unallocated.', output, errmsg).
26failed ! Check if any error has occurred.
27T
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.hjdsapauibotereswfhd': No such file or directory'
30
31
32failed = isFailedGetOutput(SK_'ls', output)
33failed ! Check if any error has occurred.
34F
35if (.not. failed) call disp%show( output , deliml = SK_'''' )
36'CMakeCache.txt
37CMakeFiles
38CMakeLists.txt
39Makefile
40build.sh
41cmake_install.cmake
42main.F90
43main.exe
44main.out.F90'
45
46
47failed = isFailedGetOutput(SK_'ls -lhtr', output, errmsg) ! Gracefully handle runtime errors and capture the error message.
48failed ! Check if any error has occurred.
49F
50if (failed) then; call disp%show( trim(errmsg) , deliml = SK_'''' ); else; call disp%show( output , deliml = SK_'''' ); end if
51'total 68K
52-rw-r--r-- 1 amir amir 3.1K Mar 14 05:44 main.F90
53-rw-r--r-- 1 amir amir 1.9K May 18 20:52 cmake_install.cmake
54-rw-r--r-- 1 amir amir 13K May 18 20:52 CMakeCache.txt
55-rwxr-xr-x 1 amir amir 76 Aug 22 21:08 build.sh
56-rwxr-xr-x 1 amir amir 1.3K Aug 22 21:08 CMakeLists.txt
57-rw-r--r-- 1 amir amir 6.1K Aug 22 21:15 Makefile
58-rwxr-xr-x 1 amir amir 18K Aug 22 21:15 main.exe
59drwxr-xr-x 7 amir amir 4.0K Aug 22 21:15 CMakeFiles
60-rw-r--r-- 1 amir amir 1.6K Aug 22 21:15 main.out.F90'
61
62


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.

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 at Austin

Definition at line 586 of file pm_sysShell.F90.


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