1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。
#!/bin/bash #author:Zhu Chaoming #date:20160816 #version:0.1 # while true do w w |grep "^bbb" &> /dev/null REV=$? if [ $REV -eq 0 ]; then w >> /var/log/login.log write bbb < 1.txt fi sleep 3 done |
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash PRICE=$(expr $RANDOM % 10 + 1) TIMES=0 echo "商品实际价格为0-10之间,猜猜看是多少?" while true do read -p "请输入你猜测的价格数目:" INT let TIMES++ if [ $INT -eq $PRICE ] ; then echo "恭喜你答对了,实际价格是 $PRICE" echo "你总共猜测了 $TIMES 次" exit 0 elif [ $INT -gt $PRICE ] ; then echo "太高了!" else echo "太低了!" fi done |
3、编写脚本,求100以内所有正整数之和
#!/bin/bash # declare -i SUM=0 for I in `seq $1 $2`; do let SUM+=$I done echo "$SUM" |
4、编写脚本,打印九九乘法表
#!/bin/bash # I=1 J=1 for I in `seq 1 $1`; do for J in `seq 1 $I`; do let num=$I*$J echo -ne "$I*$J=$num\t" done echo -e "\n" done |
5、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者
#!/bin/bash #date:2016/08/16 #work:作业 declare -i MAX=0 declare -i MIN=0 i=1 #MYrandom=$RANDOM while [ $i -le 10 ]; do MYrandom=$RANDOM #\\将$RANDOM的数据赋值个MYRANDOM [ $i -eq 1 ]&&MIN=$MYrandom if [ $i -lt 9 ]; then echo -ne "$MYrandom\t" else echo -ne "$MYrandom\t" fi [ "$MYrandom" -gt "$MAX" ] && MAX=$MYrandom [ "$MYrandom" -lt "$MIN" ] && MIN=$MYrandom let i++ done echo -e "\nMAX=$MAX , MIN=$MIN" |
6、编写脚本,实现打印国际象棋棋盘
#!/bin/bash for i in $(seq 1 8);do for j in $(seq 1 8);do [ $[i%2-$j%2] -eq 0 ] && echo -ne "\033[41m \033[0m" || echo -ne "\033[47m \033[0m" let j++ done echo "" let i++ done |
7、打印等腰三角形
#!/bin/bash #author:Zhu Chaoming #date:20160817 #version:0.1 while true do read -p "please input number: " Line i=1 while [ $i -le $Line ]; do let Space=Line-i for((h=1; h<=$Space; h++));do echo -ne " " done #echo "" let x=2*$i-1 for((j=1; j<=$x; j++)); do echo -n "*" done echo "" let i++ done done |
原创文章,作者:皱 多利亚,如若转载,请注明出处:http://www.178linux.com/37416