标 题: 读取使用命令行参数——GetArg
发信站: 瀚海星云 (2008年09月27日22:48:37 星期六), 站内信件 WWWPOST
做计算的同学也许有过这样的经历,写好一个程序,当我们改变参数时,往往会在
源程序里面直接改,然后重新编译。
也许某一天,我们发现这个方法太笨了,于是我们加入了输入参数的read语句,这
样一次编译生成的程序就可以使用不同的参数运行了。
有没有更方便的方法呢?比如直接以命令行参数的方式读入一个数以供使用呢?
getArg这个subroutine可以读取命令行参数,以供程序使用。下面是一个例子:
program test
character(12) :: cbuff
integer :: ibuff
real :: rbuff
call getArg(0, cbuff) ! 第0个参数是最终生成运行的程序的名字
print *, cbuff
call getArg(1, cbuff)
print *, cbuff
read(cbuff, *) ibuff ! 可以把参数转变成integer型
call getArg(2, cbuff)
print *, cbuff
read(cbuff, *) rbuff ! real型也没有问题
print *, rbuff*ibuff
end program test
运行结果如下:
format@server:~/test/getarg> ./a.out 12 3.14
./a.out
12
3.14
37.68000
嗯,看起来不错。
在linux环境下,我们还可以结合使用bash脚本来“并行”运行程序。比如在一个双4核的机子
上,利用如下的bash脚本,可以同时计算8个不同参数的程序。
#!/bin/sh
for ((i=1;i<=8;++i))
do
nohup ./a.out $i > 0$i.log &
done
把上面的内容保存下来(比如run.sh),加入可执行属性后运行。就可以在ps命令里面
看到8个正在运行的程序。原来的屏幕输出(即标准输出)则可以在文件
01.log,02.log,...,08.log里面看到。
由于使用了nohup命令,使得我们logout以后,程序仍然继续运行。
参考资料:
1. 关于nohup以及后台运行程序的内容,参见:
http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/?S_TACT=105AGX52&S_CMP=NL&ca=dnl-cn-06042008 2. GetArg的详细资料如下(摘自intel fortran compile的文档):
GETARG
Intrinsic Subroutine: Returns the specified command-line argument (where the
command itself is argument number zero). This subroutine cannot be passed as
an actual argument.
Syntax
CALL GETARG (n, buffer [, status])
n
(Input) Must be a scalar of type integer. This value is the position of the
command-line argument to retrieve. The command itself is argument number 0.
buffer
(Output) Must be a scalar of type default character. Its value is the
returned command-line argument.
status
(Output; optional) Must be a scalar of type integer. If specified, its value
is the returned completion status.
If there were no errors, status returns the number of characters in the
retrieved command-line argument before truncation or blank-padding. (That is,
status is the original number of characters in the command-line argument.)
Errors return a value of -1. Errors include specifying an argument position
less than 0 or greater than the value returned by IARGC.
GETARG returns the nth command-line argument. If n is zero, the name of the
executing program file is returned.
GETARG returns command-line arguments as they were entered. There is no case
conversion.
If the command-line argument is shorter than buffer, GETARG pads buffer on
the right with blanks. If the argument is longer than buffer, GETARG
truncates the argument on the right. If there is an error, GETARG fills
buffer with blanks.
See Also
NARGS, IARGC, COMMAND_ARGUMENT_COUNT, GET_COMMAND, GET_COMMAND_ARGUMENT
Example
Assume a command-line invocation of PROG1 -g -c -a, and that buffer is at
least five characters long. The following calls to GETARG return the
corresponding arguments in buffer and status:
Statement String returned Length returned
in buffer in status
CALL GETARG (0, buffer, status) PROG1 5
CALL GETARG (1, buffer) -g undefined
CALL GETARG (2, buffer, status) -c 2
CALL GETARG (3, buffer) -a undefined
CALL GETARG (4, buffer, status) all blanks -1