shell 脚本之printf
- 上篇转载了echo的用法,那么我们本篇就讲一下shell 脚本的格式化输出。shell 的printf基本上继承了 c语言的printf 函数的格式,你只要会c语言的printf 函数,那么shell 脚本的printf 函数对于你来说也没有什么难度。
参考了这篇博客:http://blog.csdn.net/shanyongxu/article/details/46744055
先上代码:
#! /bin/bash
printf "%-50s %-20s \n" first second #第一个字符串50个字符左对齐,第二个20个字符左对齐,\n 代表换行
printf "%50s %20s \n" first second #默认右对齐
printf "%4d %4d \n" 1234 12345 #输出整型
printf "%s %s %s \n" a b c d e f g h i j #多个参数,每行输出三个字符
printf "%s %s hello \n" first second #hello为在参数后输出的字符串
printf "%04d %10.2f \n" 123 9 #不足部位,4 位整数不足补0,保留两位小数
最后看一下结果:
是不是很简单,那就自己动手做一下吧。