比如说,我有一个 C 代码,我编译如下:
$ gcc code.c -o f.out
$ ./f.out inputfile outputfile
然后代码要求输入
$ enter mass:
现在,如果我需要运行此代码 200 次,并且输入文件的名称为:0c.txt、1c.txt、.....、199c.txt 等,并且我想每次使用相同的质量值时间(例如质量= 6)那么我如何为此编写“awk”命令?感谢您的帮助。
最佳答案
您没有指定输出文件名。我假设 0c.out, 1c.out, ...
我还假设 f.out 程序从 stdin 读取质量,而不是更复杂的内容。
#!/usr/bin/gawk -f
BEGIN {
mass = 6
for (i=0; i<200; i++) {
cmd = sprintf("./f.out %dc.txt %dc.out", i, i)
print mass |& cmd
close(cmd, "to")
while ((cmd |& getline out) > 0) {
do something with each line of output from ./f.out
}
close(cmd)
}
}
引用http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html
在 bash 中,你可以这样写:
for i in $(seq 0 199); do
echo 6 | ./f.out ${i}c.txt ${i}c.out
done
关于awk 命令运行 C++ 代码并输入多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26265152/