实现有价值的IT服务
全部博文(709)
分类: LINUX
2005-11-13 22:21:34
rindex.sh代码:
#!/bin/bash
strToCheck=$1;
charToSearch=$2;
let pos=`echo "$strToCheck" | awk -F ''$charToSearch'' '{printf "%d", length($0)-length($NF)}'`
echo "char $charToSearch lastpos is: $pos"
使用示例:
[zeal]$ sh rindex.sh .
char . lastpos is: 10
则提供了一种比较自力更生的方式:自己用c写了个小程序来实现rindex的功能,gcc rindex.c -o rindex 之后shell就拥有可以调用的rindex了 :)
rindex.c代码:
int main(int argc, char *argv[]){
char* wholeWord;
char subChar;
char* subWord;
int ret;
if(argc!=3){
printf("Use:rindex word char ");
exit(0);
}
wholeWord=argv[1];
subChar=*argv[2];
subWord=rindex(wholeWord,subChar);
if(0 == subWord)
ret = 0;
else
ret = (subWord-wholeWord+1);
printf("%d ",ret);
}