- 论坛徽章:
- 0
|
shell十三问:
http://bbs.chinaunix.net/viewthread.php?tid=218853&extra=&page=5
测试环境:
OS:debian
shell:bash shell
一:var=${parameter:=word}
如果parameter是unset或者null,parameter就会被赋值为经过expansion处理后的word,var值为word。
如果不为not null,则就等于parameter.
- showvar(){
- var=${str:=expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
- 结果为:
- $var is expr #扩展
- $str is expr #扩展
- $var is expr #扩展
- $str is expr #扩展
- $var is 123456
- $str is 123456
复制代码
var=${parameter=word}
如果parameter是unset,则parameter值赋值为word,var值为word.
如果parameter是null,则parameter赋值为null,var值为null.
如果不为not null,则就等于parameter.
- showvar(){
- var=${str=expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
结果为:
- $var is expr
- $str is expr
- $var is
- $str is
- $var is 123456
- $str is 123456
复制代码
二:var=${parameter:-word}
如果parameter是unset或者null,扩展的结果就是经过expansion处理后的word,parameter值不变.
如果不为not null,则就等于parameter.
- showvar(){
- var=${str:-expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
结果为:
- $var is expr
- $str is
- $var is expr
- $str is
- $var is 123456
- $str is 123456
复制代码
var=${parameter-word}
如果parameter是unset,var值为word,parameter值为null.
如果parameter是null,var和parameter值为null.
如果not null,则就等于parameter.
- showvar(){
- var=${str-expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
结果为:
- $var is expr
- $str is
- $var is
- $str is
- $var is 123456
- $str is 123456
复制代码
三:var=${parameter word} #我这里测试出来是错误,大家测试结果如何
- showvar(){
- var=${str:?expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
- 结果为:
- ./showvar.sh: str: expr
复制代码
var=${parameter?word} #我这里测试出来是错误,大家测试结果如何
- showvar(){
- var=${str?expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
- 结果为:
- ./showvar.sh: str: expr
复制代码
四:var=${parameter:+word}
如果parameter是unset或者null,parameter和var结果为空。
如果parameter不为null的话,parameter不变,var的值为word.
- showvar(){
- var=${str:+expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
结果为:
- $var is #null
- $str is
- $var is #null
- $str is
- $var is expr #var=${parameter:+word}的值为word
- $str is 123456
复制代码
var=${parameter+word}如果parameter是unset,parameter和var结果为空.
如果parameter是null,var结果为word,parameter结果为null.
如果parameter不为空的话,parameter值为value,var的值为word.
- showvar(){
- var=${str+expr}
- echo \$var is $var
- echo \$str is $str
- }
- unset str
- showvar
- str= #the value is null
- showvar
- str=123456
- showvar
复制代码
结果为:
- $var is
- $str is
- $var is expr
- $str is
- $var is expr #var=${parameter:+word}的值为word
- $str is 123456
复制代码
有什么不对的地方,请大家帮忙指出。谢谢
[ 本帖最后由 ballball2 于 2007-1-29 15:41 编辑 ] |
|