Linux Shell參数扩展(Parameter Expansion)
本文主要參考:http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
其它资料:http://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html
參数扩展的表示形式为:${expression}。
expression包含各种字符直到匹配上'}'。当出现下面情况时候'}'不会被检查来匹配:
1)在转义字符\之后,如\{;
2)在引號里面,如‘}’;
3) 在算术表达式,命令替换或者变量扩展里面的。如${value}
最简单的參数扩展形式如:${parameter}
使用例如以下模式能够改动參数扩展:
${parameter:-[word]}Use Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise,
the value of parameter shall be substituted.当${parameter}值为空或者没有设定的时候,用[word]值来替换。否则它就是该表达式的值。
[hdfs@cdh51kdc ~]$ bb=3
[hdfs@cdh51kdc ~]$ echo ${aa} [hdfs@cdh51kdc ~]$ echo ${bb}
3
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}
3
[hdfs@cdh51kdc ~]$ aa=2
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}
2
${parameter:=[word]}Assign Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be assigned to parameter.
In all cases, the final value of parameter shall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.当${parameter}值为空或者没有设定的时候。用[word]值来给${parameter}赋值并替换最后的表达式。否则它就是该表达式的值。
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}
2
[hdfs@cdh51kdc ~]$ echo ${aa:=${bb}}
2
[hdfs@cdh51kdc ~]$ echo ${cc} [hdfs@cdh51kdc ~]$ echo ${cc:=${bb}}
3
[hdfs@cdh51kdc ~]$ echo ${cc}
3
${parameter:?
[word]}Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted)
shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of parameter shall be substituted. An interactive shell need not exit.当${parameter}值为空或者没有设定的时候,用[word]值作为标准错误输出提示并退出shell且返回非0状态。
否则它就是该表达式的值。
[hdfs@cdh51kdc ~]$ echo ${cc:?"Value not set"}
3
[hdfs@cdh51kdc ~]$ echo ${dd:? "Value not set"}
-bash: dd: Value not set
${parameter:+[word]}Use Alternative Value. If parameter is unset or null, null shall be substituted; otherwise, the expansion of word (or an empty string if word is
omitted) shall be substituted.当${parameter}值为空或者没有设定的时候,表达式返回null。否则用[word]替换表达式的值。
[hdfs@cdh51kdc ~]$ echo ${cc:+"Value not set"}
Value not set
[hdfs@cdh51kdc ~]$ echo ${dd:+"Value not set"}
${#parameter}
String Length. The length in characters of the value of parameter shall be substituted. If parameter is '*' or '@', the result of
the expansion is unspecified. If parameter is unset and set -u is in
effect, the expansion shall fail.表达式返回${parameter}值中字符的个数。
[hdfs@cdh51kdc ~]$ echo ${#cc}
1
[hdfs@cdh51kdc ~]$ echo ${#dd}
0
The following four varieties of parameter expansion provide for substring processing. In each case, pattern matching notation (see Pattern
Matching Notation), rather than regular expression notation, shall be used to evaluate the patterns. If parameter is '#', '*', or '@', the result of the expansion is unspecified. If parameter is unset and set -u is
in effect, the expansion shall fail. Enclosing the full parameter expansion string in double-quotes shall not cause the following four varieties of pattern characters to be quoted, whereas quoting characters within the braces shall have this effect. In each
variety, if word is omitted, the empty pattern shall be used.
${parameter%[word]}
If present,word shall not begin with an unquoted '%'.
用[word]产生的模式来匹配${parameter}的后缀并去除掉最小匹配部分
[hdfs@cdh51kdc ~]$ echo ${cc}
Value not set
[hdfs@cdh51kdc ~]$ echo ${cc%"et"}
Value not s
${parameter%%[word]}Remove Largest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest
portion of the suffix matched by the pattern deleted.用[word]产生的模式来匹配${parameter}的后缀并去除掉最大匹配部分
[hdfs@cdh51kdc ~]$ echo ${cc%%t*}
Value no
[hdfs@cdh51kdc ~]$ echo ${cc%t*}
Value not se
${parameter#[word]}Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest
portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted '#'.${parameter##[word]}Remove Largest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest
portion of the prefix matched by the pattern deleted.最后两个各自是去除掉前缀的[word]最小匹配和最大匹配
[hdfs@cdh51kdc ~]$ echo ${cc#*t}
set
[hdfs@cdh51kdc ~]$ echo ${cc##*t} [hdfs@cdh51kdc ~]$ echo ${cc#V}
alue not set
Linux Shell參数扩展(Parameter Expansion)的更多相关文章
- ubuntu nginx安装及相关linux性能參数优化
一.安装 下载源代码,解压:tar -xzvf nginx-1.4.7.tar.gz ./configure make && make install 改动默认nginx的监听port ...
- Linux shell猜数游戏
题目:猜随机数随机1-100中的一个数字,要求用户猜数字,猜中则退出脚本并告知用户猜测次 数和随机数字,否则要求用户继续猜,并告知当前猜的数字和随机数的关系. #!/bin/bash #猜数游戏 Ra ...
- AFNetworking 3.0携带參数上传文件Demo
一.服务端代码: 服务端是java用国产nutz搞的,实际mvc框架都大同小异.就是提交文件的同一时候还带了个表单參数 @AdaptBy(type=UploadAdaptor.class, args= ...
- linux下形如{command,parameter,parameter}执行命令 / bash花括号扩展
背景 在复现vulhub上的漏洞ActiveMQ Deserialization Vulnerability (CVE-2015-5254)时,发现官方文档给出反弹shell的payload bash ...
- linux kernel的cmdline參数解析原理分析
利用工作之便,今天研究了kernel下cmdline參数解析过程.记录在此.与大家共享.转载请注明出处.谢谢. Kernel 版本:3.4.55 Kernel启动时会解析cmdline,然后依据这些參 ...
- 【原】用PHP搭建基于swoole扩展的socket服务(附PHP扩展的安装步骤及Linux/shell在线手册)
最近公司的一项目中,需要用PHP搭建一个socket服务. 本来PHP是不适合做服务的,因为和第三方合作,需要采用高效而稳定的TCP协议进行数据通信.经过多次尝试,最终选择了开源的PHP扩展:swoo ...
- Linux Shell多进程并发以及并发数控制
1. 基础知识准备 1.1. linux后台进程 Unix是一个多任务系统,允许多用户同时运行多个程序.shell的元字符&提供了在后台运行不需要键盘输入的程序的方法.输入命令后,其后紧跟&a ...
- mysqladmin在SuSE linux系统中--sleep參数使用不准确问题
我们都知道,在MySQL中.能够使用mysqladmin命令的extended-status选项来查看MySQL的执行状态,比方获取我们经常关注的几个值: # mysqladmin -uroot -p ...
- shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟
/********************************************************************* * Author : Samson * Date ...
随机推荐
- CAD设置背景图片(com接口)
把图片作为背景图片可见但是不能编辑操作. 主要用到函数说明: _DMxDrawX::DrawImageToBackground 绘光栅图到背景.详细说明如下: 参数 说明 BSTR sFileName ...
- laravel JWTAuth实现api接口鉴权(基础篇)
官网:https://jwt-auth.readthedocs.io 参考:https://learnku.com/articles/10885/full-use-of-jwt#99529f 1.to ...
- pavenet资源
网络结构:http://www.cnblogs.com/fariver/p/7449563.html 源码:https://github.com/sanghoon/pva-faster-rcnn 训练 ...
- python3中shuffle函数
1. shuffle函数与其他函数不一样的地方 shuffle函数没有返回值!shuffle函数没有返回值!shuffle函数没有返回值!仅仅是实现了对list元素进行随机排序的一种功能 请看下面的坑 ...
- PageOffice NET MVC下使用
1)下载官方demo http://www.zhuozhengsoft.com/dowm/ 2)选择此项下载 3)官方demo暂时还未修改支持42版本以上的谷歌浏览器 所以需要修改以下部分 /home ...
- 跨平台字符编码转换GBK、UTF8
#if (defined _WIN32 || defined _WIN64) # include <windows.h> # include <stdio.h> # inclu ...
- Linux从入门到适应(四):Ubuntu 16.04环境下,安装Nvidia驱动,cuda9.2和 cudnn
在安装深度学习框架之前,cuda和cudnn是必须要提前安装的,现在按照流程而nvidia驱动的版本和cuda版本有这一些对应关系,所以需要按照版本进行安装,现在说一下如何安装: 1 安装nvidia ...
- MYSQL有那些优化?
版权声明:本文为博主转载文章,原博主地址: https://blog.csdn.net/u013087513/article/details/77899412 MySQL优化三大方向 ① 优化MySQ ...
- 你知道你常用的dos和linux命令吗?
功能 Linux MS-DOS 进入到该目录 cd cd 列举文件 ls dir 创建目录 mkdir mkdir 清除屏幕 clear cls 复制文件 cp copy 移动文件 mv move 删 ...
- 用element-ui的走马灯carousel轻松实现自适应全屏banner图
写在前面:网站轮播图建议使用swiper组件,非常方便快捷.vue轮播图插件之vue-awesome-swiper 接手一个项目,轮播图是用element-ui的carousel实现的,看起来效果还不 ...