设为首页 收藏本站
查看: 862|回复: 0

DBA需要掌握的shell知识

[复制链接]

尚未签到

发表于 2015-12-3 14:29:42 | 显示全部楼层 |阅读模式
  每个中高级DBA都需要掌握一些简单脚本的编写,这样才能从繁杂重复的基础维护工作中解脱出来,才能有时间去研究更有价值的技术。VBird在讲shell script的时候,给出了几个经典的小范例练习,对于初学shell的人来说是很好的入门,现就根据VBird给出的几个典型练习进行近一步的系统整理,总结出bash shell的系统知识,希望能给各位读者起到抛砖引玉的作用。


  • 顺序执行  

  • 分支判断  

  • 循环结构  

  • 巩固练习
1.顺序执行
  练习1:用户选择输入Y/N,不区分大小写,根据用户输入屏幕打印不同内容。
  考查:read,[],exit 0,&&,echo
  

#!/bin/bash  
#Usage: user input a charector, program shows the different result.
  
#Author: Alfred Zhao
  
#Creation: 2015-05-06
  
#Version: 1.0.0
  

  
#1.Input 'Y' or 'N'
  
read -p "Input (Y/N)" input
  
[ "$input" == "Y" -o "$input" == "y" ] && echo -e "you choice is: $input\n" && exit 0
  
[ "$input" == "N" -o "$input" == "n" ] && echo -e "you choice is: $input\n" && exit 0
  
echo -e "I don't know what your choice is" && exit 0
  

2.分支判断
  两种常用的分支判断:if...else...fi分支判断,case...esac分支判断。
  练习2:将练习1中的代码改写为if分支判断,使程序的执行逻辑更直观。
  考查:==,||
  if[]; then
  ...
  elif[]; then
  ...
  else
  ...
  fi
  

#!/bin/bash  
#Usage: user input a charector, program shows the different result.
  
#Author: Alfred Zhao
  
#Creation: 2015-05-06
  
#Version: 1.0.1
  

  
#1.Input 'Y' or 'N'
  
read -p "Input (y/n)" input
  
if [ "$input" == "Y" ] || [ "$input" == "y" ]; then
  echo -e "you choice is: $input\n"
  exit 0
  
elif [ "$input" == "N" ] || [ "$input" == "n" ]; then
  echo -e "you choice is: $input\n"
  exit 0
  
else
  echo -e "I don't know what you choice is.\n"
  exit 0
  
fi
  

  练习3:用分支判断来辨别参数1的输入是否合法。
  考查:$0,$1
  

#!/bin/bash
  
#Usage: To judge $1's>  
#Author: Alfred Zhao
  
#Creation: 2015-05-07
  
#Version: 1.0.0
  

  
if [ "$1" == "Alfred" ]; then
  echo -e "Authorization Successful! \n"
  exit 0
  
elif [ "$1" == "" ]; then
  echo -e "Waring: Authorization is null! ex> {$0 Username}\n"
  exit 0
  
else
  echo -e "Waring: Only Alfred can be authorized. ex> {$0 Alfred}\n"
  exit 0
  
fi
  

  练习4:用case判断改写练习3.
  考查:case...esac判断
  

#!/bin/bash
  
#Usage: To judge $1's>  
#Author: Alfred Zhao
  
#Creation: 2015-05-07
  
#Version: 1.0.1
  

  
case "$1" in
  "Alfred")
  echo -e "Authorization Successful! \n"
  ;;
  "")
  echo -e "Waring: Authorization is null! ex> {$0 Username}\n"
  ;;
  *)
  echo -e "Waring: Only Alfred can be authorized. ex> {$0 Alfred}\n"
  ;;
  
esac
  

3.循环结构
while do done, until do done(不定循环)
  练习5:输入名字直到输入的名字是“Alfred”为止。
  考查:while do done
  

#!/bin/bash  
#Usage: Input the name until it is "Alfred".
  
#Author: Alfred Zhao
  
#Creation: 2015-05-07
  
#Version: 1.0.0
  

  
while [ "$name" != "Alfred" ]
  
do
  read -p "Please Input your name: " name
  
done
  
echo -e "\nWelcome, My friend, Alfred.\n"
  

  而如果是使用until do done,
  只需要修改while [ "$name" != "Alfred" ]为until [ "$name" == "Alfred" ]
  练习6:计算1+2+3+...+num的结果
  考察:正则
  

#!/bin/bash  
#Usage: Calculate the result "1+2+...+num".
  
#Author: Alfred Zhao
  
#Creation: 2015-05-07
  
#Version: 1.0.0
  

  
i=0 #i
  
s=0 #sum
  

  
echo -e "This program will help you calculate the result of '1+2+...+num'\n"
  
read -p "Please input your num: " num
  

  
if [ "$(echo "$num"|grep '[0-9]'|grep -v '[:alpha:]')" == "" ]; then
  echo -e "Waring: Please input a number.\n"
  exit 1
  
elif [ "$num" -lt "1" ]; then
  echo -e "Waring: Not support.\n"
  
elif [ "$num" == "1" ]; then
  echo -e "1=1\n"
  exit 0
  
elif [ "$num" == "2" ]; then
  echo -e "1+2=3\n"
  exit 0
  
elif [ "$num" == "3" ]; then
  echo -e "1+2+3=6\n"
  exit 0
  
else
  while [ "$i" != "$num" ]
  do
  i=$(($i+1))
  s=$(($s+$i))
  done
  

  echo -e "\n1+2+...+$num= $s\n"
  exit 0
  
fi
  

for do done(固定循环)
  for do done 第一种用法示例:
  练习7:循环输出变量who的内容
  

#!/bin/bash  
#Usage: for do done
  
#Author: Alfred Zhao
  
#Creation: 2015-05-07
  
#Version: 1.0.0
  

  
for who in mum dad brother sister
  
do
  echo -e "This is my ${who}.\n"
  
done
  

  for do done 第二种用法示例:
  练习8:计算1+2+..+100的值
  

#!/bin/bash  
#Usage: 1+2+...+100
  
#Author: Alfred Zhao
  
#Creation: 2015-05-07
  
#Version: 1.0.0
  
sum=0

  
for ((i=1; i

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-146886-1-1.html 上篇帖子: shell脚本监控Flume输出到HDFS上文件合法性 下篇帖子: 使用adb shell dumpsys检测Android的Activity任务栈
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表