Shell script exercise---day1

1. Write the script argsnum.sh and accept a file path as a parameter; if the number of parameters is less than 1, the user will be prompted "At least one parameter should be given" and exit immediately; if the number of parameters is not less than 1, the first one will be displayed The number of blank lines in the file pointed to by the parameter;

#!/bin/bash
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-09
#Filename:                    argsnum.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
if [ $# -lt 1 ];then
        echo "至少应该给一个参数"
        exit
elif [ -f $1 ];then                                                                                                                                                       
        grep  "^$" $1 | wc -l

else
        echo "请输入有效文件路径"
        exit

2. Write the script hostping.sh and accept the IPv4 address of a host as a parameter to test whether it can be connected. If it can be pinged, the user will be prompted "This IP address is accessible", if it cannot be pinged, the user will be prompted "The IP address is not accessible".


#!/bin/bash
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-10
#Filename:                    hostping.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
read -p "请输入一个IP地址:" IP
ping -c1 -w1 $IP &> /dev/null && echo "该$IP地址可访问" || echo "该$IP地址不可访问"                                                                                                                                                              
                                                                                        

3. Write the script checkdisk.sh to check the disk partition space and inode usage. If it exceeds 80%, it will send a broadcast warning that the space will be full.

#!/bin/bash
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-10
#Filename:                    checkdisk.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
WARNING=1
SPACE_USE=`df | sed -rn "/^\/dev\/sd/s@^.* ([0-9]+)%.*@\1@p" | sort -nr | head -1`
INODE_USE=`df -i| sed -rn "/^\/dev\/sd/s@^.* ([0-9]+)%.*@\1@p" | sort -nr | head -1`
[ $SPACE_USE -gt $WARNING -o $INODE_USE -gt $WARNING ] && wall sapce is full  

4. Write the script per.sh to determine whether the specified parameter file is unreadable and unwritable by the current user

#!/bin/bash                                                                                                                                                                                                                                      
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-11
#Filename:                    per.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
FILE=$1
[ ! -r $FILE -a ! -w $FILE ] && echo "不可读且不可写"

5. Write the script excute.sh to determine whether the parameter file is an ordinary file with sh suffix. If it is, add everyone's executable permission, otherwise the user is prompted for a non-script file

#!/bin/bash
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-11
#Filename:                    excute.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
read -p "请输入参数文件:" FILE
[ -f $FILE ] && [[ "$FILE" == *.sh ]] && chmod +x $FILE || echo "这不是脚本文件"      

6. Write scripts nologin.sh and login.sh to prohibit and allow ordinary users to log in to the system

#########nologin.sh#########
#!/bin/bash
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-11
#Filename:                    nologin.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
[ -f /etc/nologin ] &&  echo "已禁止普通用户登录" || {
    
     touch /etc/nologin;echo "已开启禁止普通用户登录"; }    
#########login.sh#########
#!/bin/bash
# 
#**********************************************************************
#Author:                      awakening
#QQ:                          397269789
#Date:                        2020-11-11
#Filename:                    login.sh
#URL:                         http://www.yunmix.top
#Description:                 The test script
#**********************************************************************
[ -f /etc/nologin ] &&  {
    
     rm -f /etc/nologin ;echo "已开启普通用户登录"; }|| echo "已允许普通用户登录"                

For /etc/nologin, please refer to
https://blog.csdn.net/weixin_50904580/article/details/109615117

Guess you like

Origin blog.csdn.net/weixin_50904580/article/details/109585479