Linux练习(Part6.7:shell脚本编程)


练习

1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本

[22:02:15 root@Centos8 /data/Mage]#cat until_hacker.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-29
#FileName:                 until_hacker.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
until false;do
    if who|grep "^hacker\>"&>/dev/null;then
        who|grep "^hacker\>">/var/log/login.log
        break
    fi
    sleep 3
done
[21:51:53 root@Centos8 /data/Mage]#useradd hacker
[21:52:05 root@Centos8 /data/Mage]#echo 123456|passwd --stdin hacker

[20:11:53 root@Centos7 ~]#ssh hacker@100.0.0.120
hacker@100.0.0.120's password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Fri Jan 29 21:53:53 2021 from 100.0.0.200
[21:58:16 hacker@Centos8 ~]$

[22:03:20 root@Centos8 /data/Mage]#cat /var/log/login.log
hacker   pts/1        2021-01-29 21:58 (100.0.0.200)

2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出

[22:13:36 root@Centos8 /data/Mage]#cat guess.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-29
#FileName:                 guess.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
NUM=$[RANDOM%10]
while read -p "输入 0-9 之间的数字: " INPUT;do
    if [ $INPUT -eq $NUM ];then
        echo "恭喜你猜对了!"
        break
    elif [ $INPUT -gt $NUM ];then
        echo "数字太大了,重新猜!"
    else
        echo "数字太小了,重新猜!"
    fi
done

[22:13:46 root@Centos8 /data/Mage]#bash guess.sh
输入 0-9 之间的数字: 8
数字太大了,重新猜!
输入 0-9 之间的数字: 6
数字太小了,重新猜!
输入 0-9 之间的数字: 7
恭喜你猜对了!

3、用文件名做为参数,统计所有参数文件的总行数

[22:00:42 root@Centos8 /data/Mage]#cat  until_file.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-30
#FileName:                 until_file.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#用文件名做为参数,统计所有参数文件的总行数
if [ $# -eq 0 ];then
    echo "Please input file path"
else
    until [ -z "$1" ];do
        echo "统计文件: $1"
        echo "文件总行数: `wc -l $1|cut -d" " -f1`"
        echo
        shift
    done
echo "finish"
fi
[22:00:54 root@Centos8 /data/Mage]#bash until_file.sh /etc/init.d/functions /etc/issue /etc/fstab
统计文件: /etc/init.d/functions
文件总行数: 709

统计文件: /etc/issue
文件总行数: 3

统计文件: /etc/fstab
文件总行数: 15

finish

4、用二个以上的数字为参数,显示其中的最大值和最小值

[22:12:23 root@Centos8 /data/Mage]#cat  while_maxmin.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-30
#FileName:                 while_maxmin.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#用二个以上的数字为参数,显示其中的最大值和最小值
max=$1
min=$1
while [ $# -gt 0 ];do
    if [ $1 -lt $min ];then
        min=$1
    fi
    if [ $1 -gt $max ];then
        max=$1
    fi
    shift
done
echo "maxnum is $max"
echo "minnum is $min"
[22:12:38 root@Centos8 /data/Mage]#bash while_maxmin.sh 1000 445
maxnum is 1000
minnum is 445
[22:12:42 root@Centos8 /data/Mage]#bash while_maxmin.sh 100 445
maxnum is 445
minnum is 100
posted @ 2021-01-28 17:45  加油啊坚持啊搞钱啊  阅读(145)  评论(0)    收藏  举报