1、编写脚本 argsnum.sh,接受一个文件路径作为参数;如果参数个数小于 1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于 1,则显示第一个参数所指向的文件中的空白行数;
#!/bin/bash
if [ $
echo "至少应该给一个参数"
elif [ -f $1 ] ; then
echo "第一个参数指向的文件中空白行数:` grep '^$' $1 | wc -l` "
else
echo "指定的参数需为文件。"
fi
[ yinxd@centos7 ~] $ ./argsnum.sh /etc/yum.conf
第一个参数指向的文件中空白行数:3
2、编写脚本 hostping.sh,接受一个主机的 IPv4 地址做为参数,测试是否可连通。如果能 ping 通,则提示用户“该 IP 地址可访问”;如果不可 ping 通,则提示用户“该 IP 地址不可访问”;
#!/bin/bash
if [ $
echo “”请指定一个地址作为参数。”
else
ping -c1 $1 & > /dev/null
[ [ $? -eq 0 ] ] && echo "该IP地址可以访问。" || echo "该IP地址不可访问或输入的IP不正确。"
fi
[ yinxd@centos7 ~] $ ./hostping.sh
请指定一个地址作为参数。
[ yinxd@centos7 ~] $ ./hostping.sh 10.10.10.1
该IP地址可以访问。
[ yinxd@centos7 ~] $ ./hostping.sh 10.10.10.111
该IP地址不可访问或输入的IP不正确。
[ yinxd@centos7 ~] $ ./hostping.sh 111
该IP地址不可访问或输入的IP不正确。
3、编写脚本 checkdisk.sh,检查磁盘分区空间和 inode 使用率,如果超过 80% ,就发广播警告空间将满;
#!/bin/bash
DISK_USAGE_RATE_MAX= ` df | grep '^/dev/' | tr -s ' ' '%' | cut -d% -f5 | sort -nr | head -1`
INODE_USAGE_RATE_MAX= ` df -i | grep '^/dev/' | tr -s ' ' '%' | cut -d% -f5 | sort -nr | head -1`
[ [ DISK_USAGE_RATE_MAX -gt 80 ] ] && wall Warning: Disk Usage rate is $DISK_USAGE_RATE_MAX %.
[ [ INODE_USAGE_RATE_MAX -gt 80 ] ] && wall Warning: Inode Usage rate is $INODE_USAGE_RATE_MAX %.
执行结果(使用率均未超过80%,测试时使用较小的值判断)
Broadcast message from yinxd@centos7 ( pts/1) ( Tue May 26 09:10:20 2020) :
Warning: Disk Usage rate is 20%.
Broadcast message from yinxd@centos7 ( pts/1) ( Tue May 26 09:10:20 2020) :
Warning: Inode Usage rate is 4%.
4、编写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写;
#!/bin/bash
if [ $
echo "请输入一个文件名作为参数。"
elif [ -f $1 ] ; then
if [ ! -r $1 -a ! -w $1 ] ; then
echo File $1 unreadable and unwritable.
fi
else
echo "指定的参数无效,请指定一个文件。"
fi
[ yinxd@centos7 ~] $ ./per.sh /etc/sudoers
File /etc/sudoers unreadable and unwritable.
5、编写脚本 excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件;
#!/bin/bash
if [ $
echo "请指定一个脚本文件作为参数。"
elif [ ! -f $1 ] ; then
echo "指定的文件不存在。"
elif [ ` echo $1 | grep -o ".[[:alpha:]]\+$" ` != '.sh' ] ; then
echo “"指定的参数不是扩展名为.sh脚本文件。"
else
chmod +x $1 & > /dev/null
if [ $? == 0 ] ; then
echo "添加所有人可执行权限成功。"
else
echo "添加所有人可执行权限失败。"
fi
fi
[ yinxd@centos7 ~] $ ll 1.sh
-rw-rw-r--. 1 yinxd yinxd 21 May 26 09:56 1.sh
[ yinxd@centos7 ~] $ ./excute.sh 1.sh
添加所有人可执行权限成功。
[ yinxd@centos7 ~] $ ll 1.sh
-rwxrwxr-x. 1 yinxd yinxd 21 May 26 09:56 1.sh
6、编写脚本 nologin.sh 和 login.sh,实现禁止和允许普通用户登录系统。
#!/bin/bash
if [ $
echo "请指定一个用户名作为参数。"
elif ! ` id $1 & > /dev/null` ; then
echo "指定的用户名不存在。"
else
sudo usermod -s /bin/bash $1
echo 允许登录设置成功。
fi
#!/bin/bash
if [ $
echo "请指定一个用户名作为参数。"
elif ! ` id $1 & > /dev/null` ; then
echo "指定的用户名不存在。"
else
sudo usermod -s /sbin/nologin $1
echo 禁止登录设置成功。
fi
[ yinxd@centos7 ~] $ getent passwd solin
solin:x:1009:1009::/home/solin:/bin/bash
[ yinxd@centos7 ~] $ ./nologin.sh solin
禁止登录设置成功。
[ yinxd@centos7 ~] $ getent passwd solin
solin:x:1009:1009::/home/solin:/sbin/nologin
[ yinxd@centos7 ~] $ ./login.sh solin
允许登录设置成功。
[ yinxd@centos7 ~] $ getent passwd solin
solin:x:1009:1009::/home/solin:/bin/bash