Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How delete file from fortran code?

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.

like image 762
axion Avatar asked Sep 07 '13 01:09

axion


1 Answers

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')
like image 135
Alexander Vogt Avatar answered Sep 21 '22 21:09

Alexander Vogt