Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
16 Views

How to use execute_command_line cmdmsg

All,

I'm hoping you can help with this. I just tried this simple program:

program test
   implicit none

   integer :: estat, cstat
   character(len=255) :: cmsg

   estat = -999
   cstat = -999
   cmsg  = "notfilled"

   call execute_command_line("/bin/cp ~/yayay ~/alskalkd", &
      exitstat=estat, cmdstat=cstat, cmdmsg=cmsg)
   write (*,*) 'estat: ', estat
   write (*,*) 'cstat: ', cstat
   write (*,*) "cmsg:  ", trim(cmsg)

end program test

Nothing too fancy or anything. I do not have a file called "yayay" in my home, so I expected to see cmdmsg to have something and yet:

(276) $ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 17.0.0.098 Build 20160721
Copyright (C) 1985-2016 Intel Corporation.  All rights reserved.

(277) $ ifort test.F90 && ./a.out
/bin/cp: cannot stat ‘/home/mathomp4/yayay’: No such file or directory
 estat:            1
 cstat:            0
 cmsg:  notfilled

So, we see that cp failed (as it should) but cmdmsg was never changed. I suppose I'm wondering, given the standard:

CMDMSG (optional) shall be a default character scalar. It is an INTENT (INOUT) argument. If an error condi-
tion occurs, it is assigned a processor-dependent explanatory message. Otherwise, it is unchanged.

have I encountered the dreaded "processor-dependent" result of nothing? EXITSTAT seems to have been set to non-zero (which is the return status of the cp call), but...no cmdmsg.

Currently 9th Place in Fortran Code Golf
0 Kudos
7 Replies
Highlighted
Black Belt
16 Views

No - in this context an error

No - in this context an error condition refers to something that prevents the command from being executed  - for example if `/bin/cp` didn't exist.  See the description of CMDSTAT. 

In your example, `/bin/cp` was able to be executed - so CMDSTAT is zero, CMDMSG is unchanged, and EXITSTAT has been defined with the exit status of `/bin/cp`, as expected.  It is up to your program to interpret that EXITSTAT.

0 Kudos
Highlighted
Beginner
16 Views

Quote:ianh wrote:

ianh wrote:

No - in this context an error condition refers to something that prevents the command from being executed  - for example if `/bin/cp` didn't exist.  See the description of CMDSTAT. 

In your example, `/bin/cp` was able to be executed - so CMDSTAT is zero, CMDMSG is unchanged, and EXITSTAT has been defined with the exit status of `/bin/cp`, as expected.  It is up to your program to interpret that EXITSTAT.

Okay, let's say I change my program to:

   call execute_command_line("/bin/alsdjfasjdflasjdfa", &
      exitstat=estat, cmdstat=cstat, cmdmsg=cmsg)

Now, let's see:

(284) $ /bin/alsdjfasjdflasjdfa
/bin/alsdjfasjdflasjdfa: Command not found.
(285) $ ifort test.F90 && ./a.out
sh: /bin/alsdjfasjdflasjdfa: No such file or directory
Segmentation fault (core dumped)

Ummm...is that expected?

Currently 9th Place in Fortran Code Golf
0 Kudos
Highlighted
16 Views

From what I can tell, with

From what I can tell, with the latter command line the "system" command used by EXECUTE_COMMAND_LINE doesn't return. I don't get a segmentation fault, just a hung program.

Retired 12/31/2016
0 Kudos
Highlighted
Beginner
16 Views

Quote:Steve Lionel (Intel)

Steve Lionel (Intel) wrote:

From what I can tell, with the latter command line the "system" command used by EXECUTE_COMMAND_LINE doesn't return. I don't get a segmentation fault, just a hung program.

I guess that's better than a core dump. I suppose until all compilers support EXECUTE_COMMAND_LINE, it's sort of a moot point, but I'd really love to get rid of the tangle of ifdefs I have in Fortran those programs that use system. At least iargc and its brethren are no longer needed...

Currently 9th Place in Fortran Code Golf
0 Kudos
Highlighted
16 Views

Does it work ok for valid

Does it work ok for valid commands?

Retired 12/31/2016
0 Kudos
Highlighted
Beginner
16 Views

Quote:Steve Lionel (Intel)

Steve Lionel (Intel) wrote:

Does it work ok for valid commands?

It does. Our thought here was we were seeing some issue with a system call that wasn't working, but the output/error was being suppressed or moved away some how. Luckily for us, the commands we are issuing are more like the first style (bad cp) so that we can rely on exitstat to test for issues. 

I was just really hoping cmdmsg worked in all errors. I love IOMSG so much and it's so useful, that anything like it I will use!

Currently 9th Place in Fortran Code Golf
0 Kudos
Highlighted
16 Views

As Ian says, CMDMSG only

As Ian says, CMDMSG only tells you if there was an issue executing the command, not anything about what the command itself does. In fact, it would have no way of interpreting the exit status.

Retired 12/31/2016
0 Kudos