Shell-day11
1. 上周回顾
1. Shell编程介绍
2. 什么是Shell,为什么学习Shell,任何学习好Shell,Shell能干什么
3. Shell的开发规范习惯,Shell的执行方式
4. 登录式Shell和非登录式Shell 交互式 非交互式
5. Shell的变量介绍
6. 用户自定义变量 环境变量 位置变量 预定义变量
7. 变量的定义实战
8. Shell变量的read复制
9. Shell的变量替换
10. Shell的变量运算
11. 流程控制语句介绍
12. 流程控制语句之文件比较
13. 流程控制语句之整数比较
14. 函数库的应用
15. 流程控制语句之字符比较
16. 流程控制语句之多条件比较
17. 流程控制语句之正则比较
18. 流程控制语句之case语句
19. 循环语句之For循环
20. For循环取值方式
21. 循环语句之while循环
22. 循环语句之until循环
23. 跳出循环的内置指令
2. Shell函数基本介绍
什么是函数?
函数其实就是一堆命令的集合,用来完成特定的功能代码块,方便后期调用。
函数的作用:
函数可以将代码进行模块化,便于后期的代码的复用,增加脚本的可读性
函数和变量类似,先定义,后引用,直接写函数名就能直接引用 不引用就不会执行函数
3. 函数的语法示例
函数名() {
command1
command2
.....
}
变量名=值 command
function 函数名 {
command1
command2
........
}
[root@qls ~]
[root@qls ~]
hello
[root@qls ~]
[root@qls ~]
hello
root
[root@qls ~]
[root@qls ~]
test
[root@qls ~]
[root@qls ~]
Test
0
[root@qls /service/scripts/day11]
fun1() {
echo "Test"
}
fun1
[root@qls /service/scripts/day11]
Test
[root@qls /service/scripts/day11]
function fun1 {
echo "Test"
}
fun1
[root@qls /service/scripts/day11]
Test
4. 函数的参数传递
函数参数传递跟脚本类似 都是使用$1 $2 .. $9 ....
[root@qls /service/scripts/day11]
fun1() {
echo "$1"
}
fun1 $1
[root@qls /service/scripts/day11]
test
[root@qls /service/scripts/day11]
fun1() {
echo "$1"
}
fun1 hello
[root@qls /service/scripts/day11]
hello
[root@qls /service/scripts/day11]
fun1() {
echo "$Num"
}
Num=1
fun1
[root@qls /service/scripts/day11]
1
[root@qls /service/scripts/day11]
fun1() {
local Num=1
echo "$Num"
}
fun1
echo "变量Num值为: $Num"
[root@qls /service/scripts/day11]
1
变量Num值为:
[root@qls /service/scripts/day11]
fun1() {
echo "$Num"
}
Num=$1
fun1
[root@qls /service/scripts/day11]
test
[root@qls /service/scripts/day11]
fun1() {
echo "$1"
}
fun1 $1
fun1 $2
fun1 $3
[root@qls /service/scripts/day11]
123
456
789
[root@qls /service/scripts/day11]
fun1() {
echo "$1"
}
fun1 $1 $2 $3
fun1 $3
fun1 $2
[root@qls /service/scripts/day11]
123
789
456
[root@qls /service/scripts/day11]
fun1() {
echo "$1 $2 $3"
}
Test=$(fun1 $1 $2 $3)
echo $Test
[root@qls /service/scripts/day11]
123 456 789
[root@qls /service/scripts/day11]
fun1() {
echo "$*"
}
Test=$(fun1 $*)
echo $Test
[root@qls /service/scripts/day11]
123 456 789
[root@qls /service/scripts/day11]
123 456 789 abc cba
[root@qls /service/scripts/day11]
函数的位置变量跟脚本的位置变量作用是类似的,但是函数的位置变量不等于脚本的位置变量,脚本的位置变量不等于函数的位置变量,两者不能混为一谈。
[root@qls /service/scripts/day11]
fun1() {
Test=hello
echo "$Test"
}
fun1
echo "Test变量值为:$Test"
[root@qls /service/scripts/day11]
hello
Test变量值为:hello
[root@qls /service/scripts/day11]
[root@qls /service/scripts/day11]
fun1() {
local Test=hello
echo "$Test"
}
fun1
echo "Test变量值为:$Test"
[root@qls /service/scripts/day11]
hello
Test变量值为:
[root@qls /service/scripts/day11]
fun1() {
Test=hello
echo "$Test"
}
echo "Test变量值为:$Test"
[root@qls /service/scripts/day11]
Test变量值为:
函数中调用局部变量时,一定要加上local 使用全局变量时,将变量定义在脚本中,而不是函数中,变量一定要定义在调用函数的前面。
[root@qls /service/scripts/day11]
fun1() {
echo "$Test"
}
fun1
Test=hell0
echo "Test变量值为:$Test"
[root@qls /service/scripts/day11]
Test变量值为:hell0
5. 函数参数传递案例
执行脚本时,需要在脚本后面加上计算的数字及操作符
sh scripts 1 + 1
执行结果
1 + 1 = 2
sh scripts 1 - 1
1 - 1 = 0
[root@qls /service/scripts/day11]
Cale() {
awk "BEGIN{print $1 $2 $3}"
}
if [ $# -ne 3 ];then
echo "你输入的参数不等于3个!请重新输入!例:1 + 1"
exit
fi
echo "$1 $2 $3 = $(Cale $1 "$2" $3)"
[root@qls /service/scripts/day11]
你输入的参数不等于3个!请重新输入!例:1 + 1
[root@qls /service/scripts/day11]
你输入的参数不等于3个!请重新输入!例:1 + 1
[root@qls /service/scripts/day11]
1 * 1 = 1
[root@qls /service/scripts/day11]
1 * 5 = 5
[root@qls /service/scripts/day11]
1 / 5 = 0.2
[root@qls /service/scripts/day11]
1 ^ 5 = 1
[root@qls /service/scripts/day11]
2 ^ 5 = 32
[root@qls /service/scripts/day11]
2 % 5 = 2
[root@qls /service/scripts/day11]
9 % 5 = 4
[root@qls /service/scripts/day11]
Cale() {
case $2 in
+|-|/|%|^)
awk "BEGIN{print $1 $2 $3}"
;;
x)
awk "BEGIN{print $1 * $3}"
;;
*)
echo "你输入的第二个位置参数不是一个操作符!请重新输入"
exit
esac
}
if [ $# -ne 3 ];then
echo "你输入的参数不等于3个!请重新输入!例:1 + 1"
exit
fi
echo "$1 $2 $3 = $(Cale $1 $2 $3)"
[root@qls /service/scripts/day11]
你输入的参数不等于3个!请重新输入!例:1 + 1
[root@qls /service/scripts/day11]
1 + 1 = 2
[root@qls /service/scripts/day11]
1 - 1 = 0
[root@qls /service/scripts/day11]
1 / 1 = 1
[root@qls /service/scripts/day11]
1 % 1 = 0
[root@qls /service/scripts/day11]
2 ^ 1 = 2
[root@qls /service/scripts/day11]
你输入的参数不等于3个!请重新输入!例:1 + 1
[root@qls /service/scripts/day11]
2 * 1 = 你输入的第二个位置参数不是一个操作符!请重新输入
[root@qls /service/scripts/day11]
2 x 1 = 2
[root@qls /service/scripts/day11]
2 x 4 = 8
6. 使用函数编写Nginx启动脚本
/usr/sbin/nginx
[root@qls /service/scripts/day11]
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
if [ $USER != "root" -o $UID -ne 0 ];then
echo "当前用户${USER}对此脚本${0}没有权限执行!"
exit
fi
if [ $# -ne 1 ];then
echo "Usage: $0 {start|stop|status|restart|reload}"
exit
fi
Suo=/tmp/nginx.lock
if [ -f $Suo ];then
echo "此脚本${0}正在执行中!请稍后再执行....."
exit
fi
touch $Suo &>/dev/null
State=$1
Pid_File=/var/run/nginx.pid
Return() {
if [ $? -eq 0 ];then
action "Nginx Service $State Successfully!" /bin/true
else
action "Nginx Service $State Failed!" /bin/false
fi
}
Syntax_Err() {
local Err_Log=/tmp/nginx_err.log
/usr/sbin/nginx -t &>$Err_Log
Err_File=$(awk -F '[ :]' 'NR==1{print $(NF-1)}' $Err_Log)
Err_Line=$(awk -F '[ :]' 'NR==1{print $NF}' $Err_Log)
/usr/sbin/nginx -t
echo "Nginx Syntax Check Failed!Error File is $Err_File,Error Line is $Err_Line"
read -p "请问你是否选择进行修改配置文件[Yes|No]: " Confirm
case $Confirm in
y|Y|Yes|yes)
vim +$Err_Line $Err_File
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
State_1() {
/usr/sbin/nginx &>/dev/null && sleep 3
}
State_2() {
/usr/sbin/nginx -s reload &>/dev/null && sleep 3
}
if [ $State == "start" ];then
State_1
Return
fi
if [ $State == "reload" ];then
State_2
Return
fi
if [ $State == "stop" -o $State == "restart" ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 3
Return
fi
else
action "Nginx Service Syntax Error!" /bin/false
fi
;;
n|N|No|NO)
echo "你选择不进行修改配置文件!"
;;
*)
echo "你输入的不符合要求!"
esac
}
Start() {
if [ -f $Pid_File ];then
action "Nginx Service is Running....." /bin/true
else
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
/usr/sbin/nginx &>/dev/null && sleep 3
Return
else
action "Nginx Service Syntax Error!" /bin/false
Syntax_Err
fi
fi
}
Stop() {
if [ -f $Pid_File ];then
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 3
Return
else
Syntax_Err
fi
else
action "Nginx Service is Not Running....." /bin/true
fi
}
Status() {
if [ -f $Pid_File ];then
action "Nginx Service is Running....." /bin/true
else
action "Nginx Service is Not Running....." /bin/true
fi
}
Restart() {
if [ -f $Pid_File ];then
local State=stop
Stop
local State=start
Start
else
action "Nginx Service is Not Running....." /bin/true
Start
fi
}
Reload() {
if [ -f $Pid_File ];then
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
/usr/sbin/nginx -s reload &>/dev/null && sleep 3
Return
else
Syntax_Err
fi
else
action "Nginx Service is Not Running!Unable to Reload!" /bin/false
fi
}
case $1 in
start)
Start
;;
stop)
Stop
;;
status)
Status
;;
restart)
Restart
;;
reload)
Reload
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload}"
esac
rm -f $Suo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现