LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 895|回复: 6

变量处理的问题

[复制链接]
发表于 2004-7-16 09:44:40 | 显示全部楼层 |阅读模式
关于 ${parameterffset:length} 的用法,man bash中解释如下:
              Substring  Expansion.   
              ...
              If  offset  evaluates  to  a number less than zero, the value is
              used as an offset from the end of the value  of  parameter.
              ...
我的问题如下:
> echo $mypara
abcdef
> echo ${mypara:3:2}
de
> echo ${mypara:-3:2}
abcdef                              #为什么会这样? 我以为是 de
 楼主| 发表于 2004-7-16 09:45:52 | 显示全部楼层
不好意思,上面那个小脸应该是冒号,不知怎么搞成这样
发表于 2004-7-16 10:18:34 | 显示全部楼层
禁用表情
发表于 2004-7-16 10:52:29 | 显示全部楼层

回复: 变量处理的问题

最初由 doubleelec 发表
关于 ${parameterffset:length} 的用法,man bash中解释如下:
              Substring  Expansion.   
              ...
              If  offset  evaluates  to  a number less than zero, the value is
              used as an offset from the end of the value  of  parameter.
              ...
我的问题如下:
> echo $mypara
abcdef
> echo ${mypara:3:2}
de
> echo ${mypara:-3:2}
abcdef                              #为什么会这样? 我以为是 de

请参阅精华区中:
http://www.linuxsir.cn/bbs/showthread.php?threadid=92866
 楼主| 发表于 2004-7-16 15:08:35 | 显示全部楼层
这篇贴子中好像没提到 offset 为负的情况
如果能弄清负值的用法,对"从变量右边取一个子串"的操作应该很有帮助
发表于 2004-7-17 01:48:57 | 显示全部楼层
BASH,可以这样取后面的子串:不知道是不是这样意思! :ask[php]
/home/lee#str=0123456
/home/lee#n=1;while((n<=${#str}));do echo ${str((-n))};((n+=1));done
6
56
456
3456
23456
123456
0123456
/home/lee#echo $0
bash
[/php]
 楼主| 发表于 2004-7-19 09:07:19 | 显示全部楼层
噢,多谢,明白了,把我顶楼写的那一句改成
> echo ${mypara((-3)):2}
就对了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表