寻觅生命中的那一片浅草......

关于Shell的补零,用printf来格式化数字列。

解答论坛一个朋友关于获取01、02…10,而非1、2….10。
因为需要用flashget下载这样一些列的文件。自己了解这个应用,但是以前也没有处理过。还是有需要的。经过g后,测试得到。

[root@kook tmp]# cat for.sh
for ((a=1; a<=10 ; a++))
do
printf “%02d\n” $a
done
[root@kook tmp]# ./for.sh
01
02
03
04
05
06
07
08
09
10

这么写,也可以。

[root@kook tmp]# cat for.sh
for ((a=1; a<=10 ; a++))
do
printf “%.2d\n” $a
done

继续测试。

[root@kook tmp]# printf “%04d\n” -3
-003
[root@kook tmp]# printf “%.4d\n” -3
-0003

转载自:http://cnc.52zhe.cn/read.php/165.htm

尚无评论

发表评论

2024年四月
« 5月    
1234567
891011121314
15161718192021
22232425262728
2930