14.Shell脚本编程进阶之函数。
1.编写函数,实现OS的版本判断。
[root@localhost /data/scripts]#bash os.sh
os version is 7
[root@localhost /data/scripts]#cat os.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-12
#FileName: os.sh
#********************************************************************
os () {
osnum=`sed -r 's/.* ([0-9])\..*/\1/' /etc/redhat-release`
echo "$osnum"
}
if [ `os` -eq 6 ];then
echo "os version is 6"
elif [ `os` -eq 7 ];then
echo "os version is 7"
elif [ `os` -eq 8 ];then
echo "os version is 8"
fi
2.编写函数,实现取出当前系统eth0的IP地址。
[root@localhost /data/scripts]#bash eth0_ip.sh
ipaddr=10.50.100.12/8
[root@localhost /data/scripts]#cat !*
cat eth0_ip.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-12
#FileName: eth0_ip.sh
#********************************************************************
eth0_ip () {
ip=`ip add show eth0 |grep "inet"|tr -s ' '|cut -d ' ' -f3|head -1`
echo ipaddr=$ip
}
eth0_ip
3.编写函数,实现打印绿色OK和红色FAILED。
[root@localhost /data/scripts]#bash !*
bash ok_failed.sh
success! [ OK ]
failed! [FAILED]
[root@localhost /data/scripts]#cat ok_failed.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-12
#FileName: ok_failed.sh
#********************************************************************
. /etc/init.d/functions
action "success!" true
action "failed!" false
4.编写函数,实现判断是否无位置参数,如无参数,提示错误。
[root@localhost /data/scripts]#bash parameter.sh
fail...you must input a parameter [FAILED]
[root@localhost /data/scripts]#cat !*
cat parameter.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-12
#FileName: parameter.sh
#********************************************************************
args () {
if [ $# -eq 0 ];then
. /etc/init.d/functions
action "fail...you must input a parameter" false
fi
}
args $1
5.编写函数,实现两个数字做为参数,返回最大值。
[root@localhost /data/scripts]#bash !*
bash twonmu.sh
you must input two number
[root@localhost /data/scripts]#bash twonmu.sh 100 10
100
[root@localhost /data/scripts]#cat twonmu.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-12
#FileName: twonmu.sh
#********************************************************************
>/app/f1
max () {
[ $# -ne 2 ] && echo "you must input two number"&&exit 10
until [ $# -eq 0 ];do
echo $1 >>/app/f1
shift
done
sort -nr /app/f1|head -n1
}
max $*
6.编写服务脚本/root/bin/testsrv.sh,完成如下要求
(1) 脚本可接受参数:start, stop, restart, status
(2) 如果参数非此四者之一,提示使用格式后报错退出
(3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功”考虑:如果事先已经启动过一次,该如何处理?
(4) 如是stop:则删除/var/lock/subsys/SCRIPT_NAME, 并显示“停止完成”考虑:如果事先已然停止过了,该如何处理?
(5) 如是restart,则先stop, 再start考虑:如果本来没有start,如何处理?
(6) 如是status, 则如果/var/lock/subsys/SCRIPT_NAME文件存在,则显示“SCRIPT_NAME isrunning...”,如果/var/lock/subsys/SCRIPT_NAME文件不存在,则显示“SCRIPT_NAME is stopped...”
(7)在所有模式下禁止启动该服务,可用chkconfig 和 service命令管理说明:SCRIPT_NAME为当前脚本名
[root@localhost ~/bin]#cat testsrv.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: testsrv.sh
#********************************************************************
#chkconfig:-96 07
#description
[ -f /etc/rc.d/init.d/`basename $0` ]||mv /app/script/`basename $0` /etc/rc.d/init.d/`basename $0`&>/dev/null
chkconfig --add `basename $0`
sta () {
touch /var/lock/subsys/`basename $0`
. /etc/init.d/functions
action "`basename $0` start successful!" true
}
sto () {
rm /var/lock/subsys/`basename $0`
. /etc/init.d/functions
action "`basename $0` is stopped" true
}
rest () {
sto
sta
}
statu () {
if [ -f /var/lock/subsys/`basename $0` ];then
. /etc/init.d/functions
action "`basename $0` is running" true
else
action "`basename $0` is stopped" true
fi
}
case $1 in
start)
if [ -f /var/lock/subsys/`basename $0` ];then
echo "`basename $0` is running"
else
sta
fi
;;
stop)
if [ -f /var/lock/subsys/`basename $0` ];then
sto
else
echo "`basename $0` has been stopped before"
fi
;;
restart)
if [ -f /var/lock/subsys/`basename $0` ];then
sto
sta
else
sta
fi
;;
status)
statu
;;
esac
7.编写脚本/root/bin/copycmd.sh
(1) 提示用户输入一个可执行命令名称
(2) 获取此命令所依赖到的所有库文件列表
(3) 复制命令至某目标目录(例如/mnt/sysroot)下的对应路径下
如:/bin/bash ==> /mnt/sysroot/bin/bash
/usr/bin/passwd ==> /mnt/sysroot/usr/bin/passwd
(4) 复制此命令依赖到的所有库文件至目标目录下的对应路径下: 如:/lib64/ld-linux-x86-64.so.2 ==> /mnt/sysroot/lib64/ld-linux-x86-64.so.2
(5)每次复制完成一个命令后,不要退出,而是提示用户键入新的要复制的命令,并重复完成上述功能;直到用户输入quit退出
[root@localhost ~/bin]#bash copycmd.sh
please input a excute cmd(eg:cat|bash|quit):cat
cp /usr/bin/cat finished
cp /lib64/libc.so.6 finished
cp /lib64/ld-linux-x86-64.so.2 finished
please input new excute cmd(eg:cat|bash|quit):bash
cp /usr/bin/bash finished
cp /lib64/libtinfo.so.5 finished
cp /lib64/libdl.so.2 finished
/lib64/libc.so.6 has been cp before
/lib64/ld-linux-x86-64.so.2 has been cp before
please input new excute cmd(eg:cat|bash|quit):quit
which: no quit in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/root/bin)
[root@localhost ~/bin]#cat copycmd.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: copycmd.sh
#********************************************************************
#!/bin/bash
#
read -p "please input a excute cmd(eg:cat|bash|quit):" command
[ -a /mnt/sysroot ]||mkdir /mnt/sysroot &>/dev/null
cpdir () {
cmd=`which --skip-alias $command`
if [ -f /mnt/sysroot$cmd ];then
echo "the $cmd has been cp before"
else
local dir=`dirname $cmd`
mkdir -p /mnt/sysroot$dir
cp $cmd /mnt/sysroot$cmd
echo "cp $cmd finished"
fi
}
cplib () {
cmd=`which --skip-alias $command`
ldd $cmd |egrep -o "/lib64.* "|while read i;do
if [ ! -f /mnt/sysroot$i ];then
local dir=`dirname $i`
mkdir -p /mnt/sysroot$dir
cp $i /mnt/sysroot$i
echo "cp $i finished"
else
echo "$i has been cp before"
fi
done
}
type $command&>/dev/null||{ echo the command is not exsit;exit 100; }
until [ $command == quit ];do
type $command&>/dev/null||{ echo the command is not exsit;exit 100; }
cpdir
cplib
read -p "please input new excute cmd(eg:cat|bash|quit):" command
cmd=`which --skip-alias $command`
done