1、创建用户的脚本:(标准版本)
[ ! -f "$1" ] && echo "$1 file is not exit ! " && exit
STUDEN_FILE = $ 1
for USER in cat $STUDEN_FILE
;do
if id ${USER} &> /dev/null;then
echo " 用户: ${USER}已存在,创建失败 "
else
useradd ${USER}
echo ${USER} | passwd --stdin ${USER} &> /dev/null
echo "用户:${USER}创建成功"
fi
done
注:/dev/urandom 本文件可生成随机字符
2、提示请输入网址如172.20.119 判断输入的网段中主机在线状态:
NET=172.20.119
START=1
END=254
for HOSTID in seq $START $END
;do
{
if ping -c1 -w1 ${NET}.${HOSTID} &> /dev/null;then
echo ${NET}.${HOSTID} is up
echo ${NET}.${HOSTID} >> hostlist。txt
else
echo ${NET}.${HOSTID} is down
fi
}&
done
wait
3、判断/var/目录下所有文件的类型
for i in $(find /var) ;do
if [ -b $i ];then
echo "$i是块设备"
elif [ -c $i ];then
echo "$i是字符设备"
elif [ -f $i ];then
echo "$i是普通文件"
elif [ -h $i ];then
echo "$i是符号链接文件"
elif [ -p $i ];then
echo "$i是管道文件"
elif [ -s $i ];then
echo "$i是 套接文件"
elif [ -d $i ];then
echo "$i是目录文件"
else
echo “文件或目录不存在”
fi
done
exit 0
4、目录下分别有多个以K开头和以S开头的文件;分别读取每个文件, 以K开头的输出为文件加stop,以S开头的输出为文件名加start。
for i in /etc/rc.d/rc3.d/k;do
echo $i stop
done
for i in /etc/rc.d/rc3.d/s;do
echo $i start
done
5、提示输入正整数n的值,计算1+2+…+n的总和
read -p "please input a positive integer:" n
i=1
sum=0
for i in seq 1 $n
;do
let sum+=i
done
echo "sum is $sum"
6、计算100以内所有能被3整除的整数之和
for n in {1..100};do
if [ $[ $n%3 ] -eq 0 ];then
let sum+=n
fi
done
echo "sum is $sum”
7、打印九九乘法表
for i in {1..9};do
for j in seq 1 $i
;do
result = $[ $j$i ]
echo -e "${j}x${i}=$result\t\c"
done
echo
done
8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字 母,如: 1AbCdeFgH.html
if [ ! -d /testdir ];then
mkdir /testdir
fi
a=1
while [ $a -le 10 ];do
touch /testdir/$a$(tr -cd [:alnum:] < /dev/random |head -c 8).html
a=$(echo $a+1|bc)
done
9、打印等腰三角形
read -p "请输入需要生成的等腰三角形的行数:" zongline
for curline in $(seq 1 $zongline);do
spacenum=$[$zongline-$curline]
anum=$[2$curline-1]
for kongge in $(seq 1 $spacenum);do
echo -e "" "\c"
done
for a in $(seq 1 $anum);do
echo -e "a\c"
done
echo
done
10、求100以内所有正奇数之和
sum=0
n=1
while [ $n -le 100 ]
do
sum=$((sum+n))
n=$((n+2))
done
echo $sum
11、提示请输入网络地址,如192.168.0.0,判断输入的网段中主机 在线状态,并统计在线和离线主机各多少
read -p "please input nework(eg:192.168.0.0): " network
netid=echo $network|cut -d. -f1-3
hostid=1
up=0
down=0
while [ $hostid -le 254 ];do
if ping -c1 -w1 $netid.$hostid &>/dev/null;then
echo "the $netid.$hostid is up"
let up++
else
echo "the $netid.$hostid is down"
let down++
fi
let hostid++
done
echo the up is $up
echo the down is $down
12、打印九九乘法表
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
let k=ij
echo -en "$i$j=$k\t"
let j++
done
echo
let i++
done
13、利用变量RANDOM生成10个随机数字,输出这个10数字,并显 示其中的最大值和最小值
x=0
while [ $x -le 9 ];do
if [ $x -le 9 ];then
echo $RANDOM |tee -a /tmp/suiji.txt
else
break
fi
let x++
done
echo "max num is : $(sort -n /tmp/suiji.txt |tail -1)"
echo "min num is : $(sort -n /tmp/suiji.txt |head -1)"
14、实现打印国际象棋棋盘
for i in {1..8};do
for j in {1..8};do
if [ $[$i%2] -eq 1 ];then
if [ $[$j%2] -eq 1 ];then
echo -en "\033[47;37m \033[0m"
else
echo -en "\033[40;37m \033[0m"
fi
else
if [ $[$j%2] -eq 1 ];then
echo -en "\033[40;37m \033[0m"
else
echo -en "\033[47;37m \033[0m"
fi
fi
done
echo
done
15、后续六个字符串: efbaf275cd、 4be9c40b8b、 44b2395c46、 f8c8873ce0、 b902c16c8b、 ad865d2f63是通过对随机数变量RANDOM随机 执行命令: echo $RANDOM|md5sum|cut –c1-10 后的结果,请破解这些 字符串对应的RANDOM值
ps1=efbaf275cd
ps2=4be9c40b8b
ps3=44b2395c46
ps4=f8c8873ce0
ps5=b902c16c8b
ps6=ad865d2f63
echo ‘num ‘ ‘random‘
time=1
for i in {0..32767}
do
if [ $time -gt 6 ];then
break
fi
ps=$(echo $i | md5sum | cut -c1-10)
for j in $ps1 $ps2 $ps3 $ps4 $ps5 $ps6
do
if [ $ps == $j ];then
echo $i $ps
let time++
fi
done
done
16、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录, 则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本
until false;do
if who |grep "^hacker\>" &> /dev/null;then
who|grep "^hacker\>" > /var/log/login.log
break
fi
sleep 3
done
17、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
suiji=$[$RANDOM%10]
time=1
while [ $time -le 3 ];do
read -p "请输入0-9的任意整数:" shuru
if [ $shuru -eq $suiji ];then
echo "猜中了"
break
elif [ $shuru -gt $suiji ];then
echo "请小一点"
echo "您还有$[3-$time]次机会"
let time++
elif [ $shuru -lt $suiji ];then
echo "请大一点"
echo "您还有$[3-$time]次机会"
let time++
fi
done
原文地址:http://blog.51cto.com/14128387/2342150