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

  1. 顺序执行
  2. 分支判断
  3. 循环结构
  4. 巩固练习

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 identity. Aha, Only Alfred is ok.
#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 identity. Aha, Only Alfred is ok.
#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<=100; i=i+1))
do
sum=$(($sum+$i))
done echo -e "The result is $sum.\n"

4.巩固练习

1.用分支判断哪些数据库默认端口在运行.

提示:不同数据库的默认监听端口不同

Oracle数据库判断netstat -tuln |grep ":1521 "是否有结果;

Mysql数据库判断netstat -tuln |grep ":3306 "是否有结果;

IEE数据库判断netstat -tuln |grep ":5029 "是否有结果;

Vertica数据库判断netstat -tuln |grep ":5433 "是否有结果.

2.输入毕业日期,计算当前离毕业还有多少天。

提示:将时间换算成秒,相减后换算成天数。

day1=$(date --date="20150507" +%s)

day2=$(date --date="20160630" +%s)

days=$((($day2-$day1)/3600/24))

3.检查Linux系统所有用户的标识符与特殊参数

提示:cut -d ':' -f1 /etc/passwd

4.检查192.168.1.1~192.168.1.100的主机网络情况

提示:for site in $(seq 1 100)

reference

《鸟哥的Linux私房菜》

DBA需要掌握的shell知识的更多相关文章

  1. 每天一个shell知识--数组

    1.shell中数组的定义: 数组名=(value value1 value2 ) 也可以单独的设定数组的分量: arrayL[0]=value arrayL[1]=value1 2.${arrayL ...

  2. 几个常用的linux快捷键和shell知识

    1)   !$    !$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串.如:你可能会这样:     $mkdir mydir     $mv mydir yourdir     $cd y ...

  3. shell知识

    shell基本的语句 一:if语句 格式: if [ #条件的判断 ];then #执行的动作 fi 操作符 + 加 - 减 * 乘 /   除[取整数商] % 余[取余数] 数值的比较 参数 说明 ...

  4. shell知识积累

    Ubuntu下常用的快捷键:https://blog.csdn.net/u010771356/article/details/53543041 变量名和等号之间不能有空格,变量名中间不能有空格,可以使 ...

  5. 看懂shebang吧,只需一点点shell知识,从此再也不犯强迫症

    Python2: 开启一个terminal,输入下面命令: yshuangj@ubuntu:~$ vim helloA.py 在vim编辑器中,进入编辑模式(按i),输入下面的代码,然后退出编辑模式( ...

  6. shell 知识

    解压 tar.bz2文件 bunzip2 linux-2.6.13.tar.bz2 | tar xvf -

  7. 8个DBA最常用的监控Oracle数据库的常用shell脚本

    本文介绍了8个常用的监控数据shell脚本.首先回顾了一些DBA常用的Unix命令,以及解释了如何通过Unix Cron来定时执行DBA脚本.网上也有好多类似的文章,但基本上都不能正常运行,花点时间重 ...

  8. 1_mysql +DBA职业发展

    MYSQL + DBA 职业发展 mysql :the world's most popular open source database 最流行的开源数据库 数据库世界 关系数据库(又称SQL数据库 ...

  9. Oracle DBA 的常用Unix参考手册(一)

    作为一名Oracle DBA,在所难免要接触Unix,但是Unix本身又是极其复杂的,想要深刻掌握同样很不容易.那么到底我们该怎么入手呢?Donald K Burleson 的<Unix for ...

随机推荐

  1. 学习Swift,一定不能错过的10大开源项目!

    如果你是位iOS开发者,或者你正想进入该行业,那么Swift为你提供了一个绝佳的机会.Swift的设计非常优雅,较Obj-C更易于学习,当然也非常强大. 为了指导开发者使用Swift进行开发,苹果发布 ...

  2. ADB工具包15秒快速安装器,已集合ADB、FASTBOOT工具箱和最新的驱动程序

    http://www.cnroms.com/adb-and-fastboot-toolkit-with-google-usb-drivers.html 通过电脑管理安卓手机需要的三个最常用的工具包集合 ...

  3. mac os x用macport安装redis

    一.Redis简要介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的 ...

  4. web跨域解决方案

    阅读目录 什么是跨域 常用的几种跨域处理方法: 跨域的原理解析及实现方法 总结 摘要:跨域问题,无论是面试还是平时的工作中,都会遇到,本文总结处理跨域问题的几种方法以及其原理,也让自己搞懂这方面的知识 ...

  5. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  6. sap 怎么导出sap的各种表

    其实方法很简单,只是以前一直没用过! 进入事务码se84

  7. Mac 下配置 SSH 免密码安全登录

    Win下个人常使用 SecureCRT ,Mac 下感觉 SecureCRT 并不好使,常用 iTerm2+zsh 搭配使用.A连接B 无密码登陆,则A上面执行 ssh-keygen 一路回车,把 ~ ...

  8. 2016年象行中国(上海站)圆满结束,会议PPT分享

    2016年象行中国(上海站)已于5-21日圆满结束,所有技术交流的文档和PPT经整理后,现集中存放在云盘中,相关议题如下:     DeepGreen-LLVM-Intro.pptx ... 1.1M ...

  9. schedule() 和 scheduleAtFixedRate() 区别

    1.  schedule() ,2个参数方法:在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行. ...

  10. IOS 开发环境,证书和授权文件等详解

    (转自:http://blog.csdn.net/gtncwy/article/details/8617788) 一.成员介绍1.    Certification(证书)证书是对电脑开发资格的认证, ...