# Execute\_command\_line problem

**URL:** <https://fortran-lang.discourse.group/t/execute-command-line-problem/7151>\
**Category:** Help\
**Created:** [January 9, 2024, 10:49pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151 "2024-01-09T22:49:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [January 9, 2024, 10:49pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/1 "2024-01-09T22:49:29Z")

</div>

This program works well with ifort, ifx and AMD flang, but with gfortran in my system

```auto
gcc version 13.1.0 (Ubuntu 13.1.0-8ubuntu1~22.04)

```

it prints nothing at run-time and has to be stopped manually. Is the bug in my program or gfortran? The program:

```auto
program test
! f2008 using execute_command_line and assuming Linux
  implicit none
  print "(A,L2)", 'I am john',iam('john')
  print "(A,L2)", 'I am JOHN',iam('JOHN')

contains

  logical function iam( name)
    character(*),intent(in)::name
    integer estat
    character(len(name)+38):: cmd
    cmd = 'if [`whoami` != "'//name//'"]; then exit 1; fi'
    call execute_command_line(cmd,exitstat=estat)
    iam = (estat==0)
  end function iam
end program test

```

The output from flang, ifx or ifort:

```auto
I am john T
I am JOHN F

```

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [January 10, 2024, 7:41am UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/2 "2024-01-10T07:41:16Z")

</div>

You have named the program “test”, is that also the name of the executable? I remember from a grey past that such a name may confuse the shell.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [January 10, 2024, 9:42am UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/3 "2024-01-10T09:42:51Z")

</div>

This is the same behavior that one sees when recursive i/o is invoked. Based on that observation, if you change the main program to

```auto
   logical :: q
   q = iam('john')
   print "(A,L2)", 'I am john', q
   q = iam('JOHN')
   print "(A,L2)", 'I am JOHN', q

```

then I think the code will work (it does on MacOS+gfortran). Of course, the code is not doing recursive i/o, so this appears to be some kind of compiler or library bug.

[edit] I experimented a little more with this. It appears that the error has nothing to do with the `execute_command_line` intrinsic. If you comment out that call, the program still hangs. I also experimented a little with constructing the `cmd` string with substring indexing and assignments,

```auto
    cmd(1:18) = 'if [ `whoami` != "'
    cmd(18+1:18+len(name)) = name
    cmd(19+len(name):) = '" ]; then exit 1; fi'

```

and the program still hangs. If you eliminate `cmd`, and just insert the expression as the subroutine argument, it still hangs. I don’t see anything wrong with either the original code or any of these modified versions, they all look alright to my eye. One can disagree with the concept of executing functions just for side effects (i.e. a subroutine might be better), but the language allows this so it looks like it should work.

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [January 10, 2024, 7:52pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/4 "2024-01-10T19:52:46Z")

</div>

No @Arjen I never call an executable “test”. I shall report the bug to gfortran.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [January 10, 2024, 7:59pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/5 "2024-01-10T19:59:56Z")

</div>

> [@RonShepard](#):
>
> It appears that the error has nothing to do with the `execute_command_line` intrinsic. If you comment out that call, the program still hangs.

Not on my side:

```bash
/tmp$ gfortran test.f90 && ./a.out
I am john F
I am JOHN F

```

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [January 10, 2024, 8:07pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/6 "2024-01-10T20:07:25Z")

</div>

> [@vmagnin](#):
>
> Not on my side:

I should have said, “If I comment out that call…”

Did the `logical :: q` version work for you?

I hope this gets fixed quickly, this seems like a dangerous land mine to have sitting somewhere in the compiler.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [January 10, 2024, 8:10pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/7 "2024-01-10T20:10:02Z")

</div>

A minimalist version, hanging on the second call (in the print):

```fortran
program test
  implicit none
  logical :: foo

  ! OK:
  call execute_command_line("export A=3")

  foo = iam()
  ! OK:
  print "(L2)", foo
  ! NOK (with gfortran):
  print "(L2)", iam()

contains

  logical function iam()
    call execute_command_line("export A=3")
  end function iam

end program test

```

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [January 10, 2024, 8:19pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/8 "2024-01-10T20:19:55Z")

</div>

Trying to be even more minimalist, I tried to pass an empty string to `execute_command_line()`:

```fortran
call execute_command_line("")
end

```

and discovered another difference between GFortran and ifx:

```bash
/tmp$ ifx test2.f90 && ./a.out
forrtl: severe (124): Invalid command supplied to EXECUTE_COMMAND_LINE
Image PC Routine Line Source             
a.out 00000000004051A3 Unknown Unknown Unknown
a.out 000000000040515D Unknown Unknown Unknown
libc.so.6 00007F93106280D0 Unknown Unknown Unknown
libc.so.6 00007F9310628189 __libc_start_main Unknown Unknown
a.out 0000000000405075 Unknown Unknown Unknown
/tmp$ gfortran test2.f90 && ./a.out
/tmp$

```

If the string contains one space, ifx is OK.

Page 367, the Fortran 2018 standards states:

> COMMAND shall be a default character scalar. It is an INTENT (IN) argument. Its value is the command line to be executed. The interpretation is processor dependent.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [January 10, 2024, 11:01pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/9 "2024-01-10T23:01:12Z")

</div>

> [@vmagnin](#):
>
> ```auto
> logical function iam()
> call execute_command_line("export A=3")
> end function iam
> 
> ```

Is it standard-conforming for a function not to assign any return value? I’d guess it isn’t.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [January 11, 2024, 10:08am UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/10 "2024-01-11T10:08:38Z")

</div>

Fortran 2023 standard, page 340:

> if the function result is not a pointer, its value shall be defined by the function.

ifx complains:

```bash
$ ifx test.f90
test.f90(16): warning #6178: The return value of this FUNCTION has not been defined. [IAM]
  logical function iam()
-------------------^

```

GFortran does not complain.

Fixed code:

```fortran
  logical function iam()
    call execute_command_line("export A=3")
    iam = .true.
  end function iam

```

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [January 11, 2024, 11:48am UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/11 "2024-01-11T11:48:40Z")

</div>

> [@RonShepard](#):
>
> Of course, the code is not doing recursive i/o, so this appears to be some kind of compiler or library bug

This is exactly where the issue lies. With gfortran you cannot call a function involving I/O from a print statement. This also hangs:

```auto
program test
  implicit none
  print "(A,L2)", 'I am john',iam('john')
contains
  logical function iam( name)
    character(*),intent(in)::name
    print *, 'hello world!'
    iam = .false.
  end function iam
end program test

```

probably the bash script still involves accessing `stdout`

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [January 11, 2024, 12:12pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/12 "2024-01-11T12:12:17Z")

</div>

> [@FedericoPerini](#):
>
> With gfortran you cannot call a function involving I/O from a print statement

Yes, it seems stdout is locked by the first `print`. And the second `print` is waiting (forever) for stdout to be unlocked.

I remember we had already discuss that somewhere in the Discourse.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [January 11, 2024, 2:06pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/13 "2024-01-11T14:06:37Z")

</div>

> [@vmagnin](#):
>
> Yes, it seems stdout is locked by the first `print`. And the second `print` is waiting (forever) for stdout to be unlocked.

This is true when the `iam()` function has itself a print/write statement. But the original code had no print/write statements in the function.

> [@FedericoPerini](#):
>
> probably the bash script still involves accessing `stdout`

While this is true in some cases, the code still hangs when the execute\_command\_line() operation does not write to stdout. Furthermore, on unix/posix operating systems, the execute\_command\_line() operation is performed in a separate process. One normally expects separate processes to have their own enviornments, their own file handles, etc., so one would expect this to work even if it does write to stdout (possibly with the results interlaced together in some arbitrary way).

This does work with other compilers, so it is not an OS feature, it appears to be specific to the gfortran compiler and/or its i/o libraries. And it fails on both linux and MacOS in a similar way, both unix/posix systems, but very different kernels.

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [January 11, 2024, 2:23pm UTC](https://fortran-lang.discourse.group/t/execute-command-line-problem/7151/14 "2024-01-11T14:23:01Z")

</div>

> [@RonShepard](#):
>
> the code still hangs when the execute\_command\_line() operation does not write to stdout

This is what I see from `lldb` when I ctrl+c it:

```shell
  * frame #0: 0x0000000182630524 libsystem_kernel.dylib`__psynch_mutexwait + 8
    frame #1: 0x000000018266b168 libsystem_pthread.dylib`_pthread_mutex_firstfit_lock_wait + 84
    frame #2: 0x0000000182668af8 libsystem_pthread.dylib`_pthread_mutex_firstfit_lock_slow + 248
    frame #3: 0x0000000100712acc libgfortran.5.dylib`_gfortrani_flush_all_units + 104
    frame #4: 0x00000001007252b8 libgfortran.5.dylib`execute_command_line + 56
    frame #5: 0x0000000100725514 libgfortran.5.dylib`_gfortran_execute_command_line_i4 + 132
    frame #6: 0x0000000100003c6c a.out`iam.0 at ffff.f90:14:49
    frame #7: 0x0000000100003d1c a.out`MAIN__ at ffff.f90:4:41
    frame #8: 0x0000000100003e04 a.out`main at ffff.f90:5:41
    frame #9: 0x00000001822ed0e0 dyld`start + 2360

```
