1.编写函数,实现OS的版本判断
[root@node2 ~]#vi osversion.sh
[root@node2 ~]#cat osversion.sh
#!/bin/bash
get_os_version () {
os_version=`sed -r 's/.* ([0-9])\..*/\1/' /etc/redhat-release`
echo "$os_version"
}
if [ `get_os_version` -eq 6 ];then
echo "os_version is 6"
elif [ `get_os_version` -eq 7 ];then
echo "os_version is 7 "
elif [ `get_os_version` -eq 5 ];then
echo "os_version is 5"
fi
[root@node2 ~]#chmod +x osversion.sh
[root@node2 ~]#./osversion.sh
os_version is 7
2.编写函数,实现取出当前系统ens32的IP地址
[root@node2 ~]#vi get_ens32_ip.sh
[root@node2 ~]#chmod +x get_ens32_ip.sh
[root@node2 ~]#./get_ens32_ip.sh
ip_address=192.168.43.141
[root@node2 ~]#cat get_ens32_ip.sh
#!/bin/bash
get_ens32_ip () {
ip=`ifconfig ens32|grep netmask|tr -s ' '|cut -d ' ' -f3`
echo ip_address=$ip
}
get_ens32_ip
3.编写函数,实现打印绿色OK和红色FAILED
解法一:
[root@node2 ~]#vi print_color.sh
[root@node2 ~]#chmod +x print_color.sh
[root@node2 ~]#./print_color.sh
success! [ OK ]
failed [FAILED]
[root@node2 ~]#cat print_color.sh
#!/bin/bash
#
. /etc/init.d/functions
action "success!" true
action "failed" false
解法二:
[root@node2 ~]#vi print_color2.sh
[root@node2 ~]#chmod +x print_color2.sh
[root@node2 ~]#./print_color2.sh
FAILED
OK
[root@node2 ~]#cat print_color2.sh
#!/bin/bash
print_color2(){
echo -e "\033[41mFAILED\033[0m"
echo -e "\033[42mOK\033[0m"
}
print_color2
4.编写函数,实现判断是否无位置参数,如无参数,提示错误
[root@node2 ~]#vi test_para.sh
[root@node2 ~]#chmod +x test_para.sh
[root@node2 ~]#cat test_para.sh
#!/bin/bash
para_test () {
if [ $# -eq 0 ];then
echo "please input at least a para"
else
echo total $# parameters
fi
}
para_test a
[root@node2 ~]#cat test_para.sh
#!/bin/bash
para_test () {
if [ $# -eq 0 ];then
echo "please input at least a para"
else
echo total $# parameters
fi
}
para_test
[root@node2 ~]#./test_para.sh
please input at least a para
网友评论