函数
什么是函数
函数其实就是一堆命令的集合,用来完成一些特定的代码块
作用:
便于代码的复用
跟脚本类似
函数的基本概述
函数名() {
命令集合
}
function 函数名 {
命令集合
}
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
456
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123 456
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
123 456
[root@shell /scripts/shell11]
123 456 789
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
123 456
[root@shell /scripts/shell11]
#!/bin/bash
fun_1() {
echo "$1"
}
fun_1 123
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
#!/bin/bash
fun_1() {
echo "$Num"
}
Num=123
fun_1
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
#!/bin/bash
fun_1() {
echo "$1"
}
fun_1 $1
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
#!/bin/bash
fun_1() {
echo "$1"
}
Num=$1
fun_1
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
#!/bin/bash
fun_1() {
echo "$Num"
}
Num=$1
fun_1
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123
[root@shell /scripts/shell11]
#!/bin/bash
fun_2() {
echo "$1"
}
fun_2 $1
fun_2 $2
fun_2 $3
[root@shell /scripts/shell11]
123
456
789
[root@shell /scripts/shell11]
#!/bin/bash
fun_2() {
echo "$1"
}
fun_2 $1 $2 $3
fun_2 $3
fun_2 $2
[root@shell /scripts/shell11]
123
789
456
[root@shell /scripts/shell11]
#!/bin/bash
fun_3() {
echo "$1 $2 $3"
}
fun_3 123 456 789
[root@shell /scripts/shell11]
123 456 789
[root@shell /scripts/shell11]
#!/bin/bash
fun_3() {
echo "$1 $2 $3"
}
Num=$(fun_3 123 456 789)
echo $Num
[root@shell /scripts/shell11]
123 456 789
[root@shell /scripts/shell11]
#!/bin/bash
fun_3() {
echo "$1 $2 $3"
}
Num=$(fun_3 $1 $2 $3)
echo $Num
[root@shell /scripts/shell11]
[root@shell /scripts/shell11]
123 456 789
1. 函数场景示例
写一个脚本,实现简单的计算器功能,能够实现加减乘除四种计算
执行脚本
sh script.sh 1 + 1
1 + 1 = 2
sh script.sh 1 - 1
1 - 1 = 0
[root@shell /scripts/shell11]
#!/bin/bash
cale() {
echo $(( $1 $2 $3 ))
}
if [ $# -ne 3 ];then
echo "你的传参不足3个!你可以这么写:1 + 1"
exit
fi
echo "$1 $2 $3 = $(cale $1 "$2" $3)"
[root@shell /scripts/shell11]
#!/bin/bash
cale() {
case $2 in
+|-|/)
echo $1 $2 $3 = $(( $1 $2 $3 ))
;;
x)
echo $1 $2 $3 = $(( $1 * $3 ))
;;
*)
echo "你输入的不是一个计算操作符!"
exit
esac
}
if [ $# -ne 3 ];then
echo "你的传参不足3个!你可以这么写:1 + 1"
exit
fi
cale $1 $2 $3
[root@shell /scripts/shell11]
你的传参不足3个!你可以这么写:1 + 1
[root@shell /scripts/shell11]
1 + 1 = 2
[root@shell /scripts/shell11]
1 - 1 = 0
[root@shell /scripts/shell11]
1 / 1 = 1
[root@shell /scripts/shell11]
你的传参不足3个!你可以这么写:1 + 1
[root@shell /scripts/shell11]
你输入的不是一个计算操作符!
[root@shell /scripts/shell11]
1 x 1 = 1
[root@shell /scripts/shell11]
1 x 4 = 4
2. Nginx启动停止脚本,使用函数
[root@shell /scripts/shell11]
#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
if [ $USER != "root" -a $UID -ne 0 ];then
echo "当前用户${USER}对此脚本${0}没有执行权限!"
exit
fi
State=$1
if [ $# -ne 1 ];then
echo "Usage: $0 {start|stop|status|restart|reload}"
exit
fi
Suo_file=/tmp/nginx.lock
if [ -f $Suo_file ];then
echo "此脚本正在执行中!请稍后再运行!"
exit
fi
Tong() {
if [ $? -eq 0 ];then
action "Nginx Server $State Successfully." /bin/true
else
action "Nginx Server $State Failed!" /bin/false
fi
}
Start() {
if [ -f /var/run/nginx.pid ];then
action "Nginx Server is running........." /bin/true
else
/usr/sbin/nginx &>/dev/null && sleep 3
Tong
fi
}
Stop() {
if [ -f /var/run/nginx.pid ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 3
Tong
else
action "Nginx Server is stop..........." /bin/true
fi
}
Status() {
if [ -f /var/run/nginx.pid ];then
action "Nginx Server is running........." /bin/true
else
action "Nginx Server is stop..........." /bin/true
fi
}
Reload() {
if [ -f /var/run/nginx.pid ];then
/usr/sbin/nginx -s reload &>/dev/null && sleep 3
Tong
else
action "Nginx Server not running,Unable to proceed reload!" /bin/false
fi
}
Restart() {
State=stop
Stop
State=start
Start
}
case $State in
start)
Start
;;
stop)
Stop
;;
status)
Status
;;
reload)
Reload
;;
restart)
Restart
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload}"
esac
rm -f $Suo_file
1.1 写一个操作系统初始优化脚本
见附件
2. 函数状态返回
返回值在shell中有两种方式
echo
return
[root@shell /scripts/shell12]
#!/bin/bash
fun() {
echo "123"
return 1
}
Test=$(fun)
echo "函数的状态返回码为:$?"
echo "函数的返回数据为:$Test"
[root@shell /scripts/shell12]
函数的状态返回码为:1
函数的返回数据为:123
[root@shell /scripts/shell12]
#!/bin/bash
File() {
if [ -f /etc/hosts ];then
return 0
else
return 1
fi
}
File && echo "文件存在!" || echo "文件不存在!"
[root@shell /scripts/shell12]
文件存在!
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
#!/bin/bash
File() {
if [ -f /etc/hosts ];then
return 1
else
return 0
fi
}
File && echo "文件存在!" || echo "文件不存在!"
[root@shell /scripts/shell12]
文件不存在!
3. 状态返回案例
猜数字的游戏
1. 用户输入的数值大于等于0且小于10 返回状态码为0
2. 用户输入的数值大于等于10且小于20 返回状态码为1
3. 用户输入的数值大于等于20且小于30 返回状态码为2
4. 输入的其他数值 返回状态码为3
[root@shell /scripts/shell12]
#!/bin/bash
check_num() {
read -p "请输入一个数字:" Num
if [ $Num -ge 0 -a $Num -lt 10 ];then
return 0
elif [ $Num -ge 10 -a $Num -lt 20 ];then
return 1
elif [ $Num -ge 20 -a $Num -lt 30 ];then
return 2
else
return 3
fi
}
check_num
Rc=$?
if [ $Rc -eq 0 ];then
echo "你输入的数字大于等于0且小于10."
elif [ $Rc -eq 1 ];then
echo "你输入的数字大于等于10且小于20."
elif [ $Rc -eq 2 ];then
echo "你输入的数字大于等于20且小于30."
else
echo "你输入的数值超出范围!请重新输入!"
echo $Rc
fi
[root@shell /scripts/shell12]
请输入一个数字:0
你输入的数字大于等于0且小于10.
[root@shell /scripts/shell12]
请输入一个数字:10
你输入的数字大于等于10且小于20.
[root@shell /scripts/shell12]
请输入一个数字:20
你输入的数字大于等于20且小于30.
[root@shell /scripts/shell12]
请输入一个数字:30
你输入的数值超出范围!请重新输入!
3
[root@shell /scripts/shell12]
请输入一个数字:40
你输入的数值超出范围!请重新输入!
3
[root@shell /scripts/shell12]
请输入一个数字:a
return-3.sh: line 4: [: a: integer expression expected
return-3.sh: line 6: [: a: integer expression expected
return-3.sh: line 8: [: a: integer expression expected
你输入的数值超出范围!请重新输入!
3