1、编写函数,实现 OS 的版本判断;
#!/bin/bash
os-version()
{
echo OS Version : `cat /etc/redhat-release | sed -nr 's/^.* ([0-9]+).*/\1/p'`
}
os-version
[root@localhost ~]
OS Version : 6
[root@centos7 ~]
OS Version : 7
2、编写函数,实现取出当前系统 eth0 的 IP 地址;
get-ip()
{
echo IP Address : `ifconfig eth0 | sed -nr 's/^[^0-9]+([0-9.]{7,15}).*/\1/p'`
}
get-ip
[root@localhost ~]
IP Address : 10.10.10.66
[root@centos7 ~]
IP Address : 10.10.10.10
3、编写函数,实现打印绿色 OK 和红色 FAILED;
#!/bin/bash
okfailed()
{
if [[ $1 == 'ok' ]]
then
echo -e "\t\t\t\t\t\t [ \e[1;32mOK\e[0m ] "
else
echo -e "\t\t\t\t\t\t [ \e[1;31mFAILED\e[0m ] "
fi
}
okfailed ok
okfailed failed
[root@centos7 ~]
[ OK ]
[ FAILED ]
4、编写函数,实现判断是否无位置参数,如无参数,提示错误。
#!/bin/bash
args()
{
if [[ $
then
echo Yes , $
else
echo Error , No args.
fi
}
echo test 1: no arg.
args
echo ----
echo test 2: 1 arg.
args arg0
echo ----
echo test 3: 2 args.
args arg1 arg2
[root@centos7 ~]
test 1: no arg.
Error , No args.
----
test 2: 1 arg.
Yes , 1 arg[s].
----
test 3: 2 args.
Yes , 2 arg[s].