Sun Studio 12: Fortran Library Reference

1.4.45 sh: Fast Execution of an sh Command

The function is called by:

INTEGER*4 sh

status = sh( string )

string

character*n

Input 

String containing command to do 

Return value 

INTEGER*4

Output 

Exit status of the shell executed. See wait(2) for an explanation of this value.

Example: sh():


       character*18 string / ’ls > MyOwnFile.names’ /
       INTEGER*4 status, sh
       status = sh( string )
       if ( status .ne. 0 ) stop ’sh: error’
       ...
       end

The function sh passes string to the sh shell as input, as if the string had been typed as a command.

The current process waits until the command terminates.

The functions sh(3f) and system(3f) pass the argument string to a shell for execution. They convert the argument string from a Fortran character value to a C string value and pass it to the C routine system(3c). The routines sh(3f) and system(3f) differ in that system flushes the Fortran I/O buffers before calling the C routine system, while sh does not. Flushing the buffers can take significant time, and so, if any Fortran output is irrelevant to the result of the call, the routine sh is preferred over the routine system.

The sh() function is not MT-safe. Do not call it from multithreaded or parallelized programs.

See also: execve(2), wait(2), and system(3c).

Note: string cannot be longer than 1,024 characters.