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 help me to clarify the situation (actually, on some systems, it works, but not on mine).
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.
Step 1. make simple script del.sh and put it into ~/bin
$ cat del.sh
[ $# -ge 1 ] && rm -f $1
$ chmod u+x del.sh; mv del.sh ~/bin
Step 2. Fortran code, 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:
$ 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.
From the Fortran code:
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 bash
installed, but no csh
. Thank you.
The
system
call invokes the shell to execute your command, which shell depends on the system/environment. Since you getsh: 1: source: not found
, the shell which is invoked doesn't understand thesource
command, which is abash
builtin. On Ubuntu, by default/bin/sh
is linked to/bin/dash
, not/bin/bash
, anddash
does not understandsource
. Instead, using the.
(portable) builtin instead ofsource
:should work, if
del.sh
is in your$PATH
.This is why I would think that these should all work:
But you have it differently? In that case, beats me :)
So in your shell script, you don't specify a program in the first line. Try adding:
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.
Please try
Why not let Fortran do the work for you? This code is portable (compare cup's comment):
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. Bothdel.sh
andbash del.sh
worked, and either of those represent the way you should be doing it.