Chinaunix首页 | 论坛 | 博客
  • 博客访问: 476920
  • 博文数量: 148
  • 博客积分: 4424
  • 博客等级: 上校
  • 技术积分: 1211
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-25 21:50
文章分类

全部博文(148)

文章存档

2012年(89)

2011年(20)

2010年(39)

分类: LINUX

2012-08-22 13:50:36

下面的shell有2个功能:
1)从字符串users中获取单独的user,并重命名
2) 判断user中是否包含特定字符串


点击(此处)折叠或打开

  1. #! /bin/sh
  2. users=120_AB_5152109,12_CD_05152128,1205_CC_152153

  3. field_number=`echo $users | awk 'BEGIN{FS = ",";}{print NF}'`
  4. count=1
  5. str="_AB_"

  6. while [ $count -le $field_number ]; do
  7. _user=`echo $users | cut -d ',' -f $count`
  8. count=$(( $count+1 ))
  9. var=$_user.db3
  10. echo $var
  11. echo "$var" |grep -q "$str"
  12. if [ $? -eq 0 ];
  13. then echo find $str in $var
  14. fi
  15. done
输出结果:
120_AB_5152109.db3
find _AB_ in 120_AB_5152109.db3
12_CD_05152128.db3
1205_CC_152153.db3

改良版本,查找多个字符串

点击(此处)折叠或打开

  1. #! /bin/sh
  2. users=222_CS_222,098_CH_333,120_AB_5152109,12_CD_05152128,1205_CC_152153

  3. field_number=`echo $users | awk 'BEGIN{FS = ",";}{print NF}'`
  4. count=1

  5. while [ $count -le $field_number ]; do
  6. _user=`echo $users | cut -d ',' -f $count`
  7. count=$(( $count+1 ))
  8. var=$_user.db3
  9. echo $var
  10. for i in "_AB_" "_CH_"
  11. do
  12. echo "$var" |grep -q "$i"
  13. if [ $? -eq 0 ]; then
  14. echo find $i in $var
  15. break
  16. fi
  17. done
  18. 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

更严格的写法
  1. #! /bin/sh
  2. users=222_CS_222,098_CH_333,120_AB_5152109,12_CD_05152128,1205_CC_152153

  3. field_number=`echo $users | awk 'BEGIN{FS = ",";}{print NF}'`
  4. count=1

  5. while [ $count -le $field_number ]; do
  6. _user=`echo $users | cut -d ',' -f $count`
  7. count=$(( $count+1 ))
  8. var=$_user.db3
  9. echo $var
  10. for i in _AB_ _CH_; do
  11. tmp=`echo $var | grep $i | wc -l` 2>./error.txt
  12. if [ $tmp -ne 0 ]; then
  13. echo find $i in $var
  14. break
  15. fi
  16. done
  17. done

参考文档:

http://www.blogjava.net/xzclog/archive/2011/03/04/345712.html

阅读(2590) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~