前不久入职实习生,现在在帮着组里面dalao们跑Case,时不时要上去收一下有木有Dump,每次敲命令太烦人于是逼着自己学写Shell脚本。一开始真的是很痛苦啊,也没能搞到书,只能凭网上半真半假的消息照葫芦画瓢!废话少说,上正文!
=========================我是分割线=========================
- 1 clear
- 2
- 3 echo "Check_dump is a tool help you check dumps"
- 4
- 5 numberOfSPs=2
- 6 commands="-lcd"
- 17 ###########################################################
- 18 #
- 19 # getopts 命令是 Korn/POSIX shell 的内置命令
- 20 # ,用来从参数列表检索选项以及选项参数。选项由一个+(加号)
- 21 # 或者是由一个-(减号)后跟一个字符开始。一个既不是以+,也不
- 22 # 是以-开始的选项结束选项字符串。每次调用 getopts 命令时,它
- 23 # 将下一个选项的值放置在 Name 内,并将下一个要处理的参数的索引
- 24 # 置于 shell 变量 OPTIND 中。每当调用 shell 时,都会将
- 25 # OPTIND 初始化为 1。当选项以 + 开头时,会将 + 预
- 26 # 追加到 Name 中的值。
- 27 #
- 28 ##########################################################
- 29
- 30 while getopts ":a:b:c:n:" opt # 如果选项字符串中的字符后面带有“:”(冒号),那么预期此选项将带有参数。
- 31 # 在这里a前面有没有冒号会决定下面的*/?能不能起效
- 32 # 详细的说明可以参考 https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_71/com.ibm.aix.cmds2/getopts.htm
- 33 do
- 34 case $opt in
- 35 a)
- 36 d1=$OPTARG # 当选项需要选项参数时,getopts 命令就将其置于变量 OPTARG 中
- 37 echo "d1 is $d1 "
- 38 ;; # 一定要记得加上末尾的;;,相当于其它语言的 break
- 39 b)
- 40 d2=$OPTARG # OPTARG 出现了,他就是取得参数的具体地方
- 41 echo "d2 is $d2, and destination is $d1.$d2" # Shell 里面的变量很有意思,直接这样写就能顺着输出出来
- 42 ;; # 双引号会解析字符串里面的转义字符和变量名,单引号不会
- 43 c)
- 44 commands=$OPTARG
- 45 echo " Addtional command: $commands "
- 46 ;;
- 47 n)
- 48 numberOfSPs=$OPTARG
- 49 echo "Number of SPs has changed to $numberOfSPs "
- 50 ;;
- 51 *) # * 或者 ? 匹配所有不是上面列出的东东
- 52 echo "Usage: check_dump -a<xxx.xxx.xxx> -b<xxx> -c<commands> -n<number of SP>
- 53 And the destination is IP for Unisphere."
- 54 exit
- 55 esac
- 56 done
- 57
- 58 i=1
- 59 while [ $i -le $numberOfSPs ]
- 60 do
- 61 d2=$((d2+1))
- 62 destination="$d1.$d2"
- 63 echo "=============================================
- 64 checking SP$i, destination is :$destination" # 很有趣吧,直接在字符串里面换行,打印输出内容也会换行!
- 65 i=$((i+1)) # $(()) 和 下面的 ``是一样的,表示里面的东东可以执行,需要执行以后再做其他操作
- 66 res="`ssh $destination svc_dc $commands`"
- 67 if [[ ${#res} == 234 ]];then # 比较应该深度学习的“expr指令”的缩写版本,原版有些指令少而不太好使
- 68 # 看到if后面跟的什么了没!!!!";then""五个字符一个不能少!
- 69 echo "No dumps"
- 70 else # 分支比较多的话还可以选择用elif[[ condition ]];then
- 71 echo "Found dumps:
- 72 $res"
- 73 fi
- 74
- 75 done
- 76 exit
就是这样子,在想到什么就再往上面丢点什么吧!