1 、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信

息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU 型号,内存大小,硬盘大小

  1. #!/bin/bash
  2. #Author: song
  3. #Date: 2018-08-19
  4. #Description: show system info
  5. echo "The host is `hostname`"
  6. echo "The IP is `ifconfig ens33 | grep netmask | tr -s ' ' | cut -d" " -f3`"
  7. echo "The release is `cat /etc/centos-release`"
  8. echo "The kernel version is `uname -r`"
  9. echo "The CPU is `lscpu | grep "Model name:" | tr -s ' ' | cut -d: -f2`"
  10. cat /proc/meminfo | head -n1
  11. echo "The Disk is `lsblk | grep "^sda" |tr -s ' ' '%' | cut -d% -f4`"

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录

备份到/root/etcYYYY-mm-dd中

  1. #!/bin/bash
  2. #Author: song
  3. #Date: 2018-08-19
  4. #Description:
  5. cp -a /etc /root/etc$( date +%F )

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利

用率最大的值

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远

程主机的IPv4地址和连接数,并按连接数从大到小排序

5、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中

的第10个用户和第20用户的ID之和

  1. #!/bin/bash
  2. # ------------------------------------------
  3. # Filename: sumid.sh
  4. # Revision: 1.0
  5. # Author: song
  6. # Date: 2018-08-19
  7. # Description:
  8. # ------------------------------------------
  9. uid1=`head /etc/passwd | tail -1 | cut -d: -f3`
  10. uid2=`head -n20 /etc/passwd | tail -1 | cut -d: -f3`
  11. echo $[uid1+uid2]

6、编写脚本/root/bin/sumspace.sh,传递两个文件路径作

为参数给脚本,计算这两个文件中所有空白行之和

7、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目

录中共有多少个一级子目录和文件

8、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为

参数;如果参数个数小于1,则提示用户“至少应该给一个参 数”,并立即退出;如果参数个数不小于1,则显示第一个参 数所指向的文件中的空白行数

  1. #!/bin/bash
  2. # ------------------------------------------
  3. # Filename: argsnum.sh
  4. # Revision: 1.0
  5. # Author: song
  6. # Date: 2018-08-20
  7. # Description:
  8. # ------------------------------------------
  9. [ $# -lt 1 ] && echo "at least one arg!" && exit 10
  10. [ -e $1 ] && echo "The blankLine is `grep '^[[:space:]]*$' $1 | wc -l`" || echo "No such file or directory!"

9、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4

地址做为参数,测试是否可连通。如果能ping通,则提示用户 “该IP地址可访问”;如果不可ping通,则提示用户“该IP地 址不可访问”

  1. #!/bin/bash
  2. # ------------------------------------------
  3. # Filename: hostping.sh
  4. # Revision: 1.0
  5. # Author: song
  6. # Date: 2018-08-20
  7. # Description:
  8. # ------------------------------------------
  9. [ $# -ne 1 ] && echo "should be given one IPv4 arg" && exit 10
  10. [[ $1 =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] || { echo "IPv4 is not valid";exit 20; }
  11. ping -w1 -c1 $1 &> /dev/null && echo "该IP地址可访问" || echo "该IP地址不可访问"

10、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和

inode使用率,如果超过80%,就发广播警告空间将满

#!/bin/bash
# ------------------------------------------
# Filename:    checkdisk.sh
# Revision:    1.0
# Author:      song
# Date:        2018-08-20
# Description: 
# ------------------------------------------

disk=`(df /dev/sd* ; df -i /dev/sd*) | egrep -o "[0-9]+%" | egrep -o "[0-9]+" | sort -nr | head -n1`

[ $disk -gt 80 ] && wall "disk will be full"

11、编写脚本/bin/per.sh,判断当前用户对指定的参数文件,

是否不可读并且不可写

简写
[ ! \( -r /etc/shadow -o -w /etc/shadow \) ] && echo true
上述等价
[ ! -r /etc/shadow -a ! -w /etc/shadow ] && echo true

12、编写脚本/root/bin/excute.sh ,判断参数文件是否为sh

后缀的普通文件,如果是,添加所有人可执行权限,否则提示 用户非脚本文件

13、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充

许普通用户登录系统

方法一
touch /etc/nologin
可添加提示
echo Disable common user login > /etc/nologin

14、让所有用户的PATH环境变量的值多出一个路径,例如:

/usr/local/apache/bin

15、用户root登录时,将命令指示符变成红色,并自动启用如

下别名: rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg- eno16777736 或 ifcfg-ens33 ’ (如果系统是CentOS7)

16、任意用户登录系统时,显示红色字体的警示提醒信息

“Hi,dangerous!”

vim /etc/profile.d/danger.sh
echo -e "\033[1;5;31mHi,dangerous!\033[0m"

17、编写生成脚本基本格式的脚本,包括作者,联系方式,版

本,时间,描述等

18、编写用户的环境初始化脚本reset.sh,包括别名,登录提

示符,vim的设置,环境变量等