Linux程序设计(Linux shell编程十一)
来源:互联网 发布:淘宝网店经营 编辑:程序博客网 时间:2024/06/11 06:38
各位看官,咱们上一回主要说了Linux shell编程中的脚本输入,不知道大家还记得吗?咱们今天主要
接着脚本输入的内容说脚本的输出。闲话休提,言归正转。
脚本输出:看官们,咱们在这里说的脚本输出,主要指如何把脚本的运行结果显示在终端中。
脚本的输出主要是使用echo和printf命令来实现。接下来咱们说一下这两个命令的用法。
echo命令:常用的输出命令。echo 选项 参数。
选项只有两个e和n,可有可无。参数必须有,表示输出的内容。注意: 它输出内容时默认在输出内容
的后面加上了换行符,选项n可以去掉换行符。选项e表示可以使用转义字符。比如\t表示制表符。
各位看官们,咱们举个例子来说明echo命令的用法。
#! /bin/bashecho "-----------------the starting line of shell-----------------"str="hello"echo "sample of echo: no opention"echo $strecho "sample of echo: opention:n"echo -n $strechoecho "sample of echo: opention:e"echo -e "$str \t $str"echo "-----------------the ending line of shell-----------------"
新建立一个名叫t1.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在
终端中运行该文件后得到以下结果:
-----------------the starting line of shell-----------------
sample of echo: no opention
hello
sample of echo: opention:n
hello
sample of echo: opention:e
hello hello
-----------------the ending line of shell-----------------
从运行的结果中可以看到,选项n没有换行,我们手动加上后在程序结果中才有换行。使用选项e后可以在
输出的内容中使用转义字符。我们的例子中使用的是\t表示播入一个制表符。
printf命令:格式化输出命令。printf 选项 参数。
选项是格式化的串。参数表示要输出的内容。关于格式化串我们通过例子来说明。例如:%s.%d.%是格式化
串的开始,s表示字符串,d表示数字。在它们中间可以加上数字和减号。数字表示输出内容占用的空间。
减号表示内容向左对齐。不加减号时默认为向右对齐。例如:%5s,%-3d.
各位看官们,咱们举个例子来说明printf命令的用法。
#! /bin/bashecho "-----------------the starting line of shell-----------------"a="hello"b="shell"echo "sample of printf: no opention"printf "$a \n$b \n"echo "sample of printf: format string"printf "%9s,%9s" $a $bechoprintf "%9s,%-9s" $a $bechoprintf "%-9s,%9s" $a $bechoprintf "%-9s,%-9s" $a $bechoecho "-----------------the ending line of shell-----------------"
新建立一个名叫t2.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在
终端中运行该文件后得到以下结果:
-----------------the starting line of shell-----------------
sample of printf: no opention
hello
shell
sample of printf: format string
hello, shell
hello,shell
hello , shell
hello ,shell
-----------------the ending line of shell-----------------
通过程序的运行结果,大家可以看到printf输出时没有加换行,我们使用\n表示换行。在输出变量a和b
中的内容时,输出的内容各占用了9个字符宽的位置。4种运行结果表示了不同对齐方式。至于使用哪种对
齐方式,大家可以自己去定义。
echo和printf这两个命令最大的区别是输出的内容是否有换行。另外,echo直接输出内容原来的样子,
而printf可以自定义输出内容的格式。
各位看官,脚本输入和输出的内容,咱们都说完了。欲知后事如何,且听下回分解。
- Linux程序设计(Linux shell编程十一)
- Linux程序设计(Linux shell编程一)
- Linux程序设计(Linux shell编程二)
- Linux程序设计(Linux shell编程三)
- Linux程序设计(Linux shell编程四)
- Linux程序设计(Linux shell编程五)
- Linux程序设计(Linux shell编程六)
- Linux程序设计(Linux shell编程七)
- Linux程序设计(Linux shell编程八)
- Linux程序设计(Linux shell编程九)
- Linux程序设计(Linux shell编程十)
- Linux程序设计(Linux shell编程十二)
- Linux程序设计(Linux shell编程十三)
- Linux程序设计(Linux shell编程十四)
- Linux程序设计(Linux shell编程的例子:倒计时程序)
- Linux程序设计(Linux shell编程的例子:选择菜单)
- Linux程序设计(Linux shell编程的例子:总结)
- linux c/c++ 程序设计(二)shell程序编程
- 给QTreeWidget添加右键菜单功能
- Linux禁用显示“缓冲调整”
- mysql命令语句来去除掉字段中空格字符的方法
- 随笔记录:URL和URI的区别
- 读取通达信日线数据
- Linux程序设计(Linux shell编程十一)
- IOS持续的后台定位
- JAVA设计模式(二十)——备忘录模式
- Linux启动过程概述(X86)
- 实现UML 序列图 -- csdn MarkDown 第六篇 技术篇
- Java synchronized 用法
- 程序员练级攻略(CoolShell)
- GDUT 游戏王(状压DP)
- horizon is easy and complex