Repeat printf arguments

萝らか妹 提交于 2019-12-12 00:34:53

问题


I've found some related posts, but nothing seems to work.

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

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`

Any suggestions would be greatly appreciated!


回答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


来源:https://stackoverflow.com/questions/18832102/repeat-printf-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!