bash 如何从fortran代码中删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18668832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:29:30  来源:igfitidea点击:

How delete file from fortran code?

linuxbashfileshellfortran

提问by axion

I need to delete a file from a Fortrancode. I am on ubuntu 12.04, x86_64. I don't understand why the procedure described below does not work. Please help me to clarify the situation (actually, on some systems, it works, but not on mine).

我需要从Fortran代码中删除一个文件。我在ubuntu 12.04, x86_64。我不明白为什么下面描述的过程不起作用。请帮我澄清一下情况(实际上,在某些系统上,它有效,但在我的系统上无效)。

There is another way: I can call directly unix command rm -f file, but I'd like to know what is wrong with my method. Thank you.

还有另一种方法:我可以直接调用 unix command rm -f file,但我想知道我的方法有什么问题。谢谢你。

Step 1. make simple script del.sh and put it into ~/bin

步骤 1. 制作简单脚本 del.sh 并将其放入 ~/bin

$ cat del.sh
[ $# -ge 1 ] && rm -f 
$ chmod u+x del.sh; mv del.sh ~/bin 

Step 2. Fortran code, del.for:

步骤 2. Fortran 代码,del.for:

character*100 cmd
character*30 file
call getarg(1,file)
write(cmd,100) file
100   format('source del.sh ',a30)
call system(cmd)
end

Step 3. Compile and run:

步骤 3. 编译并运行:

$ ifort -o del del.for
$ ./del file

Results:

结果:

sh: 1: source: not found

What is wrong? The simple 'source del.sh file' works, but not from Fortran code... that is confusing.

怎么了?简单的“源 del.sh 文件”有效,但不能来自 Fortran 代码……这令人困惑。

From the Fortran code:

从 Fortran 代码:

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

work perfectly, but

工作完美,但

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

does not work. I have bashinstalled, but no csh. Thank you.

不起作用。我已经bash安装了,但没有csh。谢谢你。

回答by Alexander Vogt

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

为什么不让 Fortran 为您完成这项工作?这段代码是可移植的(比较cup的评论):

open(unit=1234, iostat=stat, file=file, status='old')
if (stat == 0) close(1234, status='delete')

回答by steabert

The systemcall 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 sourcecommand, which is a bashbuiltin. On Ubuntu, by default /bin/shis linked to /bin/dash, not /bin/bash, and dashdoes not understand source. Instead, using the .(portable) builtin instead of source:

system调用调用 shell 来执行您的命令,哪个 shell 取决于系统/环境。因为你得到sh: 1: source: not found了,被调用的shell 不理解source命令,它是一个bash内置命令。在 Ubuntu 上,默认情况下/bin/sh链接到/bin/dash,而不是/bin/bash,并且dash不理解source。相反,使用.(便携式)内置而不是source

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

should work, if del.shis in your $PATH.

应该工作,如果del.sh在你的$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 :)

但你有不同的看法吗?在那种情况下,打败我:)

回答by Jim Garrison

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

source是一个 shell 内置程序,它在当前进程中加载​​另一个脚本(而不是在子进程中运行它)。

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

source正如您发现的那样,从 Fortran 调用脚本时不需要。无论 del.shbash del.sh工作,并任那些代表你应该做的方式。

回答by HaiQuanHO

Please try

请尝试

call system(trim(cmd))

回答by Edwin

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

所以在你的 shell 脚本中,你没有在第一行指定一个程序。尝试添加:

#!/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.

作为 del.sh 中的第一行。当 bash 在没有它的情况下启动它时,它可能正在使用 /bin/sh 运行脚本,而不是像您期望的那样使用 /bin/bash。(我现在无法确认,但我知道如果我使用特定于 bash 的代码但忘记将 shebang 放在顶部,我过去会遇到麻烦。)当 bash 以该行开头时,它会看到它需要用 bash 来执行。由于您的代码似乎表明将其作为 bash 参数直接调用是有效的,因此我认为这应该可以解决您的问题。祝一切顺利。