1、編寫腳本 argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
#!/bin/bash
[ $# -lt 1 ] && { echo "Usage: ./argsnum.sh xxx Provide at least 1 argument!!!"; exit; }
space_line=`grep -E "^[[:space:]]*$" $1 |wc -l`
echo "Number of sapce lines/line are/is ;${space_line}"
2、編寫腳本 hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
#!/bin/bash
[ $# -lt 1 ] && { echo "Usage: ./hostping.sh IP"; exit; }
ping -c4 -W2 $1 > /dev/null && echo "$1 can be connected!" || echo "$1 can't be connected!"
3、編寫腳本 checkdisk.sh,檢查磁盤分區(qū)空間和inode使用率,如果超過80%,就發(fā)廣播警告空間將滿
#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-09 15:10
#FileName: checkdisk.sh
#Description:Shell Script
#********************************************************************
warning=80
disk_usage=`df |grep -E "/dev/sd" |tr -s ' ' % |cut -d% -f5 |sort -rn |head -n1`
inode_usage=`df -i |grep -E "/dev/sd" |tr -s ' ' % |cut -d% -f5 |sort -rn |head -n1`
[ ${disk_usage} -gt ${warning} -o ${inode_usage} -gt ${warning} ] &&
echo -e "\E[1;31m DISK USED: ${disk_usage}%, INODE USED: ${inode_usage}% WILL BE FULL SOON!!! \E[0m" ||
echo -e "\E[1;32m DISK USED: ${disk_usage}%, INODE USED: ${inode_usage}% SAFE USE! \E[0m"
驗證:
[root@centos8 shell-excercises]# dd if=/dev/zero of=/boot/img.txt bs=1M count=700
[root@centos8 shell-excercises]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 948M 0 948M 0% /dev
tmpfs 976M 0 976M 0% /dev/shm
tmpfs 976M 9.5M 966M 1% /run
tmpfs 976M 0 976M 0% /sys/fs/cgroup
/dev/sda2 94G 5.8G 88G 7% /
/dev/sda5 47G 365M 47G 1% /data
/dev/sda1 947M 922M 26M 98% /boot
tmpfs 196M 3.5M 192M 2% /run/user/0
[root@centos8 shell-excercises]# ./checkdisk.sh
DISK USED: 98%, INODE USED: 1% WILL BE FULL SOON!!!
[root@centos8 shell-excercises]# rm -f /boot/img.txt
[root@centos8 shell-excercises]# ./checkdisk.sh
DISK USED: 24%, INODE USED: 1% SAFE USE!
4、編寫腳本 per.sh,判斷當前用戶對指定參數文件,是否不可讀并且不可寫
#!/bin/bash
[ ! -r $1 -a ! -w $1 ] && echo "File $1 can't be read or write!" || {
echo "File $1 can be r or w!"; }
5、編寫腳本 excute.sh ,判斷參數文件是否為sh后綴的普通文件,如果是,添加所有人可執(zhí)行權限,否則提示用戶非腳本文件
#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-08 09:42
#FileName: excute.sh
#Description:Shell Script
#********************************************************************
[[ -f $1 && "$1" =~ \.sh$ ]] && { chmod a+x $1; echo -e "\E[1,32m Add x for all user!\E[0m"; } || echo "No such file or not sh file"
6、編寫腳本 nologin.sh和 login.sh,實現禁止和允許普通用戶登錄系統
#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-08 11:18
#FileName: login.sh
#Description:Shell Script
#********************************************************************
id $1 &> /dev/null || { echo "no user $1!"; exit; }
user_id=`id $1 |grep -oE "uid=[0-9]+" |cut -d= -f2`
user_shell=`cat /etc/passwd |grep $1|cut -d: -f7`
[ ${user_id} -ge 1000 -a ${user_shell} != "/bin/bash" ] && {
echo -e "\E[1;32m Set user $1 shell from ${user_shell} to /bin/bash...\E[0m";
usermod -s /bin/bash $1 &> /dev/null;
echo -e "\E[1;32m Done...\E[0m"; } || {
echo -e "\E[1;31m Shell already in login or not valid user...\E[0m"
}
#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-08 11:18
#FileName:nologin.sh
#Description:Shell Script
#********************************************************************
id $1 &> /dev/null || { echo "no user $1!"; exit; }
user_id=`id $1 |grep -oE "uid=[0-9]+" |cut -d= -f2`
user_shell=`cat /etc/passwd |grep $1|cut -d: -f7`
[ ${user_id} -ge 1000 -a ${user_shell} != "/sbin/nologin" ] && {
echo -e "\E[1;32m Set user $1 shell from ${user_shell} to /sbin/nologin...\E[0m";
usermod -s /sbin/nologin $1 &> /dev/null;
echo -e "\E[1;32m Done...\E[0m"; } || {
echo -e "\E[1;31mUser shell already in nologin or not valid user...\E[0m"
}