 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
arctica
Joined: 10 Sep 2006 Posts: 146 Location: United Kingdom
|
Posted: Wed Jan 29, 2025 4:46 pm Post subject: Running command line programs |
|
|
Hello,
I have tested the following code (ftn95 v9.06), which works using system but fails with execute_command_line:
Code: | program dos_cmd
integer :: i
character(len=512) :: cmd
!Input string using programs grdinfo, piping and gawk (programs in PATH)
!Get header info from a netCDF file using GMT
cmd = 'grdinfo Bathy_crop1_scaled.grd -C | ' // &
'gawk "{xmin=$2; xmax=$3; ymin=$4; ymax=$5; nx=$10; ny=$11};' // &
'{print xmin, xmax, ymin, ymax, 2*nx, 2*ny}" > nxy.txt'
print *, "Executing command:"
print *, trim(cmd)
!test alternate command-line calls
call system(cmd) ! works
call execute_command_line(cmd, exitstat=i) ! fails (F2008 intrinsic)
print *, "Exit status of external_prog.exe was ", i
end program dos_cmd
|
system(cmd) correctly writes nxy.txt. However execute_command_line(cmd,exitstat=i) fails with run-time error:
Code: | Executing command:
grdinfo Bathy_crop1_scaled.grd -C | gawk "{xmin=$2; xmax=$3; ymin=$4; ymax=$5; nx=$10; ny=$11};{print xmin, xmax, ymin,
ymax, 2*nx, 2*ny}" > nxy.txt
grdinfo [ERROR]: Cannot find file |
grdinfo [ERROR]: Cannot find file gawk
grdinfo [ERROR]: Cannot find file {xmin
Exit status of external_prog.exe was 72 |
Any ideas why the second call fails? grdinfo, gawk all sit in the windows PATH.
Not a big issue, just curious to know why there is a difference in behaviour.
Lester |
|
Back to top |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 801 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Wed Jan 29, 2025 8:20 pm Post subject: |
|
|
What do the optional arguments CMDSTAT, and CMDMSG return? |
|
Back to top |
|
 |
arctica
Joined: 10 Sep 2006 Posts: 146 Location: United Kingdom
|
Posted: Thu Jan 30, 2025 2:02 am Post subject: |
|
|
Hi Ken,
Adding the cmdstat and cmdmsg options gives:
Code: | Executing command:
grdinfo Bathy_crop1_scaled.grd -C | gawk "{xmin=$2; xmax=$3; ymin=$4; ymax=$5; nx=$10; ny=$11};{print xmin, xmax, ymin,
ymax, 2*nx, 2*ny}" > nxy.txt
grdinfo [ERROR]: Cannot find file |
grdinfo [ERROR]: Cannot find file gawk
grdinfo [ERROR]: Cannot find file {xmin
Exit status of external_prog.exe was 72
cmdstat = 0
cmdmsg =�
�@��@h�`�p���@
��8�8�
�
�PCNAME
PCNAME
@ |
The extra options, i, j (integers) and csmg (character)
Code: | call execute_command_line(cmd, exitstat=i, cmdstat=j, cmdmsg=csmg) ! fails (F2008 intrinsic) |
|
|
Back to top |
|
 |
wahorger

Joined: 13 Oct 2014 Posts: 1255 Location: Morrison, CO, USA
|
Posted: Thu Jan 30, 2025 2:13 am Post subject: |
|
|
A general problem might be the use of the pipe.
Windows does not operate like Linux, specifically that a pipe does not concurrently run at the same time as the "sending" command.
Or, so I have been told/read. |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8184 Location: Salford, UK
|
Posted: Thu Jan 30, 2025 10:07 am Post subject: |
|
|
EXECUTE_COMMAND_LINE has an optional logical argument WAIT which when present and set .FALSE. executes the command asynchronously.
But I think that SYSTEM is the same as EXECUTE_COMMAND_LINE with WAIT set .TRUE. which is the default. |
|
Back to top |
|
 |
arctica
Joined: 10 Sep 2006 Posts: 146 Location: United Kingdom
|
Posted: Thu Jan 30, 2025 1:12 pm Post subject: |
|
|
The solution when using execute_command_line(cmd) is to wrap the command in a shell invoccation, as that was the only way to get it to understand piping and redirection.
Code: | cmd2 = 'cmd.exe /c "grdinfo Bathy_crop1_scaled.grd -C | ' // &
'gawk "{xmin=$2; xmax=$3; ymin=$4; ymax=$5; nx=$10; ny=$11};' // &
'{print xmin, xmax, ymin, ymax, 2*nx, 2*ny}" > nxy_test.txt" ' |
The output of exitstat and cmdstat are 0 (process succeds), however the output from cmdmsg is garbage
As you say Paul, SYSTEM and EXECUTE_COMMAND_LINE do the same thing.
Lester |
|
Back to top |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 801 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Thu Jan 30, 2025 2:45 pm Post subject: |
|
|
Lester,
cmdmsg has intent(inout) so needs to be initialised to " ".
Code: | program p
implicit none
integer :: cstat, estat
character(100) :: cmsg
cmsg = ' '
! This will fail since xxx.exe does not exist
call execute_command_line ("xxx.exe", wait=.true., exitstat=estat, cmdstat=cstat, cmdmsg=cmsg)
if (cstat .gt. 0) print *, "Command execution failed with error " , trim( cmsg )
end |
Returns
Code: | Command execution failed with error Failed to create process. |
If an error condition occurs, cmdmsg is assigned a explanatory message. Otherwise, cmdmsg is unchanged. |
|
Back to top |
|
 |
arctica
Joined: 10 Sep 2006 Posts: 146 Location: United Kingdom
|
Posted: Thu Jan 30, 2025 3:44 pm Post subject: |
|
|
Thanks for the information on cmdmsg Ken, that helps to follow the logic. |
|
Back to top |
|
 |
DanRRight
Joined: 10 Mar 2008 Posts: 2911 Location: South Pole, Antarctica
|
Posted: Thu Jan 30, 2025 4:04 pm Post subject: |
|
|
I always thought that this error is an indication and bad example of users being lazy procrastinators not reporting bugs some of which are deeply hidden and living for centuries not exposed. This bug will outlive us all together. Impression is that the developers also tried to find out why this happening and seems found nothing suspicious.
And the pure EXECUTE_COMMAND_LINE not just does not work sometimes, it can do the whole flurry of unimaginable things. That clearly is one of those proverbial "devilry" cases cured only with the large amount users and their active reporting.
Meantime as usually we have only workarounds... workarounds.
The only way EXECUTE_COMMAND_LINE finally started working amicably in my cases was when Paul ones suggested to use it in a subroutine isolated like this
Code: | subroutine LaunchH5dump(iThread)
call EXECUTE_COMMAND_LINE(text256, exitstat=ierr4)
if(ierr4.ne.0) then
call sound@(2000,1)
print*,'Sound 2000 error launching h5dump in thread . text256=', trim(text256), iThread
endif
end subroutine |
and then launch it via thread launcher which is calling this subroutine
Code: | ii = START_THREAD@(LaunchH5dump,1)
call wait_for_thread@ (0) |
|
|
Back to top |
|
 |
arctica
Joined: 10 Sep 2006 Posts: 146 Location: United Kingdom
|
Posted: Wed Feb 12, 2025 6:05 pm Post subject: |
|
|
Quick update.
Came across an old bit of code I had and this command works without issue for a long string:
Code: | call cissue@('grdinfo xyz-test-gmt.grd -C | ' // &
'gawk "{xmin=$2; xmax=$3; ymin=$4; ymax=$5; nx=$10; ny=$11};' // &
'{print xmin, xmax, ymin, ymax, 2*nx, 2*ny}" > nxy.txt', fail) |
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|