(4)shell替换
2016-02-02 20:53
465 查看
1、shell转义字符(都可以使用在echo中):
如果表达式中包含特殊字符,Shell 将会进行替换。
-e 表示对转义字符进行替换
不使用 -e 选项,将会原样输出
-E 选项禁止转义
默认也是不转义的
-n 选项可以禁止插入换行符
#!/bin/bash a=10 echo -e "Value of a is $a \n" 输出结果:Value of a is 10 如果不使用-e,则会输出:Value of a is 10\n,也就是转移字符\n不会被替换。
2、命令替换:
命令替换是指Shell可以先执行命令,将输出结果暂时保存,在适当的地方输出。使用语法: `command` 注意:是反引号,不是单引号,这个键位于Esc键下方。
举例: #!/bin/bash DATE=`date` echo "Date is $DATE" USERS=`who | wc -l` echo "Logged in user are $USERS" UP=`date ; uptime` echo "Uptime is $UP" 运行结果为: Date is Thu Jul 2 03:59:57 MST 2009 Logged in user are 1 Uptime is Thu Jul 2 03:59:57 MST 2009 03:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15 将命令date运行结果保存在变量DATE中,然后在echo中输出。
3、变量替换:
变量替换可以根据变量的状态(是否为空、是否定义等)来改变它的值。可以使用变量替换形式为:
#!/bin/bash echo ${var:-"Variable is not set"} echo "1 - Value of var is ${var}" #变量var没有定义,输出Variable is not set,且不改变var值,var为空 #1 - Value of var is echo ${var:="Variable is not set"} echo "2 - Value of var is ${var}" #变量var没有定义,输出Variable is not set,且改变var值为Variable is not set #2 - Value of var is Variable is not set unset var echo ${var:+"This is default value"} echo "3 - Value of var is $var" #var已被unset,无输出,var为空 #3 - Value of var is var="Prefix" echo ${var:+"This is default value"} echo "4 - Value of var is $var" #var已被定义,因此输出This is default value #4 - Value of var is Prefix echo ${var:?"Print this message"} echo "5 - Value of var is ${var}" #var已经定义,因此输出var,Prefix #5 - Value of var is Prefix 运行结果: Variable is not set 1 - Value of var is Variable is not set 2 - Value of var is Variable is not set 3 - Value of var is This is default value 4 - Value of var is Prefix Prefix 5 - Value of var is Prefix
相关文章推荐
- (3)shell特殊变量
- ssh远程执行shell实例
- (2)第一个shell脚本和shell变量
- shell之路【第一篇】shell简介与入门
- Linux中profile、bashrc、bash_profile之间的区别和联系
- grep 仅显示匹配部分
- linux shell 编程7 tomcat的启动停止
- linux shell 编程6 函数获取参数
- 使用Powershell配置Hyper-V Server 资源计量 推荐
- shell实现多线程笔记
- shell命令例子
- Shell脚本--结构化命令
- shell 变量
- shell字符串替换
- Linux Shell常用技巧(十二) Shell编程
- Linux Shell常用技巧(五) awk编程
- xshell中目录颜色的修改方法
- xshell远程连接自动断开的问题解决办法
- Python 反弹shell后门用51CTO Blog绑定
- find 必杀技巧