码迷,mamicode.com
首页 > 其他好文 > 详细

$ ${} ${!}

时间:2016-07-17 09:01:45      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

1、基本参数扩展:

[xiluhua@vm-xiluhua][~]$ echo $temp
100
[xiluhua@vm-xiluhua][~]$ echo $temp1
结果:echo nothing
这时必须使用${}
[xiluhua@vm-xiluhua][~]$ echo ${temp}1
1001

 

2、间接参数扩展:

[xiluhua@vm-xiluhua][~]$ temp=100
[xiluhua@vm-xiluhua][~]$ param=temp
[xiluhua@vm-xiluhua][~]$ echo $param
temp
[xiluhua@vm-xiluhua][~]$ echo ${!param}
100

 

Bash4.0特性

3、大小写修改:

${PARAMETER^}    将参数值的第一个字符改为大写
${PARAMETER^^}    将参数值的所有字符改为大写
${PARAMETER,}    将参数值的第一个字符改为小写
${PARAMETER,,}    将参数值的所有字符改为小写
[xiluhua@vm-xiluhua][~/shell_script]$ touch test1.123 test2.123 test3.123
 
[xiluhua@vm-xiluhua][~/shell_script]$ ll -t *.123
-rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 test1.123
-rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 test2.123
-rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 test3.123  

[xiluhua@vm-xiluhua][~/shell_script]$ cat 2.sh
#=======================================================================================
#/bin/bash
#auth: xiluhua
#date: 20160626
#desc: test
#=======================================================================================
for file in *.123; do
        mv "$file" "${file^^}"
done

 

[xiluhua@vm-xiluhua][~/shell_script]$ 2.sh
 
[xiluhua@vm-xiluhua][~/shell_script]$ ll -t *.123
-rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 TEST1.123
-rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 TEST2.123
-rw-rw-r--. 1 xiluhua xiluhua 0 2016/07/16 21:27:31 TEST3.123
 

 

4、变量名扩展:

[xiluhua@vm-xiluhua][~/shell_script]$ temp1=1
 
[xiluhua@vm-xiluhua][~/shell_script]$ temp2=2
 
[xiluhua@vm-xiluhua][~/shell_script]$ temp3=3

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${!temp*}
temp1 temp2 temp3

或

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${!temp@}
temp1 temp2 temp3

 

5、字符串移除:(举例说明)

[xiluhua@vm-xiluhua][~/shell_script]$ pride="IRON MAN is the pride of the PEOPLE‘S REPUBLIC OF CHINA"
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride#* }  #移除第一个空格前的内容
MAN is the pride of the PEOPLES REPUBLIC OF CHINA
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride##* }  #移除最后一个空格前的内容
CHINA
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride% *}  #移除最后一个空格后的内容
IRON MAN is the pride of the PEOPLES REPUBLIC OF
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride%% *}  #移除最后一个空格前的内容
IRON

最常用的业务场景是提取文件名的一部分:
[xiluhua@vm-xiluhua][~/shell_script]$ filename=test1.123

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename##*.}  #得到文件后缀
123
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename%.*}  #得到文件名
test1

[xiluhua@vm-xiluhua][~/shell_script]$ filename=/home/xiluhua/test1.123
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename%/*}  #得到文件目录
/home/xiluhua
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${filename##*/}  #得到文件名称
test1.123

 

 

 6、字符串搜索与替换

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride}    #原文
IRON MAN is the pride of the PEOPLES REPUBLIC OF CHINA
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride/the/a}  #the替换成a,仅替换第一处
IRON MAN is a pride of the PEOPLES REPUBLIC OF CHINA
您在 /var/spool/mail/xiluhua 中有新邮件
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride//the/a}  #他和替换成a,替换所有
IRON MAN is a pride of a PEOPLES REPUBLIC OF CHINA
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride/the}  #删除第一个the
IRON MAN is pride of the PEOPLES REPUBLIC OF CHINA

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride//the}  #删除所有the
IRON MAN is pride of PEOPLES REPUBLIC OF CHINA

 

7、字符串长度

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${#pride}
55

 

8、子字符串扩展

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:5}  #截取第5个字符后的字符串
MAN is the pride of the PEOPLES REPUBLIC OF CHINA

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:28:55}  #截取从28-55的字符
PEOPLES REPUBLIC OF CHINA  

 

9、指定默认值

[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:-"the pride of the PEOPLE‘S REPUBLIC OF CHINA"}
IRON MAN is the pride of the PEOPLES REPUBLIC OF CHINA
 
[xiluhua@vm-xiluhua][~/shell_script]$ unset pride
 
[xiluhua@vm-xiluhua][~/shell_script]$ echo ${pride:-"the pride of the PEOPLE‘S REPUBLIC OF CHINA"}
the pride of the PEOPLES REPUBLIC OF CHINA

 

$ ${} ${!}

标签:

原文地址:http://www.cnblogs.com/xiluhua/p/5677252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!