1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本, CPU型号,内存大小,硬盘大小
#! /bin/bash
IP=`ifconfig ens33 |egrep -o "[[:digit:].]{7,15}" |head -n 1`
echo "Hostname is `hostname`"
echo "IP is $IP"
echo "OS is `cat /etc/centos-release`"
echo "Kernel is `lsb_release`"
echo "CPU info is `lscpu | grep "Model name" |egrep -o "([[:space:]]{3}).*$"`"
echo "Mem size is `cat /proc/meminfo | head -n1`"
echo "Disk size is `lsblk | head -n 2 |egrep -o "[[:digit:]]+G"`"
unset IP
2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
MaxUse=`df -h |grep "^/dev/sd.*" | tr -s " " "%"|cut -d % -f 5 -n |sort -r -n |head -n 1`
4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
1、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#! /bin/bash
ID10L=`cat /etc/passwd |head -n20 | tail -n 11 |cut -d ":" -f3 |head -n1`
ID20L=`cat /etc/passwd |head -n20 | tail -n 11 |cut -d ":" -f3 |tail -n1`
Sum=$[ID10L+ID20L]
echo "ID10L+ID20L = $Sum"
unset ID10L
unset ID20L
unset Sum
2、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
#! /bin/bash
# No.46 Zhu
# test two files is noral file
[ ! -f $1 ] && { echo $1 is not a normal file; exit; }
[ ! -f $2 ] && { echo $2 is not a normal file; exit; }
#space num
Sum1Space=`cat $1 |egrep "^[[:space:]]*$" |wc -l`
Sum2Space=`cat $2 |egrep "^[[:space:]]*$" |wc -l`
echo $1 Space num is $Sum1Space
echo $2 Space num is $Sum2Space
Sum=$[Sum1Space+Sum2Space]
echo "Two Files Space Sum= $Sum"
unset Sum1Space
unset Sum12Space
unset Sum
3、编写脚本/root/bin/sumfile.sh,统计/etc,/var,/usr目录中共有多少个一级子目录和文件
#! /bin/bash
[ ! -d $1 ] && { echo $1 is not a dir; exit; }
[ ! -d $2 ] && { echo $2 is not a dir; exit; }
[ ! -d $3 ] && { echo $3 is not a dir; exit; }
Dir1File=`ls $1 |wc -l`
Dir2File=`ls $2 |wc -l`
Dir3File=`ls $3 |wc -l`
echo "sumfile = $[Dir1File+Dir2File+Dir3File]"
unset Dir1File
unset Dir2File
unset Dir3File
1、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#! /bin/bash
[ $# -lt 1 ] && { echo "Please input agrs more than one !" ; exit; } || [ ! -f $1 ] && { echo $1 is not a normal file; exit; }; echo `egrep "^[[:space:]]*$" $1 |wc -l`
2、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问” ;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash
ping -c1 $1 >& /dev/null
[ $? -eq 0 ] && echo "$1 can access !" || echo " $1 can not access !"
[ $? -eq 0 ] && echo " can access !" || echo " can not access !"
3、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
#!/bin/bash
DiskUse=`df |grep /dev/sd |egrep -o "\b[0-9]{1,3}%" |egrep -o "[0-9]{1,3}" | sort -nr |head -n 1`
InodeNum=`df -i |egrep -o "\b[0-9]{1,3}%" |egrep -o "[0-9]{1,3}" | sort -nr |head -n 1`
[ $DiskUse -gt 80 ] && wall "Disk use > 80%"
[ $InodeNum -gt 80 ] && wall "Inode use > 80%"
1、编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
#! /bin/bash
[ -r $1 ] && { echo $USER can read $1; exit; }
[ -w $1 ] && { echo $USER can write $1; exit; }
echo $USER can not read and write!
2、编写脚本/root/bin/excute.sh,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
#! /bin/bash
# Author No.46_Zhu
[ ! -f $1 ] && { echo "$1 is not a normal file"; exit; }
# no.1
# echo $1 |egrep "[^.].*\.sh" &>/dev/null
# [ $? -ne 0 ] &&{ echo $1 is not a .sh file !; exit; } || chmod +x $1
# no.2
[[ "$1" =~ [^.].*\.sh ]] && chmod +x $1
echo "All users can excute $1"
[root@centos7 ~]#excute46.sh /root/bin/f11.sh
All users can excute /root/bin/f11.sh
[root@centos7 ~]#ll /root/bin/f11.sh
-rwxr-xr-x. 1 root root 0 Aug 3 10:40 /root/bin/f11.sh
3、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
在用户建立/etc/nologin 这样用户就不能登录