关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » .NET »DBA需要掌握的Shell知识

    DBA需要掌握的Shell知识

    2015-05-07 00:00:00 出处:meituan
    分享

    每个中高级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: $inputn" && exit 0
    [ "$input" == "N" -o "$input" == "n" ] && echo -e "you choice is: $inputn" && 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: $inputn"
    	exit 0
    elif [ "$input" == "N" ] || [ "$input" == "n" ]; then
    	echo -e "you choice is: $inputn"
    	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=1n"
            exit 0
    elif [ "$num" == "2" ]; then
            echo -e "1+2=3n"
            exit 0
    elif [ "$num" == "3" ]; then
            echo -e "1+2+3=6n"
            exit 0
    else
            while [ "$i" != "$num" ]
            do
                    i=$(($i+1))
                    s=$(($s+$i))
            done
    
            echo -e "n1+2+...+$num= $sn"
            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)

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    帝国CMS7.5编辑器上传图片取消宽高的三种方法

    帝国cms如何自动生成缩略图的实现方法

    Windows 12即将到来,将彻底改变人机交互

    帝国CMS 7.5忘记登陆账号密码怎么办?可以phpmyadmin中重置管理员密码

    帝国CMS 7.5 后台编辑器换行,修改回车键br换行为p标签

    Windows 11 版本与 Windows 10比较,新功能一览

    Windows 11激活产品密钥收集及专业版激活方法

    如何从 Windows 11 中完全删除/卸载 OneNote?无解!

    抖音安全与信任开放日:揭秘推荐算法,告别单一标签依赖

    ultraedit编辑器打开文件时,总是提示是否转换为DOS格式,如何关闭?

    IT头条

    华为Pura80系列新机预热,余承东力赞其复杂光线下的视频拍摄实力

    01:28

    阿里千问3开源首战告捷:全球下载破千万,国产AI模型崛起新高度!

    01:22

    DeepSeek R1小版本试升级:网友实测编程能力已达到国际一线水平

    23:15

    NVIDIA 与 Dell 合作,大规模交付 Blackwell AI 系统

    20:52

    Cerebras 以最快的 Llama 4 Maverick 性能引领 LLM 推理竞赛

    20:51

    技术热点

    PHP中的随机性——你觉得自己幸运吗?

    搞定Ubuntu Linux下WPA无线上网

    Java使用内存映射实现大文件的上传

    MySQL安全性指南

    MySQL两项性能的基本测试浅谈

    教您使用UniqueIdentifier选取SQL Server主键

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。