TVP

# DBA需要掌握的shell知识while

Alfred Zhao

5020

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

## 1.顺序执行

``````#!/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.分支判断

``````#!/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``````

``````#!/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``````

``````#!/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(不定循环)

``````#!/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
done
echo -e "\nWelcome, My friend, Alfred.\n"``````

``````#!/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"

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.用分支判断哪些数据库默认端口在运行.

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

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

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

## reference

《鸟哥的Linux私房菜》

0 条评论

LV.

• 1.顺序执行
• 2.分支判断
• 3.循环结构
• while do done, until do done(不定循环)
• for do done(固定循环)
• 4.巩固练习
• reference
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建，轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量，更专注于业务发展，让企业一站式享受数据上云及分布式架构的技术红利！
领券