下面的shell有2个功能:
1)从字符串users中获取单独的user,并重命名
2) 判断user中是否包含特定字符串
- #! /bin/sh
- users=120_AB_5152109,12_CD_05152128,1205_CC_152153
- field_number=`echo $users | awk 'BEGIN{FS = ",";}{print NF}'`
- count=1
- str="_AB_"
- while [ $count -le $field_number ]; do
- _user=`echo $users | cut -d ',' -f $count`
- count=$(( $count+1 ))
- var=$_user.db3
- echo $var
- echo "$var" |grep -q "$str"
- if [ $? -eq 0 ];
- then echo find $str in $var
- fi
- done
输出结果:
120_AB_5152109.db3
find _AB_ in 120_AB_5152109.db3
12_CD_05152128.db3
1205_CC_152153.db3
改良版本,查找多个字符串
- #! /bin/sh
- users=222_CS_222,098_CH_333,120_AB_5152109,12_CD_05152128,1205_CC_152153
- field_number=`echo $users | awk 'BEGIN{FS = ",";}{print NF}'`
- count=1
- while [ $count -le $field_number ]; do
- _user=`echo $users | cut -d ',' -f $count`
- count=$(( $count+1 ))
- var=$_user.db3
- echo $var
- for i in "_AB_" "_CH_"
- do
- echo "$var" |grep -q "$i"
- if [ $? -eq 0 ]; then
- echo find $i in $var
- break
- fi
- done
- done
运行结果:
222_CS_222.db3
098_CH_333.db3
find _CH_ in 098_CH_333.db3
120_AB_5152109.db3
find _AB_ in 120_AB_5152109.db3
12_CD_05152128.db3
1205_CC_152153.db3
更严格的写法
- #! /bin/sh
- users=222_CS_222,098_CH_333,120_AB_5152109,12_CD_05152128,1205_CC_152153
- field_number=`echo $users | awk 'BEGIN{FS = ",";}{print NF}'`
- count=1
- while [ $count -le $field_number ]; do
- _user=`echo $users | cut -d ',' -f $count`
- count=$(( $count+1 ))
- var=$_user.db3
- echo $var
- for i in _AB_ _CH_; do
- tmp=`echo $var | grep $i | wc -l` 2>./error.txt
- if [ $tmp -ne 0 ]; then
- echo find $i in $var
- break
- fi
- done
- done
参考文档:
http://www.blogjava.net/xzclog/archive/2011/03/04/345712.html
阅读(2596) | 评论(0) | 转发(1) |