一、每隔3秒鐘到系統上獲取已經登陸的用戶的信息;若是發現用戶hacker登陸,則將登陸時間和主機記錄於日誌/var/log/login.log中,並提示該用戶退出系統。
bash
#!/bin/bashless
until who | grep "^\<hacker\>" > /var/log/login.log && echo "you must logout" | write jackide
dospa
sleep 3s日誌
done遊戲
二、隨機生成10之內的數字,實現猜字遊戲,提示比較大或小,相等則退出it
#!/bin/bashclass
declare -i r=$(($RANDOM%10))登錄
declare -i n=10000000000000000變量
until [ "$n" -eq "$r" ] && echo "you are so clear"
do
read -p "you can guess a number[0-10]:" n
if [ "$n" -gt "$r" ]
then
echo "you guess great"
elif
[ "$n" -lt "$r" ]
then
echo "you guess less"
fi
done
三、編寫腳本,求100之內全部正整數之和
#!/bin/bash
declare -i i=1
declare -i sum=0
until [ $i -eq 101 ] ; do
sum=$[$sum+$i]
i=$[$i+1]
done
echo "sum1..100:$sum"
四、編寫腳本,經過ping命令探測172.16.250.1-254範圍內的全部主機的在線狀態,統計在線主機和離線主機各多少。
#!/bin/bash
declare -i i=1
declare -i y=0
declare -i n=0
until [ $i -eq 255 ];do
ping -c 1 -W 1 10.1.252.$i &> /dev/null && y=$[$y+1] || n=$[$n+1]
i=$[$i+1]
done
echo "ping up:$y"
echo "ping down:$n"
五、編寫腳本,打印九九乘法表
#!/bin/bash
i=1
until [ $i -ge 10 ]; do
j=1
until [ $j -gt $i ];do
echo -ne ""$j"x"$i"=$[$i*$j]\t"
j=$[$j+1]
done
i=$[$i+1]
echo
done
六、編寫腳本,利用變量RANDOM生成10個隨機數字,輸出這個10數字,並顯示其中的最大者和最小者
until作法:
#!/bin/bash
MAX=$RANDOM MIN=$MAX i=1
until [ $i -ge 11 ] ; do
n=$RANDOM
i=$[$i+1]
[ $MAX -ge $n ] && MAX=$MAX || MAX=$n
[ $MIN -le $n ] && MIN=$MIN || MIN=$n
echo $n
done
echo "MAX=$MAX"
echo "MIN=$MIN"
while作法:
#!/bin/bash
i=1 a=$RANDOM
echo $a
b=$a
while [ $i -le 9 ];do
n=$RANDOM
echo $n
i=$[$i+1]
[ $n -gt $a ] && a=$n || a=$a
[ $n -lt $b ] && b=$n || b=$b
done
echo "MAX $a"
echo "MIN $b"
七、編寫腳本,實現打印國際象棋棋盤
until作法:
#/bin/bash
i=1
until [ $i -gt 9 ];do
j=1
until [ $j -gt 9 ];do
[ $[$[$i+$j]%2] -eq 0 ] && echo -en "\033[41m \033[0m" || echo -en "\033[46m \033[0m"
j=$[$j+1]
done
i=$[$i+1]
echo
done
for作法:
#/bin/bash
for i in {1..8}
do
for j in {1..8}
do
if [ $j -lt 8 ] ; then
[ "$[$[$i+$j]%2]" -eq 0 ] && echo -en "\033[41m \033[0m" || echo -en "\033[45m \033[0m"
else
[ "$[$[$i+$j]%2]" -eq 0 ] && echo -e "\033[41m \033[0m" || echo -e "\033[45m \033[0m"
j=j++
fi
done
i=i++
done
八、打印等腰三角形
#!/bin/bash
read -p "number:" n
for j in `seq $n`;do
for a in `seq $[$n-$j]` ; do
echo -n " "
done
b=$[$j*2-1]
for m in `seq $b`;do
echo -n "#"
done
echo
done