287 lines
6.9 KiB
Markdown
287 lines
6.9 KiB
Markdown
<h2><center>Shell 函数</center></h2>
|
||
|
||
------
|
||
|
||
## 一:函数
|
||
|
||
### 1. 介绍
|
||
|
||
Shell 函数的本质是一段可以重复使用的脚本代码,这段代码被提前编写好了,放在了指定的位置,使用时直接调取即可。
|
||
|
||
### 2. 定义函数
|
||
|
||
可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
|
||
|
||
```shell
|
||
function name() {
|
||
# 函数体
|
||
commands
|
||
}
|
||
```
|
||
|
||
- function 是 Shell 中的关键字,专门用来定义函数;
|
||
- name 是函数名;
|
||
- commands 是函数要执行的代码,也就是一组语句;
|
||
- return value 表示函数的返回值,其中 return 是 Shell 关键字,专门用在函数中返回一个值;这一部分可以写也可以不写。
|
||
|
||
- 由 { } 包围的部分称为函数体,调用一个函数,实际上就是执行函数体中的代码。
|
||
- 函数的优势
|
||
1. 方便n次使用,减少代码量,使之方便,整洁。
|
||
2. 当需要修改里面的重复代码时,只需要修改一次函数即可实现需求;
|
||
3. 将函数写进文件,需要时直接通过文件调用
|
||
|
||
### 3. 调用函数
|
||
|
||
**执行不带参数的函数**
|
||
|
||
直接输入函数名即可,不需要带括号
|
||
|
||
```shell
|
||
functionName
|
||
```
|
||
|
||
- 执行函数时,函数名前的关键字`function`和函数名后面的`()`均不需要带
|
||
- 函数的定义必须要在执行的程序前定义或加载
|
||
|
||
**执行带参数的函数**
|
||
|
||
```shell
|
||
greet() {
|
||
echo "Hello, $1! You have $# arguments."
|
||
}
|
||
|
||
greet "Alice" 42 # 输出:Hello, Alice! You have 2 arguments.
|
||
```
|
||
|
||
- 向函数传递参数时,参数通过位置变量 `$1`, `$2`, `$3`... 接收。
|
||
- `$@` 表示所有参数,`$#` 表示参数个数。
|
||
|
||
**调用函数示例**
|
||
|
||
```bash
|
||
[root@wxin ~]# vim testfunction.sh
|
||
#!/bin/bash
|
||
# first function
|
||
function HelloWorld() {
|
||
echo "Hello world"
|
||
}
|
||
# second function
|
||
Welcome() {
|
||
echo "Welcome to qfedu"
|
||
}
|
||
# third function
|
||
function HelloShell {
|
||
echo "Hello Shell"
|
||
}
|
||
# file functions
|
||
HelloWorld # 调用函数
|
||
Welcome
|
||
HelloShell
|
||
|
||
[root@wxin ~]# bash testfunction.sh
|
||
Hello world
|
||
Welcome to qfedu
|
||
Hello Shell
|
||
```
|
||
|
||
**从文件中调用函数示例**
|
||
|
||
```bash
|
||
[root@wxin ~]# vim filefunction.sh
|
||
function Sum () {
|
||
for((i=1;i<=100;i++))
|
||
do
|
||
((sum=sum+i))
|
||
done
|
||
echo '{1..100} sum is :' $sum
|
||
}
|
||
|
||
|
||
[root@wxin ~]# vim filefunctionfromfile.sh
|
||
#!/bin/bash
|
||
path="/root/Test/filefunction.sh"
|
||
if [ -f ${path} ]
|
||
then
|
||
source $path # 加载函数
|
||
Sum # 调用函数
|
||
else
|
||
echo "file not exist or error"
|
||
fi
|
||
|
||
|
||
[root@wxin ~]# bash filefunctionfromfile.sh
|
||
{1..100} sum is : 5050
|
||
```
|
||
|
||
**函数参数传递示例**
|
||
|
||
```bash
|
||
[root@wxin ~]# vim functionwithargs.sh
|
||
#!/bin/bash
|
||
function Add () { # 定义函数
|
||
((sum=$1+$2))
|
||
echo "$1 + $2 sum is" ${sum}
|
||
}
|
||
Add $1 $2 # 调用函数并传递参数
|
||
|
||
[root@wxin ~]# bash functionwithargs.sh 100 150
|
||
100 + 150 sum is 250
|
||
[root@wxin ~]# bash functionwithargs.sh 509 150
|
||
509 + 150 sum is 659
|
||
```
|
||
|
||
### 4. 返回值
|
||
|
||
- Shell 函数通过 `return` 返回**整数状态码**(0 表示成功,非 0 表示失败)。
|
||
- 若要返回字符串或复杂数据,可使用 `echo` 输出到标准输出,再通过命令替换捕获。
|
||
|
||
```bash
|
||
add() {
|
||
local sum=$(( $1 + $2 ))
|
||
echo $sum # 输出结果到标准输出
|
||
}
|
||
|
||
result=$(add 3 5) # 捕获返回值
|
||
echo "Sum is $result" # 输出:Sum is 8
|
||
```
|
||
|
||
### 5. 变量作用域
|
||
|
||
- 默认情况下,函数内变量是**全局的**(会影响到脚本其他部分)。
|
||
- 使用 `local` 关键字定义**局部变量**(仅在函数内有效)。
|
||
|
||
```bash
|
||
test_scope() {
|
||
local var_local="I'm local"
|
||
var_global="I'm global"
|
||
}
|
||
|
||
test_scope
|
||
echo $var_global # 输出:I'm global
|
||
echo $var_local # 输出为空(局部变量不可见)
|
||
```
|
||
|
||
### 6. 函数案例
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
|
||
list()
|
||
{
|
||
echo "+++++++++++++++++++++++++++++++++"
|
||
echo "+++++++ 百宝箱 ++++++++"
|
||
echo "+++++++++++++++++++++++++++++++++"
|
||
echo "|||||||||||||||||||||||||||||||||"
|
||
echo "================================="
|
||
echo "= 1.yum仓库初始化 ="
|
||
echo "= 2.上课笔记工具安装 ="
|
||
echo "= 3.kvm虚拟机安装 ="
|
||
echo "= 4.vmware虚拟机安装 ="
|
||
echo "= 5.vs code安装 ="
|
||
echo "= 6.google浏览器安装 ="
|
||
echo "= 7.vnc-server的安装 ="
|
||
echo "= 8.一键安装所有 ="
|
||
echo "= 9.退出 ="
|
||
echo "================================="
|
||
}
|
||
yum-install(){
|
||
echo "====正在执行yum初始化操作请耐心等待!===="
|
||
rm -rf /etc/yum.repos.d/*
|
||
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo > /dev/null
|
||
yum -y install wget > /dev/null
|
||
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
|
||
echo "====正在清空缓存,请耐心等待!===="
|
||
yum clean all
|
||
echo "====正在重新加载,请耐心等待!===="
|
||
yum makecache
|
||
echo "====successed===="
|
||
}
|
||
|
||
cherrytree(){
|
||
echo "====正在进行安装......====="
|
||
yum -y install cherrytree > /dev/null
|
||
if [ $? -eq 0 ];then
|
||
echo "====successed===="
|
||
else
|
||
echo "==== failed ===="
|
||
echo "====检查网络和yum仓库===="
|
||
exit
|
||
fi
|
||
}
|
||
|
||
kvm-install(){
|
||
echo "====正在安装kvm虚拟机===="
|
||
yum -y install libvirt* virt-manager >> /dev/null && yum -y groupinstall 'Virtualization Host' >> /dev/null
|
||
echo "==== successed ===="
|
||
}
|
||
|
||
vmware-install(){
|
||
echo "====请将vmware安装包放到当前目录下===="
|
||
chmod +x
|
||
echo "......."
|
||
echo "..........."
|
||
echo ".................100%"
|
||
|
||
}
|
||
|
||
data=`date | awk '{print $4}'`
|
||
read -p "当前时间为${data},你是否要进行电脑初始化,继续请按回车(已记录你的初始时间):"
|
||
|
||
ping -c1 www.baidu.com 1> /dev/null
|
||
|
||
if [ $? -eq 0 ];then
|
||
echo "网络状况良好,请继续~"
|
||
else
|
||
echo "网络状况不佳,检查网络~"
|
||
exit
|
||
fi
|
||
|
||
echo "xingdian" > user.txt
|
||
echo "dianye" > password.txt
|
||
|
||
read -p "欢迎使用行癫工具箱,进行安装部署操作:"
|
||
|
||
read -p "请输入用户名:" name
|
||
|
||
username=`cat user.txt | awk '{print $1}'`
|
||
passwd=`cat password.txt | awk '{print $1}'`
|
||
|
||
if [ "${name}" == "${username}" ];then
|
||
read -p "请输入密码:" password
|
||
if [ "${password}" == "${passwd}" ];then
|
||
echo "登陆成功,进入工具箱"
|
||
while :
|
||
do
|
||
list
|
||
read -p "请选择你要使用的工具代码:" num
|
||
case $num in
|
||
1)
|
||
yum-install
|
||
sleep 3
|
||
;;
|
||
2)
|
||
cherrytree
|
||
sleep 3
|
||
;;
|
||
3)
|
||
;;
|
||
4)
|
||
;;
|
||
9)
|
||
break
|
||
;;
|
||
esac
|
||
done
|
||
data2=`date | awk '{print $4}'`
|
||
echo "结束时间为${data2};感谢您的使用!"
|
||
else
|
||
echo "用户名密码错误,请重新执行脚本!"
|
||
exit
|
||
fi
|
||
else
|
||
echo "用户名输入错误,请重新输入!"
|
||
exit
|
||
fis
|
||
```
|
||
|