------------------------------------------------------------------------------- 想用“.*”来匹配./ 或者 ../ if [ "$1" = “.*” ];then ##given path like ./ or ../ 想用"$i"="*.EXE;*" -o "$i"="*.OBJ;*" -o "$i"="*.MAP;*" -o "$i"="*.LIS;*" -o "$i"="MAKEFILE*" 来匹配类似的:q.EXE;11 hello.OBJ;84 su.LIS;89 这样的文件名 if [ "$i"="*.EXE;*" -o "$i"="*.OBJ;*" -o "$i"="*.MAP;*" -o "$i"="*.LIS;*" -o "$i"="MAK...
by xb_parasite - Shell - 2005-04-21 11:49:55 阅读(3195) 回复(11)
Advanced bash-Scripting Guide: An in-depth exploration of the art of shell scripting 9.2. Manipulating Stringsbash中的字符串处理 bash支持一系列令人吃惊的数字和字符串处理。不幸的是这些工具缺乏统一的焦点/目的, 它们有些是参数替换(parameter substitution)的子集,其他的属于UNIX命令expr的功能。 这种情况导致了命令语法的不一致/统一 和 功能的重复实现,别提有多混乱了! 字符串长度(String Length) ${#string} e...
得到长度
[code]
%x="abcd"
#方法一
%expr length $x
4
# 方法二
%echo ${#x}
4
# 方法三
%expr "$x" : ".*"
4
# expr 的帮助
# STRING : REGEXP anchored pattern match of REGEXP in STRING
[/code]
查找子串
[code]
%expr index $x "b"
2
%expr index $x "a"
1
%expr index $x "b"
2
%expr index $x "c"
3
%expr index $x "d"
4
[/code]
得到子字符串
[code]
# 方法一
# expr
$ test=abc123abc $ test2=${test/123/" "} $ test3=(${test/123/" "}) $ echo $test2 abc abc $ echo $test3 abc 问题1:test3只多加了一对括号,结果就跟test2不同了,为什么它能够把匹配上的部分及后面的部分全都用空格替代了? 问题2:以test2的应用为例,如果想用2个空格来替代123,写成test2=${test/123/" "}效果还是1个空格,如何实现呢? 谢谢回答!
我有几个数组 array0= (1 2 3 6 ) array1= (2 4 1 0 5 9 3 ) array2= (2 4 6 3 1 7 2 1 8) 还有一个变量tmp; 我现在的想法是: while [ $i -lt 0 ] do tmp="array$i" 将tmp变成一个数组,然后对数组元素:em12:进行操作 ==========这个地方该怎么办啊!!! done
cat file [code] aaaa bbbb aaaa cccc aaaa [/code] 下面的程序没问题: [code] #!/bin/sh tmp=`grep "aaaa" file |wc -l` if [ $tmp != "2" ] then echo "someting" fi [/code] 问题出在这里,变量tmp是数字形还是字符串形?我觉得是什么都能说的过去, 但是如果是数字形,上面的就不成立了,如下: [code] #!/bin/sh tmp=`grep "aaaa" file |wc -l` if [ $tmp -nq 2 ] then echo "someting" fi [/code] 请问这是什么道理呢? ...
[root@RedHat-LAMP chap04]# cat repatterns western north [root@RedHat-LAMP chap04]# cat repatterns | grep -P "th$" [root@RedHat-LAMP chap04]# cat repatterns | grep -P 'th$' [root@RedHat-LAMP chap04]# 都匹配不到以th结尾的行,求解~