运维Shell脚本小试牛刀(一)
运维Shell脚本小试牛刀(二)
一: if---else.....fi 条件判断演示
[root@www shelldic]# cat checkpass.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: checkpass.sh
# USAGE: ./checkpass.sh
# DESCRIPTION:
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 显示提示用户输入密码,然后从标准输入隐式地读取用户输入,并将读取的内容赋值给变量pass
read -sp "Enter a password: " pass# 如果变量pass的值为laogao,则显示验证通过的信息,然后退出脚本的执行,退出状态码为0
if test "$pass" == "laogao"
then
echo -e "\nPasswrod verified"
exit 0fi
#退出脚本执行,退出状态码为1
exit 1
二: if---else if----else---fi fi 多层嵌套,根据不同的分支做出不同的决策
[root@www shelldic]# cat multeCheckpass.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: multeChckepass.sh
# USAGE: ./multeCheckpass.sh
# DESCRIPTION: 该实例主要是演示if ----else-----fi 根据条件的不同结果可以采取不同的行为
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-29 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================read -sp "请输入登录密码: " pass
if [ "$pass" == "yangge" ]
then
echo -e "\n你已经通过验证...."
exit 0else
if [ "$pass"=="yangyangge" ]then
echo -e "\n你输入的信息为: ${pass}, 你用通过认证....."else
echo "\n 你输入的密码为:$pass , 该密码错误,你将被拒绝再次访问....."
fi
fi
[root@www shelldic]# cat multeNestecheckcount.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: multeNestecheckcount.sh
# USAGE: ./multeNestechckcount.sh
# DESCRIPTION: if----else---fi 多级嵌套
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 声明一个变量
declare -i count# 提示用户输入一个数值,然后把该变量存入count变量中
read -p "请输入一个数值: " count
if [ $count -eq 100 ]
then
echo -e "\nyou enter data Count is 100"else
if [ $count -gt 100 ]
then
echo -e "\nyou enter data count greater than 100."else
echo -e "\n you enter data count less than 100..."
fifi