当前位置:网站首页>Shell(4)条件控制语句
Shell(4)条件控制语句
2022-08-05 04:16:00 【AHui_CSDN】
一. while循环
当前条件表达式成立(为真)则执行do后面的命令
语法结构:
while [ 条件表达式 ]
do
命令的集合
done
案例: 写一个死循环脚本
第一种
[[email protected] ]# cat while.sh
while true
do
echo test
sleep 2
done
第二种
[[email protected] day3]# cat while.sh
while [ 10 -gt 5 ]
do
echo test
sleep 2
done
第三种
[[email protected] day3]# cat while.sh
while [ -f /etc/hosts ]
do
echo test
sleep 2
done
while数字循环
[[email protected] ]# cat while.sh
i=1
while [ $i -le 10 ]
do
echo $i
let i++
done
[[email protected] ]# sh while.sh
1
2
3
4
5
6
7
8
9
10
案例: while从1加到100
[[email protected] ]# cat while.sh
i=1
while [ i − l e 100 ] d o c o u n t = i -le 100 ] do count= i−le100]docount=[count+i]
let i++
done
echo $count
[[email protected] ]# sh while.sh
5050
案例: while读取文件
for循环读取文件按照空格分割 while循环按照行分隔
[[email protected] ]# cat read.sh
#!/bin/bash
while read line
do
echo $line
done</etc/hosts
[[email protected] ]# sh read.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
案例: while创建用户
使用for循环(命令行行执行)
[[email protected] ]# for i in 取值列表; do 执行的命令;done
[[email protected] ]# for i in `cat user.txt`;do echo $i;done
aa
qq
bb
cc
[[email protected] ]# for i in `cat user.txt`;do useradd $i;done
[[email protected] ]# tail -4 /etc/passwd
aa:x:1000:1000::/home/aa:/bin/bash
qq:x:1001:1001::/home/qq:/bin/bash
bb:x:1002:1002::/home/bb:/bin/bash
cc:x:1003:1003::/home/cc:/bin/bash
for循环删除用户
[[email protected] ]# for i in `cat user.txt`;do userdel -r $i;done
使用while循环批量创建用户
[[email protected] ]# cat user.sh
#!/bin/bash
while read line
do
useradd $line
done< user.txt
[[email protected] ]# sh user.sh
[[email protected] ]# tail -4 /etc/passwd
aa:x:1000:1000::/home/aa:/bin/bash
qq:x:1001:1001::/home/qq:/bin/bash
bb:x:1002:1002::/home/bb:/bin/bash
cc:x:1003:1003::/home/cc:/bin/bash
通过用户传参的方式批量创建用户
[[email protected] ]# cat user.sh
#!/bin/bash
read -p "请输入用户的前缀名称: " pre
read -p "请输入创建用户的个数: " num
i=1
while [ $i -le $num ]
do
user=${pre}$i
id $user &>/dev/null
re=$?
if [ $re -ne 0 ];then
useradd $user
[ $? -eq 0 ] && echo "$user 创建成功"
elif [ $re -eq 0 ]
echo "$user 已经存在"
fi
let i++
done
while多级跳
三层循环:
[[email protected] ]# cat while1.sh
#!/bin/bash
while true
do
echo "第一级"
sleep 1
while true
do
echo "第二级"
sleep 1
while true
do
echo "第三级"
sleep 1
done
done
done
echo done....................
break跳出循环
[[email protected] ]# cat while1.sh
#!/bin/bash
while true
do
echo "第一级"
sleep 1
while true
do
echo "第二级"
sleep 1
while true
do
echo "第三级"
sleep 1
break 3
done
done
done
echo done....................
exit 退出整个脚本
continue 忽略剩余代码从头继续执行
[[email protected] ]# cat while1.sh
#!/bin/bash
while true
do
echo "第一级"
sleep 1
while true
do
echo "第二级"
sleep 1
continue
while true
do
echo "第三级"
sleep 1
done
done
done
echo done....................
break 跳出循环体继续执行
[[email protected] ]# cat user.sh
#!/bin/bash
read -p "请输入用户的前缀名称: " pre
read -p "请输入创建用户的个数: " num
i=1
while [ $i -le $num ]
do
user=${pre}$i
id $user &>/dev/null
if [ $? -ne 0 ];then
useradd $user
[ $? -eq 0 ] && echo "$user 创建成功"
else
break
fi
let i++
done
echo done..............
continue:
[[email protected] ]# cat user.sh
#!/bin/bash
read -p "请输入用户的前缀名称: " pre
read -p "请输入创建用户的个数: " num
i=0
while [ $i -le $num ]
do
let i++
user=${pre}$i
id $user &>/dev/null
if [ $? -ne 0 ];then
useradd $user
[ $? -eq 0 ] && echo "$user 创建成功"
else
continue
fi
done
echo done..............
#!/bin/bash
>test.txt
while true
do
ran=`echo $((RANDOM%100+1))`
if [ `grep -w $ran test.txt|wc -l` -eq 1 ];then
continue
fi
echo $ran >> test.txt
done
二.函数
函数的特点:
- 先定义在调用 如果只定义不调用则脚本不会执行(变量只定义不调用也会执行赋值)
- 函数为命令(代码)的集合(变量只能赋一个值 函数可以赋多个值)
- 可以重复调用
调用脚本的变量
[[email protected] ]# cat 1.sh
name=oldboy
[[email protected] ]# cat 2.sh
. /server/scripts//1.sh
read -p "请输入你的年龄: " num
echo 名字: $name 年龄: $num
1.函数的定义
[[email protected] ]# cat fun.sh
#!/bin/bash
fun1(){
echo "第一种书写方式"
}
function fun2 {
echo "第二种书写方式"
}
function fun3(){
echo "第三种书写方式"
}
fun1 # 调用函数 直接在函数的下面写函数名称即可调用
fun2
fun3
函数复用:
[[email protected] ]# source fun.sh
第一种书写方式
第二种书写方式
第三种书写方式
[[email protected] ]# fun1
第一种书写方式
[[email protected] ]# fun2
第二种书写方式
[[email protected] ]# fun3
第三种书写方式
2.函数的传参
函数无法直接接收脚本的传参
需要使用函数的传参方式进行传参
直接在调用函数的后面进行传参
[[email protected] ]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $1 ]
then
echo "$1 文件存在"
else
echo "$1 文件不存在"
fi
}
fun /etc/hosts /etc/passwd # 在调用函数名称后面直接传参
[[email protected] ]# sh fun.sh
/etc/hosts 文件存在
[[email protected] ]# sh -x fun.sh
+ fun /etc/hosts /etc/passwd
+ '[' -f /etc/hosts ']'
+ echo '/etc/hosts 文件存在'
/etc/hosts 文件存在
[[email protected] ]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $2 ]
then
echo "$2 文件存在"
else
echo "$2 文件不存在"
fi
}
fun $2 $1
[[email protected] ]# sh fun.sh /etc/hosts /etc/passwd
/etc/hosts 文件存在
3.函数的变量
函数中支持当前的全局变量
[[email protected] ]# cat fun.sh
#!/bin/bash
file=/etc/hosts
fun(){
if [ -f $file ]
then
echo "$file 文件存在"
else
echo "$file 文件不存在"
fi
}
fun
[[email protected] ]# cat fun.sh
#!/bin/bash
num=2
fun(){
for i in `seq $num`
do
count=$[$count+$num]
done
echo $count
}
fun
定义函数的本地变量: 只在函数体内生效
[[email protected] ]# cat fun.sh
#!/bin/bash
fun(){
local num=20
for i in `seq $num`
do
count=$[$1+$num]
done
echo $count
}
fun 10
echo $num
4.函数的返回值
通过exit返回状态码:
[[email protected] day2]# cat env.sh
#!/bin/sh
read -p "Please Input name env: " name
[ -z $name ] && echo "必须输入名称" && exit
if [[ $name =~ ^[a-Z]+$ ]];then
echo $name
else
exit 100
fi
read -p "Please Input age env: " age
if [[ $age =~ ^[0-9]{
2}$ ]];then
echo $age
else
exit 200
fi
echo $name $age
通过赋值方式:
[[email protected] ]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $1 ]
then
return 50
else
return 100
fi
}
fun $1
re=$?
if [ $re -eq 50 ]
then
echo "$1 文件存在"
elif [ $re -eq 100 ]
then
echo "$1 文件不存在"
fi
[[email protected] ]# sh fun.sh /etc/passwd
/etc/passwd 文件存在
[[email protected] ]# sh fun.sh /etc/passwdssss
/etc/passwdssss 文件不存在
案例: 重复判断
# 判断下载YUM仓库是否成功的函数
test_yum(){
if [ $re -eq 0 ];then
action "yum仓库安装" /bin/true
else
action "yum仓库安装" /bin/false
fi
}
if [ ${os_vs%%.*} -eq 7 ]
then
# 备份默认YUM仓库
$backup_yum
# 下载新的YUM仓库
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
#将执行的返回结果赋值给re
re=$?
#调用函数名称执行函数体中的命令
test_yum
elif [ ${os_vs%%.*} -eq 6 ]
案例: 显示主菜单
[[email protected] ]# cat menu.sh
#!/bin/bash
menu1(){
echo "1.PHP"
echo "2.MySQL"
echo "h.显示主菜单"
}
menu1
while true
do
read -p "请输入你要安装的服务编号: " num
if [ $num = 1 ];then
while true
do
echo "1.PHP1.1"
echo "2.PHP1.2"
echo "3.返回主菜单"
read -p "请输入安装的版本号: " re
if [ $re -eq 1 ];then
echo yum php1.1.....
elif [ $re -eq 3 ];then
break
fi
done
elif [ $num = 2 ];then
echo "1.MySQL1.1"
echo "2.MySQL1.2"
elif [ $num = h ];then
menu1
fi
done
三. case
语法结构:
变量: 直接取值 read读入 赋值
case 变量 in
匹配模式1)
命令集合
;;
匹配模式2)
命令集合
;;
匹配模式3)
命令集合
;;
*)
没有匹配到上面的模式 则执行 * 下的命令
esac
案例:创建或删除用户
[[email protected] ]# cat case.sh
#!/bin/bash
for i in `seq 10`
do
echo oldboy$i
done
read -p "选择删除或者创建以上用户:[y创建|d删除] " num
case $num in
y)
for a in `seq 10`
do
user=oldboy$a
id $user &>/dev/null
if [ $? -eq 0 ];then
echo $user 用户存在
else
useradd $user
[ $? -eq 0 ] && echo $user 创建成功
fi
done
;;
d)
for a in `seq 10`
do
user=oldboy$a
id $user &>/dev/null
if [ $? -eq 0 ];then
userdel -r $user
[ $? -eq 0 ] && echo $user 删除成功
else
echo "$user 用户不存在"
fi
done
;;
*)
echo "Usage: $0 [y|yes|d|del]"
esac
for循环包含case语句
[[email protected] ]# cat case.sh
#!/bin/bash
for i in `seq 10`
do
echo oldboy$i
done
read -p "选择删除或者创建以上用户:[y创建|d删除] " num
for a in `seq 10`
do
case $num in
y)
user=oldboy$a
id $user &>/dev/null
if [ $? -eq 0 ];then
echo $user 用户存在
else
useradd $user
[ $? -eq 0 ] && echo $user 创建成功
fi
;;
d)
user=oldboy$a
id $user &>/dev/null
if [ $? -eq 0 ];then
userdel -r $user
[ $? -eq 0 ] && echo $user 删除成功
else
echo "$user 用户不存在"
fi
;;
*)
echo "Usage: $0 [y|yes|d|del]"
esac
done
案例: 查看系统信息
菜单:
- 显示登录信息(执行w)
- 显示内存
- 显示磁盘
- 显示IP地址
- 显示外网IP地址
- 显示主机名称
- 显示菜单(可以先清屏在显示菜单)
通过case语句执行 写入死循环
[[email protected] ]# cat os.sh
#!/bin/bash
menu(){
echo -e "\t\t\t\t\t\t1.f查看内存"
echo -e "\t\t\t\t\t\t2.d查看磁盘"
echo -e "\t\t\t\t\t\t3.u查看负载"
echo -e "\t\t\t\t\t\t4.l查看登录信息"
echo -e "\t\t\t\t\t\t5.c查看外网IP地址"
echo -e "\t\t\t\t\t\t6.m显示菜单"
echo -e "\t\t\t\t\t\t7.q退出脚本"
}
menu
while true
do
read -p "请输入要查看的系统信息编号[1|f]: " num
case $num in
1|f)
free -h
;;
2|d)
df -h
;;
3|u)
uptime
;;
4|l)
w
;;
5|c)
curl cip.cc
;;
6|m)
clear
menu
;;
7|q)
exit
;;
*)
echo "Usage: $0 [1|2|3|4|5|6]"
esac
done
案例: Nginx启动脚本
启动 /usr/sbin/nginx
停止 /usr/sbin/nginx -s stop
重启 先停止后启动 /usr/sbin/nginx -s stop && /usr/sbin/nginx
重载 /usr/sbin/nginx -s reload
状态 自定义
[[email protected] ]# cat nginx.sh
#!/bin/bash
. /etc/init.d/functions
re=$1
fun(){
if [ $? -eq 0 ];then
action "Nginx $re is " /bin/true
else
action "Nginx $re is " /bin/false
fi
}
case $1 in
start)
/usr/sbin/nginx
fun
;;
stop)
/usr/sbin/nginx -s stop
fun
;;
restart)
/usr/sbin/nginx -s stop
sleep 1
/usr/sbin/nginx
fun
;;
reload)
/usr/sbin/nginx -s reload
fun
;;
status)
num=`ps axu|grep nginx|grep master|wc -l`
if [ $num -eq 1 ];then
action "Nginx is running...." /bin/true
else
action "Nginx is down......." /bin/false
fi
;;
*)
echo "Usage: $0 [start|stop|restart|reload|status]"
esac
边栏推荐
- [8.2] Code Source - [Currency System] [Coins] [New Year's Questions (Data Enhanced Edition)] [Three Stages]
- DNS被劫持如何处理?
- Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
- The production method of the powered small sailboat is simple, the production method of the electric small sailboat
- 七夕节赚徽章拉
- [8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
- How to identify false evidence and evidence?
- mutillidae下载及安装
- write the story about us
- dedecms报错The each() function is deprecated
猜你喜欢
Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit
iMedicalLIS监听程序(2)
开发属于自己的node包
Four-digit display header design
Hard power or soft power, which is more important to testers?
JeeSite新建报表
Feature preprocessing
工业级远距离无线传输装置的功能有哪些?
程序开发的一些常规套路(一)
随机推荐
Acid (ACID) Base (BASE) Principles for Database Design
UE4 opens door via interaction (keyboard key)
狗仔队:表面编辑多视点图像处理
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
说说数据治理中常见的20个问题
1007 Climb Stairs (greedy | C thinking)
【树莓派】树莓派调光
阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
[BSidesCF 2019]Kookie
关于#SQL#的迭代、父子结构查询问题,如何解决?
JeeSite新建报表
AUTOCAD - dimension association
markdown如何换行——md文件
[CISCN2019 华东南赛区]Web11
不看后悔,appium自动化环境完美搭建
How to wrap markdown - md file
关于sklearn库的安装
Qixi Festival earn badges
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
iMedicalLIS监听程序(2)