1、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
set -ue
color="\033[1;31m"
end="\033[0m"
sleep 1
echo -e " \033[1;32m-----------------------------stsat-------------------------------------- $end"
sleep 1
echo -e " hostname: $color `hostname`$end"
echo -e " ipadd: $color `ifconfig | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -1 ` $end" echo -e " osversion:$color `cat /etc/redhat-release `$end"
echo -e " kernel: $color `uname -r`$end"
echo -e " cup: $color `lscpu | grep 'Model name' |tr -s ' ' |cut -d: -f2 |tr -d ' ' `$end"
echo -e " memory: $color `free -h | grep 'Mem' |tr -s ' ' : |cut -d: -f2` $end"
echo -e " disk: $color ` lsblk |grep -w 'sda' |tr -s ' ' |cut -d' ' -f4` $end"
echo -e "\033[1;34m==============================end============================================ $end"
[10:12:45 root@contos8 data]#/data/systeminfo.sh
-----------------------------stsat--------------------------------------
hostname: contos8.1
ipadd: 10.0.0.102
osversion: CentOS Linux release 8.0.1905 (Core)
kernel: 4.18.0-80.el8.x86_64
cup: Intel(R)Core(TM)i7-8750HCPU@2.20GHz
memory: 1.8Gi
disk: 200G
==============================end============================================
2、编写脚本 backup.sh,可实现每日将/etc/目录备份到/backup/etcYYYY-mm-dd中
set -ue
color="\033[1;35m"
end="\033[0m"
sleep 1
echo -e "\033[1;36m================Backup stat===========================$end"
sleep 1
echo -e "$color $( cp -av /etc/ /data/backup/etc`date +%F-%T`)$end "
echo -e "$color======================Finish==============================$end"
3、编写脚本 disk.sh,显示当前硬盘分区中空间利用率最大的值
set -ue
echo -e "\033[1;33m `df | grep '/dev/sda' |tr -s ' ' % |cut -d% -f5 |sort -n | tail -1`\033[0m"
[10:14:54 root@contos8 data]#/data/disk.sh
15
4、编写脚本 links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
set -ue
echo -e "\033[1;32m`ss -nt | grep 'ESTAB' | tr -s ' ' | cut -d' ' -f5 |sort |uniq -c |sort -nr`\033[0m"
[10:15:02 root@contos8 data]#/data/links.sh
1 10.0.0.1:54083
1 10.0.0.1:53896
5、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/bin
[14:20:52 root@contos6[ ~]#PATH=/usr/local/sbin:$PATH >> /etc/profile.d/env.sh
[14:22:40 root@contos6[ ~]#echo $PATH
/usr/local/sbin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
6、用户 root 登录时,将命令指示符变成红色,并自动启用如下别名: rm=‘rm –i’
[14:24:46 root@contos6[ ~]#vim /etc/profile.d/env.sh
PS1="\[\e[1;31m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"
[14:31:29 root@contos6 ~]#
[14:31:29 root@contos6 ~]#alias rm="rm -i" >> ~/.bashrc
[14:32:54 root@contos6 ~]#. ~/.bashrc
[14:33:09 root@contos6 ~]#ll /data
total 28
drwxr-xr-x. 118 root root 12288 Dec 19 10:46 backupetc2019-12-19
drwx------. 2 root root 16384 Dec 19 09:10 lost+found
[14:33:55 root@contos6 ~]#touch /data/mark
[14:34:39 root@contos6 ~]#rm /data/mark
rm: remove regular empty file `/data/mark'? y
[14:34:47 root@contos6 ~]#ll /data
total 28
drwxr-xr-x. 118 root root 12288 Dec 19 10:46 backupetc2019-12-19
drwx------. 2 root root 16384 Dec 19 09:10 lost+found
7、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
[10:52:51 root@contos6[ ~]#vim /etc/profile.d/env.sh
[10:54:35 root@contos6[ ~]#bash -n /etc/profile.d/env.sh
[10:54:42 root@contos6[ ~]#bash -x /etc/profile.d/env.sh
+ PS1='\[\e[1;31m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\$'
+ echo -e '\033[1;31m Hi,dangerous! \033[0m'
Hi,dangerous!
8、编写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提
示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
read -p "please input your ID:" ID
if ping -c1 -w1 $ID >> /dev/null ; then
echo "exixt!"
else
echo "no exixt!"
fi
9、编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
set -ue
DF1=`df | egrep "^/dev/sd" |tr -s ' ' % |cut -d% -f5 |sort |tail -1`
DF2=`df | egrep "^/dev/sd" |tr -s ' ' % |cut -d% -f5 |sort |tail -1`
if [ $DF1 -lt 80 -o $DF2 -gt 80 ] ; then
echo "the inode is baoman" | mail -s 'help' root ; exit 1 ;
else
echo "No problem."
fi
10、编写脚本argsnum.sh,判断当前用户对指定参数文件,是否不可读并且不可写
set -ue
if [ ! -e $1 ] ; then
echo "no a file"
elif [ ! -r $1 -a ! -w $1 ]; then
echo "file no read and no write. "
else
echo "file read or write "
fi
11、1、编写脚本 anum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给
一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
set -ue
if cat $1 &> /dev/null ; then
grep -E "^$" $1 | wc -l ;
else
echo "please give a number."
fi