簡単な例:outputという名前のディレクトリを作成する
call system( & & 'if [ ! -d output ]; then & & (echo "Create output directory"; mkdir -p output); & & fi')サブディレクトリまで指定できるバージョン
program makedir! Description:!! Author: am!! Host: aofd30! Directory: /work2/am/teaching/21.Fortran/03.Makedir!! Revision history:! 2011-05-10 11:42! Initial Version! use implicit none character(len=200) :: outdir character(len=1000) :: comm ! unix command integer::is,ie,idxodr integer lnblnk! write(*,'(a)')'Program makedir starts.'! write(*,*)''! outdir="output/temp/" idxodr=lnblnk(outdir)! is=1; ie=is+len('if [ ! -d ') write(comm(is:ie),'(A)') 'if [ ! -d ' is=ie+1; ie=is+idxodr write(comm(is:ie),'(A)') outdir(1:idxodr) is=ie+1 ie=is+len(' ]; then (echo "Create directory,') write(comm(is:ie),'(A)')&&' ]; then (echo "Create directory,' is=ie+1; ie=is+idxodr-1 write(comm(is:ie),'(A)') outdir(1:idxodr) is=ie+1 ie=is+len('"; mkdir -p ')-1 write(comm(is:ie),'(A)')&& '"; mkdir -p ' is=ie+1; ie=is+idxodr-1 write(comm(is:ie),'(A)') outdir(1:idxodr) is=ie+1; ie=is+4 write(comm(is:ie),'(A)') '); fi'! print '(A)',comm(1:ie) call system(comm(1:ie))! write(*,'(a)')'Done program makedir.'! write(*,*)end program makedir使用例
コンパイル例
$ make
make: Warning: File `makefile' has modification time 2.5e+02 s in the future
if [ ! -d ../obj ]; then \
mkdir -p ../obj ; \
fi
ifort -c -CB -traceback -fpe0 -module ../obj -c -o ../obj/makedir.o makedir.f90
ifort -o makedir ../obj/makedir.o -module ../obj
実行例
[Tue May 10 11:56:36 JST 2011]
[am@aofd30 processor=x86_64]
[~/teaching/21.Fortran/03.Makedir]
$ ls
makedir* makedir.f90 makefile
[Tue May 10 11:56:44 JST 2011]
[am@aofd30 processor=x86_64]
[~/teaching/21.Fortran/03.Makedir]
$ makedir
Create directory, output/temp/
[Tue May 10 11:56:51 JST 2011]
[am@aofd30 processor=x86_64]
[~/teaching/21.Fortran/03.Makedir]
$ ls
makedir* makedir.f90 makefile output/
[Tue May 10 11:56:46 JST 2011]
[am@aofd30 processor=x86_64]
[~/teaching/21.Fortran/03.Makedir]
$ ls output
temp/
注意
サブルーチンsystemは, ifort, g77, gfortran, g95などでは実装されている。
大型計算機には無いことが多いようだが、同様の動作をする名前の異なるサブルーチンが存在することがある(マニュアルをチェックすること)
参考
Fortran 77用
call system( &'if [ ! -d output ]; then & (echo "Create output directory"; mkdir -p output); & fi')