函数 function
函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程
它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一
部分
函数由两部分组成:函数名和函数体
帮助参看:help function
定义函数
#语法一:
func_name (){
...函数体...
}
#语法二:
function func_name {
...函数体...
}
#语法三:
function func_name () {
...函数体...
}
查看函数
#查看当前已定义的函数名
declare -F
#查看当前已定义的函数定义
declare -f
#查看指定当前已定义的函数名
declare -f func_name
#查看当前已定义的函数名定义
declare -F func_name
删除函数
格式:
unset func_name
函数调用
函数的调用方式
可在交互式环境下定义函数
可将函数放在脚本文件中作为它的一部分
可放在只包含函数的单独文件中
调用:函数只有被调用才会执行,通过给定函数名调用函数,函数名出现的地方,会被自动替换为函数
代码
函数的生命周期:被调用时创建,返回时终止
交互式环境下定义和使用函数
范例:
[root@centos8 ~]# dir() {
> ls -l
> }
[root@centos8 ~]# dir
total 4
-rw-------. 1 root root 1559 Nov 7 19:33 anaconda-ks.cfg
范例:实现判断CentOS的主版本
[root@centos8 ~]# centos_version() {
> sed -rn 's#^.* +([0-9]+)..*#1#p' /etc/redhat-release
> }
[root@centos8 ~]# centos_version
8
在脚本中定义及使用函数
函数在使用前必须定义,因此应将函数定义放在脚本开始部分,直至shell首次发现它后才能使用,调用
函数仅使用其函数名即可
[root@centos8 ~]# cat func1.sh
#!/bin/bash
#name:func1
hello(){
echo "Hello there today's date is `date +%F`"
}
echo "now going to the function hello"
hello
echo "back from the function"
[root@centos8 ~]#./func1.sh
now going to the function hello
Hello there today's date is 2019-12-18
back from the function
范例:
#!/bin/bash
# Date: 2021-06-10
# Author: lvxuan
disable_selinux(){
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "SElinux已禁用,重新启动后才可生效"
}
disable_firewall(){
systemctl disable --now firewalld &> /dev/null
echo "防火墙已禁用"
}
set_ps1() {
echo "PS1='[e[1;35m][u@h W]\$[e[0m]'" > /etc/profile.d/reset.sh
echo "提示符已修改成功,请重新登录生效"
}
set_eth(){
sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
echo "网络名称已修改成功,请重新启动才能生效"
}
PS3="请选择相应的编号(1-6): "
MENU='
禁用SELinux
关防火墙
修改提示符
修改网卡名
以上全实现
退出
'
select M in $MENU ;do
case $REPLY in
1)
disable_selinux
;;
2)
disable_firewall
;;
3)
set_ps1
;;
4)
set_eth
;;
5)
disable_selinux
disable_firewall
set_ps1
set_eth
;;
6)
break
;;
*)
echo "请输入正确的数字"
esac
done
使用函数文件
可以将经常使用的函数存入一个单独的函数文件,然后将函数文件载入shell,再进行调用函数
函数文件名可任意选取,但最好与相关任务有某种联系,例如:functions
一旦函数文件载入shell,就可以在命令行或脚本中调用函数。可以使用delcare -f 或set 命令查看所有
定义的函数,其输出列表包括已经载入shell的所有函数
若要改动函数,首先用unset命令从shell中删除函数。改动完毕后,再重新载入此文件
实现函数文件的过程:
- 创建函数文件,只存放函数的定义
- 在shell脚本或交互式shell中调用函数文件,格式如下:
. filename
source filename
范例:
[root@centos8 ~]# cat functions
#!/bin/bash
#functions
hello(){
echo Run hello Function
}
hello2(){
echo Run hello2 Function
}
[root@centos8 ~]#. functions
[root@centos8 ~]# hello
Run hello Function
[root@centos8 ~]# hello2
Run hello2 Function
[root@centos8 ~]# declare -f hello hello2
hello ()
{
echo Run hello Function
}
hello2 ()
{
echo Run hello2 Function
}
函数返回值
函数的执行结果返回值:
使用echo等命令进行输出
函数体中调用命令的输出结果
函数的退出状态码:
默认取决于函数中执行的最后一条命令的退出状态码
自定义退出状态码,其格式为:
return 从函数中返回,用最后状态命令决定返回值
return 0 无错误返回
return 1-255 有错误返回
环境函数
类拟于环境变量,也可以定义环境函数,使子进程也可使用父进程定义的函数
定义环境函数:
export -f function_name
declare -xf function_name
查看环境函数:
export -f
declare -xf
函数参数
函数可以接受参数:
传递参数给函数:在函数名后面以空白分隔给定参数列表即可,如:testfunc arg1 arg2 ...
在函数体中当中,可使用$1, $2, ...调用这些参数;还可以使用$@, $*, $#等特殊变量
范例:实现进度条功能
#!/bin/bash
# Date: 2021-06-10
# Author: lvxuan
function print_chars()
{
# 传入的第一个参数指定要打印的字符串
local char="$1"
# 传入的第二个参数指定要打印多少次指定的字符串
local number="$2"
local c
for ((c = 0; c < number; ++c)); do
printf "$char"
done
}
COLOR=32
declare -i end=50
for ((i = 1; i <= end; ++i)); do
printf "e[1;${COLOR}me[80D["
print_chars "#" $i
print_chars " " $((end - i))
printf "] %3d%%e[0m" $((i * 2))
sleep 0.1s
done
echo
函数变量
变量作用域:
普通变量:只在当前shell进程有效,为执行脚本会启动专用子shell进程;因此,本地变量的作用
范围是当前shell脚本程序文件,包括脚本中的函数
环境变量:当前shell和子shell有效
本地变量:函数的生命周期;函数结束时变量被自动销毁
注意:
如果函数中定义了普通变量,且名称和局部变量相同,则使用本地变量
由于普通变量和局部变量会冲突,建议在函数中只使用本地变量
在函数中定义本地变量的方法
local NAME=VALUE
函数递归
函数递归:函数直接或间接调用自身,注意递归层数,可能会陷入死循环
递归特点:
函数内部自已调用自已
必须有结束函数的出口语句,防止死循环
范例: 无出口的递归函数调用
[root@centos8 ~]# func () { echo $i;echo "run func";let i++; func; }
[root@centos8 ~]# func
递归示例:
阶乘是基斯顿·卡曼于 1808 年发明的运算符号,是数学术语
一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0和1的阶乘为1,自然数n的
阶乘写作n!
n!=1×2×3×...×n
阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n
n!=n(n-1)(n-2)...1
n(n-1)! = n(n-1)(n-2)!
#!/bin/bash
#
fact() {
if [ $1 -eq 0 -o $1 -eq 1 ]; then
echo 1
else
echo $[$1*$(fact $[$1-1])]
fi
}
fact $1
fork 炸弹是一种恶意程序,它的内部是一个不断在 fork 进程的无限循环,实质是一个简单的递归程
序。由于程序是递归的,如果没有任何限制,这会导致这个简单的程序迅速耗尽系统里面的所有资源
参考:https://en.wikipedia.org/wiki/Fork_bomb
函数实现
:(){ :|:& };:
bomb() { bomb | bomb & }; bomb
脚本实现
cat Bomb.sh
#!/bin/bash
./$0|./$0&
其它脚本相关工具
信号捕捉 trap
trap 命令可以捕捉信号,修改信号原来的功能,实现自定义功能
#进程收到系统发出的指定信号后,将执行自定义指令,而不会执行原操作
trap '触发指令' 信号
#忽略信号的操作
trap '' 信号
#恢复原信号的操作
trap '-' 信号
#列出自定义信号操作
trap -p
#当脚本退出时,执行finish函数
trap finish EXIT
范例:
#!/bin/bash
trap "echo 'Press ctrl+c or ctrl+ '" int quit
trap -p
for((i=0;i<=10;i++))
do
sleep 1
echo $i
done
trap '' int
trap -p
for((i=11;i<=20;i++))
do
sleep 1
echo $i
done
trap '-' int
trap -p
for((i=21;i<=30;i++))
do
sleep 1
echo $i
done
范例: 当脚本正常或异常退出时,也会执行finish函数
#!/bin/bash
# Date: 2021-06-10
# Author: lvxuan
finish(){
echo finish| tee -a /root/finish.log
}
trap finish exit
while true ;do
echo running
sleep 1
done
创建临时文件 mktemp
mktemp 命令用于创建并显示临时文件,可避免冲突
格式
mktemp [OPTION]... [TEMPLATE]
说明:TEMPLATE: filenameXXX,X至少要出现三个
常见选项:
-d #创建临时目录
-p DIR或--tmpdir=DIR #指明临时文件所存放目录位置
范例:
[root@centos8 ~]# mktemp
/tmp/tmp.UogGCumh8C
[root@centos8 ~]# mktemp /tmp/testXXX
[root@centos8 ~]# tmpdir=`mktemp -d /tmp/testdirXXX`
[root@centos8 ~]# mktemp --tmpdir=/testdir testXXXXXX
范例:实现文件垃圾箱
#!/bin/bash
# Date: 2021-06-10
# Author: lvxuan
DIR=`mktemp -d /tmp/trash-$(date +%F_%H-%M-%S)XXXXXX`
mv $* $DIR
echo $* is move to $DIR
[root@centos8 ~]# alias rm=/data/scripts/rm.sh
#方法2:函数实现
[root@centos8 ~]# function rm () { local trash=`mktemp -d /tmp/trashXXXX`;mv $* $trash; }
安装复制文件 install
install 功能相当于cp,chmod,chown,chgrp ,mkdir 等相关工具的集合
install命令格式:
install [OPTION]... [-T] SOURCE DEST 单文件
install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY... #创建空目录
选项:
-m MODE,默认755
-o OWNER
-g GROUP
-d DIRNAME 目录
范例:
[root@centos8 ~]# install -m 700 -o long -g admins srcfile desfile
[root@centos8 ~]# install -m 770 -d /testdir/installdir
[root@centos8 ~]# install -m 600 -o long -g bin anaconda-ks.cfg /data/a.cfg
[root@centos8 ~]# ll /data/a.cfg
-rw------- 1 long bin 1550 Jun 23 20:28 /data/a.cfg
[root@centos8 ~]# install -m 700 -o king -g daemon -d /data/testdir
[root@centos8 ~]# ll -d /data/testdir
drwx------ 2 king daemon 6 Apr 29 15:09 /data/testdir
交互式转化批处理工具 expect
expect 是由Don Libes基于 Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式
操作的场景,借助 expect 处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本
上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人
员的工作效率
范例: 安装expect 及mkpasswd 工具
[root@centos8 ~]# yum -y install expect
[root@centos8 ~]# mkpasswd
mghe5J&9A
[root@centos8 ~]# mkpasswd
XhyQ67uo=
[root@centos8 ~]# mkpasswd -l 15 -d 3 -C 5
9T{htJmcA7pgCJ2
expect 语法:
expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
常见选项:
-c:从命令行执行expect脚本,默认expect是交互地执行的
-d:可以调试信息
示例:
expect -c 'expect "
" {send "pressed enter
"}'
expect -d ssh.exp
expect中相关命令
spawn 启动新的进程
expect 从进程接收字符串
send 用于向进程发送字符串
interact 允许用户交互
exp_continue 匹配多个字符串在执行动作后加此命令
expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
[root@centos8 test]# expect
expect1.1> expect "hi" {send "You said hi
"}
hahahixixi
You said hi
匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:
[root@centos8 ~]# expect
expect1.1> expect "hi" { send "You said hi
" } "hehe" { send "Hehe yourself
"
} "bye" { send "Good bye
" }
hehe
Hehe yourself
expect1.2> expect "hi" { send "You said hi
" } "hehe" { send "Hehe yourself
"
} "bye" { send "Good bye
" }
bye
Good bye
expect1.3> expect "hi" { send "You said hi
" } "hehe" { send "Hehe yourself
"
} "bye" { send "Good bye
" }
hi
You said hi
expect1.4>
匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:
expect {
"hi" { send "You said hi
"}
"hehe" { send "Hehe yourself
"}
"bye" { send " Good bye
"}
}
[root@centos8 ~]# expect
expect1.1> expect {
+> "hi" { send "You said hi
"}
+> "hehe" { send "Hehe yourself
"}
+> "bye" { send " Good bye
"}
+> }
bye
Good bye
expect1.2>
范例1:非交互式复制文件
#!/usr/bin/expect
spawn scp /etc/redhat-release 10.0.0.7:/data
expect {
"yes/no" { send "yes
";exp_continue }
"password" { send "centos
" }
}
expect eof
范例2:自动登录
#!/usr/bin/expect
spawn ssh 10.0.0.7
expect {
"yes/no" { send "yes
";exp_continue }
"password" { send "centos
" }
}
interact
范例3:expect 变量
#!/usr/bin/expect
set ip 10.0.0.7
set user root
set password centos
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes
";exp_continue }
"password" { send "$password
" }
}
interact
范例4:expect 位置参数
[root@centos8 ~]# cat expect4
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" { send "yes
";exp_continue }
"password" { send "$password
" }
}
interact
[root@centos8 ~]#./expect4 10.0.0.7 root centos
spawn ssh root@10.0.0.7
root@10.0.0.7's password:
Last login: Wed Apr 29 15:34:14 2020 from 10.0.0.8
[root@centos7 ~]#exit
logout
Connection to 10.0.0.7 closed.
范例5:expect 执行多个命令
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes
";exp_continue }
"password" { send "$password
" }
}
expect "]#" { send "useradd haha
" }
expect "]#" { send "echo centos |passwd --stdin haha
" }
send "exit
"
expect eof
#执行
#./ssh4.exp 10.0.0.7 root centos
范例6:shell脚本调用 expect
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" { send "yes
";exp_continue }
"password" { send "$password
" }
}
expect "]#" { send "useradd hehe
" }
expect "]#" { send "echo centos |passwd --stdin hehe
" }
expect "]#" { send "exit
" }
expect eof
EOF
#执行
#./ssh5.sh 192.168.8.10 root centos