重复printf参数 [英] Repeat printf arguments
问题描述
我找到了一些相关的帖子,但似乎没有任何作用。
I've found some related posts, but nothing seems to work.
我要为03-12实例重复相同的参数$ i。我确实在尝试使用某些nco运算符-但是printf语句使我很烦。
I want to repeat the same argument $i for the instances 03-12. I'm really trying to use some nco operators - but the printf statement is hanging me up.
#!/bin/csh
set i = 1
while ($i < 2)
`printf O3_BDBP_1979ghg.cam.h0.00{03,04,05,06,07,08,09,10,11,12}-%02d.nc $i`
@ i = $i + 1
end
输出为-因此它得到03的结果,但不是
The output is - so it gets it for 03 but not the rest.
printf: O3_BDBP_1979ghg.cam.h0.0004-%02d.nc: expected a numeric value
我也尝试过此语句(其他帖子)
I've also tried this statement (per other posts)
`printf O3_BDBP_1979ghg.cam.h0.00{03,04,05,06,07,08,09,10,11,12}-%1$02d.nc $i`
任何建议将不胜感激!
推荐答案
花括号为 printf
命令产生多个参数;仅第一个作为格式字符串,其余的作为第一个%1
的参数。换句话说,您正在获取
The braces produce multiple arguments for the printf
command; only the first is treated as a format string, while the rest are treated as arguments for %1
in the first. In other words, you're getting
printf O3_BDBP_1979ghg.cam.h0.0003-%02d.nc O3_BDBP_1979ghg.cam.h0.0004-%02d.nc ... O3_BDBP_1979ghg.cam.h0.0012-%02d.nc $i
作为有效命令行。尝试嵌套循环:
as the effective command line. Try a nested loop instead:
#!/bin/csh
set i = 1
while ($i < 2)
foreach j ( {03,04,05,06,07,08,09,10,11,12} )
printf O3_BDBP_1979ghg.cam.h0.00%02-%02d.nc $j $i
end
@ i = $i + 1
end
这篇关于重复printf参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!