samsungsamsung 发表于 2018-8-29 09:07:08

shell学习之函数与库(二)

  1shell的库
  perl或c等语言中都有功能库。提供一些功能的集合,而shell中通过脚本的源引用,达到相同的作用。一般也是建议在设计的脚本程序文件夹下lib/存放相应的库文件,这些文件内写有常用的功能函数,方便调用。
  2库的创建和访问
  库的创建和脚本类似,也就是你看的的库通常就是一堆接受变量的函数,而需要的时候只需要 .   /PATH/TO/LIB或者source即可。bash首先会找$PATH目录中是否有库,然后找本目录下。
  3 一个简单的库调用
  # cat lib1
  mul(){
  echo -n "$1x$2的值是"
  echo "$1 * $2"|bc
  }
  运行结果为
  # cat calling.sh
  #!/bin/bash
  #
  #调用lib1库
  . ./lib1
  #使用lib1的乘法
  mulresult=`mul15`
  echo "乘法的结果为$mulresult"
  运行结果为:
  # ./calling.sh
  乘法的结果为1x5的值是5
  4下面介绍下书上的关于了解网络信息的脚本
  ①definitions.sh负责定义一些基本的变量
  ②network.sh负责定义查看网络信息的总过程
  ③redhat-network.sh负责关于redhat network的具体操作
  ④debian-network.sh负责关于debian系列关于网络的操作
  ⑤solaris-network.sh负责关于solaris系统的网络操作
  关于definitions.sh
  #define the error info
  _WRONG_PLATFORM=1
  _NO_IP=2
  _NO_CONFIG=3
  _SUCCESS=0
  关于network.sh
  #!/bin/bash
  #读取配置文件
  [ -z $_definitions ] &&. ./definitions.sh
  [ -f /etc/redhat-release ]&& . ./redhat-network.sh
  [ -f /etc/debian.release ]&&. ./debian-network.sh
  [ `uname`== "SunOS" ]&&. ./solaris-network.sh
  #读取网卡名称
  fornic in$*
  do
  #核心调用:通过网卡名称获得ip地址
  nicinfo=`getipaddr    $nic`
  case $? in
  $_NO_IP)echo "info----$nic has no ip.";;
  $_NO_CONFIG)echo "info----$nic has no config.";;
  $_WRONG_PLATFORM)echo "info----wrong platform";;
  $_SUCCESS)echo "info----$nic has ip $nicinfo";;
  esac
  done
  关于redhat-network.sh
  #specific method on redhat
  [ -z $_definitions ]&& . ./definitions.sh
  getipaddr(){
  if [ -d /etc/sysconfig/network-scripts ];then
  if [ -f /etc/sysconfig/network-scripts/ifcfg-$1 ];then
  unset IPADDR
  ./etc/sysconfig/network-scripts/ifcfg-$1
  [ -z $IPADDR ] && return $_NO_IP|| {echo $IPADDR;return $_SUCCESS}
  else
  return _$NO_CONFIG
  fi
  else
  return $_WRONG_PLATFORM
  fi
  }
  关于debian-network.sh
  先放张ubuntu配置的图片看看

  #specific method on debian
  [ -z $_definitions ]&&. ./definitions.sh
  getipaddr(){
  [ -f /etc/network/interfaces ]||return $_NO_CONFIG
  #设立标志位初始为0
  found=0
  #按行读取,分别赋值给keyword,argument,morestuff变量
  while read keyword argument morestuffs
  do
  #最终读到了带有首位iface的行
  if [ "$keyword" == "iface" ];then
  if [ "$found" eq"1" ];then
  #很巧妙,此时found值为1说明找到了这张网卡,只是找不到address的行,如果找到,那么就在下一个if返回了
  return $_NO_IP
  else
  #第一次执行该内容
  if [ "$argument"=="$1" ];then
  #找到这个网卡
  found=1
  fi
  fi
  fi
  if [ "$found" eq "1" ];then
  if [ "$keyword" == "addresss" ];then
  echo $argument
  return $_SUCCESS
  fi
  fi
  done /dev/null &&return $_WRONG_PLATFORM
  [ -f /etc/hostname.${1} ] ||return $_NO_CONFIG
  [ ! s /etc/hostname.$1 ] && return $_NO_IP
  getent hosts ` head -1 /etc/hostname.${1} | cut d"/" f1| awk '{print $1}'` |cut f1||cat /etc/hostname.${1}
  return $_SUCCESS
  }
  分析下各个脚本:就像程序开发一样,具体系统的操作系统如果有脚本处理,那么调用同名函数返回值相似就会让程序运行很方便,不必考虑具体细节
  最终程序的运行结果为
  # ./network.sh eth0 eth1 lo
  info----eth0 has no ip.
  info----eth1 has ip 192.168.177.130
  info----lo has ip 127.0.0.1
  缺陷是对于dhcp无法,其实可以修改下,只要增加判断$BOOTPROTO是否为dhcp(建议统一tr转换大小写),是则返回dhcpstyle即可
  # ./network.sh eth0 eth1 lo
  info--eth0 has dhcp style
  info----eth1 has ip 192.168.177.130
  info----lo has ip 127.0.0.1
  5介绍下getopts的使用
  ①getopts
  通过名字就知道是获取选项参数,写脚本必然用处极大,一般用于循环中
  getopts比较重要的几个参数
  optstringoption字符串,会逐个匹配
  varname    每次匹配成功的选项
  arg      参数列表,没写时它会取命令行参数列表
  $OPTIND    特殊变量,option index,会逐个递增, 初始值为1,表示第几个参数
  $OPTARG    特殊变量,option argument,不同情况下有不同的值
  getopts option variable
  通常我们的命令行模式ps -e
  1 getopts首先会找连字符-,找到后,就把连字符后面内容和option(一般为单个字符)比较,如果不一样,会错误返回;
  如果一样,则把variable赋值为那个选项。所以此时variable为e
  getopts option:
  当我们输入ls l/etc
  2如果选项后面还有参数,在getopts中需要加:才行,那么就把参数后面的值赋给$OPTARG,所以此时OPTARG为/etc,如果加了冒号,却没有参数,那么$OPTARG会被赋值为一个?,且getopts会返回错误信息。
  getopts :option:
  3 此时,如果匹配到option,而如果option满足,比如-h,那么由于第一个是:,相当于空,所以$OPTARG为选项h,这样方便自定义错误返回。
  ②程序实例
  1获得选项和参数
  # catgetopt1.sh
  #!/bin/bash
  #
  #get option_stringvariable
  while getopts 'a:q' argv
  do
  case $argv in
  a) echo "you entered $argv and $OPTARG
  next index is $OPTIND";;
  q) echo "exiting";exit 2;;
  esac
  done
  运行结果
  # ./getopt1.sh -a haha
  you entered a and haha
  next index is 3
  2 通过选项获得提示信息
  # cat getopt2.sh
  #!/bin/bash
  #
  #get option_stringvariable
  while getopts ':a:q' option
  do
  case $optionin
  a) echo "you \$OPTARG is$OPTARG
  next index is $OPTIND
  andthe variable is $option";;
  q) echo "exiting";exit 2;;
  ":")
  case $OPTARG in
  a)echo "please input -ayour string ";;
  esac
  esac
  done运行结果为
  # ./getopt2.sh-a
  please input -ayour string
  这位博主写的很好http://blog.163.com/leekwen@126/blog/static/3316622920101543555483/ 记录下

页: [1]
查看完整版本: shell学习之函数与库(二)