You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
4.9 KiB

<h1><center>shell函数</center></h1>
作者:行癫(盗版必究)
------
## 一:函数
#### 1.函数介绍
Shell 函数的本质是一段可以重复使用的脚本代码,这段代码被提前编写好了,放在了指定的位置,使用时直接调取即可
#### 2.语法格式
```shell
function name() {
statements
[return value]
}
```
function是 Shell 中的关键字,专门用来定义函数(可以省略)
name是函数名
statements是函数要执行的代码也就是一组命令
return value表示函数的返回值其中 return 是 Shell 关键字,专门用在函数中返回一个值(可以省略)
#### 3.函数定义
```shell
myfunc(){
echo "This is a new function"
}
```
#### 4.函数调用
直接用函数名字调用函数
#### 5.函数传参
```shell
[root@xingdiancloud ~]# cat hello.sh
#!/bin/bash
hello(){
echo $1
}
hello xingdian
[root@xingdiancloud ~]# bash hello.sh
xingdian
```
#### 6.函数变量
```shell
[root@xingdiancloud ~]# cat hello.sh
#!/bin/bash
i=0
echo "$a"
hello(){
a=1
local d=3
echo "$i $a $b $c $d"
}
hello
b=2
echo "$a $d"
[root@xingdiancloud ~]# bash hello.sh
0 1 3
1
```
注意:
默认,函数里的变量会在函数外面生效
注意脚本中内容按上下文顺序执行
local定义的变量只在函数内生效
#### 7.调用函数
创建功能函数
```shell
[root@xingdiancloud ~]# cat hello.sh
#!/bin/bash
hello(){
echo "This is one"
}
```
另一个脚本调用该脚本中函数
```shell
[root@xingdiancloud ~]# cat xingdian.sh
#!/bin/bash
source ./hello.sh
hello
[root@xingdiancloud ~]# bash xingdian.sh
This is one
```
#### 8.函数案例
```shell
#!/bin/bash
#v1.24.5.27.1
#作者:行癫
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
```