本文记录了写这个脚本中出现的一些问题,问题主要出现在正则表达式匹配不准确导致。

写一个脚本,要求:
     1、脚本具有-i和-I两个选项,optind.sh [-i ethenet | -I IP]
     2、根据用户输入来输出相应信息
     3、当用户用错选项输出帮助信息,如[-i inerface| -I ip]

初步写完,测试时发现:
     当./getinterface -i eth0,测试OK,可以输出相应的信息;
     当./ getinterface -i eth1,由于没有eth1,有相应的错误提示信息;
     然而,当 ./ getinterface -i eth时,应该有 "wrong ethenet card",但是却出现了“ eth: eth: error fetching interface information: Device not found ”报错。
修改前
 
    
  1. #!/bin/bash
  2. # Name: optind.sh
  3. # Description:
  4. # Author:
  5. # Version: 0.0.1
  6. # Date: 2016-05-12 10:02:22
  7. # Usage:
  8. show_ip() {
  9. # judge whether eth exists.
  10. if ! ifconfig | grep -o "^[^[:space:]]\{1,\}" | cut -d: -f 2 | grep $1 &> /dev/null; then
  11. return 1
  12. fi
  13. # print
  14. echo -n "$1: "
  15. ifconfig $1 | grep -o "inet addr:[0-9\.]\{7,\}" | cut -d: -f 2
  16. }
  17. show_eth() {
  18. # judge whether ip exists.
  19. if ! ifconfig | grep -o "inet addr:[0-9\.]\{7,\}" | grep $1 &> /dev/null; then
  20. return 2
  21. fi
  22. # print
  23. echo -n "$1: "
  24. ifconfig | grep -B 1 $1 | grep -o "^[^[:space:]]\{1,\}"
  25. }
  26. usage() {
  27. echo "getinterface.sh [-i inerface| -I ip]"
  28. }

  29. case $1 in
  30. -i | -I)
  31. while getopts ":i:I:" opt;do
  32. case $opt in
  33. i)
  34. show_ip $OPTARG
  35. [ $? -ne 0 ] && echo "wrong ethenet card"
  36. ;;
  37. I)
  38. show_eth $OPTARG
  39. [ $? -ne 0 ] && echo "wrong ip"
  40. ;;
  41. *)
  42. usage
  43. ;;
  44. esac
  45. done
  46. ;;
  47. *)
  48. usage
  49. ;;
  50. esac
     经分析, ifconfig | grep - o "^[^[:space:]]\{1,\}" | cut - d : - f 2 | grep $1,“eth”是“eth0”的子集,此处应该对$1做单词完全匹配,使用grep -E "\b$1\b"即可(见修改1)
    对于ifconfig | grep -o "inet addr:[0-9\.]\{7,\}" | grep "$1\b",也做了相应的修改 (见修改2)
修改后
 
    
  1. #!/bin/bash
  2. # Name: getinterface.sh
  3. # Description:
  4. # Author:
  5. # Version: 0.0.1
  6. # Date: 2016-05-12 10:02:22
  7. # Usage:
  8. show_ip() {
  9. # judge whether eth exists.
  10. # 修改1
  11. if ! ifconfig | grep -o "^[^[:space:]]\{1,\}" | cut -d: -f 2 | grep -E "\b$1\b" &> /dev/null; then
  12. return 1
  13. fi
  14. # print ip
  15. echo -n "$1: "
  16. ifconfig $1 | grep -o "inet addr:[0-9\.]\{7,\}" | cut -d: -f 2
  17. }
  18. show_eth() {
  19. # judge whether ip exists.
  20. # 修改2
  21. if ! ifconfig | grep -o "inet addr:[0-9\.]\{7,\}" | grep "$1\b" &> /dev/null; then
  22. return 2
  23. fi
  24. echo -n "$1: "
  25. ifconfig | grep -B 1 $1 | grep -o "^[^[:space:]]\{1,\}"
  26. }
  27. usage() {
  28. echo "getinterface.sh [-i interface| -I ip]"
  29. }

  30. case $1 in
  31. -i | -I)
  32. while getopts ":i:I:" opt;do
  33. case $opt in
  34. i)
  35. show_ip $OPTARG
  36. [ $? -ne 0 ] && echo "wrong ethenet card"
  37. ;;
  38. I)
  39. show_eth $OPTARG
  40. [ $? -ne 0 ] && echo "wrong ip"
  41. ;;
  42. *)
  43. usage
  44. ;;
  45. esac
  46. done
  47. ;;
  48. *)
  49. usage
  50. ;;
  51. esac

小插曲
    将写好的脚本粘贴到vim编辑器中,出现了每一行都在上一行的基础上向后缩进了很多。
分析
    设置了set autoindent,启用了自动缩进,但上面的情况显然违反了设置这个选项的本意。经查资料,找到了解决办法。
解决办法:
1. 在拷贝前输入:set paste (这样的话,vim就不会启动自动缩进,而只是纯拷贝粘贴)
2. 拷贝完成之后,输入:set nopaste (关闭paste)