(4)shell替换
来源:互联网 发布:淘宝刷单被骗案例 编辑:程序博客网 时间:2024/06/06 12:57
1、shell转义字符(都可以使用在echo中):
如果表达式中包含特殊字符,Shell 将会进行替换。
- -e 表示对转义字符进行替换
- 不使用 -e 选项,将会原样输出
- -E 选项禁止转义
- 默认也是不转义的
- -n 选项可以禁止插入换行符
#!/bin/basha=10echo -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/bashDATE=`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 2009Logged in user are 1Uptime is Thu Jul 2 03:59:57 MST 200903:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15将命令date运行结果保存在变量DATE中,然后在echo中输出。
3、变量替换:
变量替换可以根据变量的状态(是否为空、是否定义等)来改变它的值。
可以使用变量替换形式为:
#!/bin/bashecho ${var:-"Variable is not set"}echo "1 - Value of var is ${var}"#变量var没有定义,输出Variable is not set,且不改变var值,var为空#1 - Value of var isecho ${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 setunset varecho ${var:+"This is default value"}echo "3 - Value of var is $var"#var已被unset,无输出,var为空#3 - Value of var isvar="Prefix"echo ${var:+"This is default value"}echo "4 - Value of var is $var"#var已被定义,因此输出This is default value#4 - Value of var is Prefixecho ${var:?"Print this message"}echo "5 - Value of var is ${var}"#var已经定义,因此输出var,Prefix#5 - Value of var is Prefix运行结果:Variable is not set1 - Value of var isVariable is not set2 - Value of var is Variable is not set3 - Value of var isThis is default value4 - Value of var is PrefixPrefix5 - Value of var is Prefix
0 0
- (4)shell替换
- Shell替换
- Shell替换
- Shell替换
- shell替换
- Shell学习--(3)替换,字符串,数组
- Shell脚本查找与替换(二)
- Shell 学习8 - Shell 替换
- SHELL 文本替换
- SHELL的字符替换
- Bash Shell命令行替换
- shell 文本替换命令
- shell 变量替换
- shell 替换文件内容
- shell变量替换
- shell中替换
- shell中的命令替换
- shell 批量查找替换
- Error:Execution failed for task ':myapp:dexDebug'. com.android.ide.common.process.ProcessException:
- 网页设计中,相对路径与绝对路径的问题
- 用栈判断回文
- Fuel Openstack 7.0在Virtualbox中部署
- %运算符 1
- (4)shell替换
- MySQL教程-原理篇-B+Tree索引
- 14 screenShotLikeQQ
- 【大话设计模式】六大原则
- 搭建一个struts2+spring+hibernate框架
- 导入AndroidStudio的项目卡死,如何快速导入AndroidStudio的项目
- 【三层学习】——初识
- ubuntu下apache2 安装 配置 卸载 CGI设置 SSL设置
- BZOJ 1013: [JSOI2008]球形空间产生器sphere