How delete file from fortran code?

后端 未结 5 1444
有刺的猬
有刺的猬 2021-02-14 00:21

I need to delete a file from a Fortran code. I am on ubuntu 12.04, x86_64. I don\'t understand why the procedure described below does not work. Please

相关标签:
5条回答
  • 2021-02-14 00:57

    Please try

    call system(trim(cmd))
    
    0 讨论(0)
  • 2021-02-14 01:07

    So in your shell script, you don't specify a program in the first line. Try adding:

    #!/bin/bash
    

    as the very first line in del.sh. When bash starts it without that, it may be running the script with /bin/sh, not /bin/bash as you'd expect. (I'm not able to confirm right now, but I know I've had trouble in the past if I use bash-specific code but forget to put the shebang at the top.) When bash starts it with that line, it will see that it needs to be executed with bash instead. Since your code appears to show that calling it as a bash argument directly works, I'd say this should fix your problem. All the best.

    0 讨论(0)
  • 2021-02-14 01:11

    Why not let Fortran do the work for you? This code is portable (compare cup's comment):

    open(unit=1234, iostat=stat, file=file, status='old')
    if (stat == 0) close(1234, status='delete')
    
    0 讨论(0)
  • 2021-02-14 01:11

    source is a shell builtin that loads another script in the current process (as opposed to running it in a subprocess).

    You have no need of source when invoking a script from Fortran, as you found out. Both del.sh and bash del.sh worked, and either of those represent the way you should be doing it.

    0 讨论(0)
  • 2021-02-14 01:17

    The system call invokes the shell to execute your command, which shell depends on the system/environment. Since you get sh: 1: source: not found, the shell which is invoked doesn't understand the source command, which is a bash builtin. On Ubuntu, by default /bin/sh is linked to /bin/dash, not /bin/bash, and dash does not understand source. Instead, using the . (portable) builtin instead of source:

    100   format('. del.sh ',a30)
    

    should work, if del.sh is in your $PATH.

    This is why I would think that these should all work:

    100 format('sh del.sh ',a30)
    100 format('bash del.sh ',a30)
    100 format('del.sh ',a30)
    

    But you have it differently? In that case, beats me :)

    0 讨论(0)
提交回复
热议问题