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 ...
随机推荐
- 所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你!
所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你! 血的代价啊~
- vue工程化引入组件模板
vue脚手架搭建好项目后,组件间的引用通过components import bannerComponent from './banner' export default { data(){ retu ...
- VINS-Mono论文笔记(未完)
这是整篇论文的架构,下面针对每一部分进行自己的详细理解.(数学公式的问题没在博客里面解决,都是论文中的截图,尽可能美观==) 一.测量预处理部分(MEASUREMENT PREPROCESSING) ...
- li标签和checkbox绑定
参考原文:https://www.cnblogs.com/youxin/p/3885496.html 我们经常需要li或span包含一个checkbox,不管点击checkbox或li都会触发相应的事 ...
- PyTorch学习笔记——softmax和log_softmax的区别、CrossEntropyLoss() 与 NLLLoss() 的区别、log似然代价函数
1.softmax 函数 Softmax(x) 也是一个 non-linearity, 但它的特殊之处在于它通常是网络中一次操作. 这是因为它接受了一个实数向量并返回一个概率分布.其定义如下. 定义 ...
- vuex如何管理需要即时更新的全局变量
自己在使用vue练习开发的时候遇到全局变量无法即时更新的问题,查了资料之后得出结论使用vuex能够快速解决该问题,但是看了好多人讲解vuex的教程自己跟着去做都没解决自己想要的,最后找到一个比较容易理 ...
- 22Spring基于配置文件的方式配置AOP
直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...
- Tomcat处理HTTP请求原理
一.Tomcat是什么? Tomcat是一个Web应用服务器,同时也是一个Servlet/JSP容器.Tomcat作为Servlet容器,负责处理客户端请求,把请求传送给Servlet,并将Servl ...
- 《ajax学习》之ajax+JavaScript事件验证用户名是否可注册
当用户注册时,服务器数据库需要对用户输入的用户信息(以用户名为例子)进行验证,在不刷新页面的情况下又需要页面和服务器进行数据请求,最好的方法是用ajax异步请求. 一.实现思路: 1.用户输入信息 2 ...
- 59. Spring Boot Validator校验【从零开始学Spring Boot】
大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...