SHELL脚本进阶练习题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SHELL脚本进阶练习题相关的知识,希望对你有一定的参考价值。
用for实现脚本
判断/var/目录下所有文件的类型
read -p "pleasr input directory: " DIS
for F inls $DIS
;do
TY=file $DIS/$F | egrep -o "link|text|block|directory"
case $TY in
text)
echo "file $DIS/$F is file"
;;
link)
echo "file $DIS/$F is Link"
;;
block)
echo "file $DIS/$F is Block"
;;
directory)
echo "file $DIS/$F is Directory"
;;
*)
echo "file $DIS/$F is Others"
esac;
done
添加10个用户user1-user10,密码为8位随机字符
取随机8位字符的方法
openssl rand 4 -base64
tr -dc ‘0-7‘ </dev/urandom |head -c8for USER in user{1..10};do
useradd $USER
echoopenssl rand 4 -base64
| passwd --stdin $USER &>/dev/null
echo $USER created successful;
done
/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
for F in
ls /etc/rc.d/rc3.d/ | egrep ‘^K|^S‘
;do
S=echo $F | egrep -o ‘^K|^S‘
if [ "$S" = "K" ];then
echo $F stop
else
echo $F start
fi;
done
或者
for i inls /data/
;do
[[ "$i" =~ ^k. ]] && echo "$i stop"
[[ "$i" =~ ^s. ]] && echo "$i start"
done
编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
read -p ‘please input digit: ‘ D
[ $D -gt 0 ] || echo "please input digit"
sum=0
for i inseq $D
;do
let sum+=$i;
done
echo $sum
计算100以内所有能被3整除的整数之和
sum=0
for i in {3..100..3};do
let sum+=$i
done
echo $sum
或者
for i in {1..100};do
if [ $[ $i%3 ] -eq 0 ];then
let sum+=i
fi
done
echo "sum is $sum"
编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
read -p "please input network: " NET
SH=echo $NET | sed -rn ‘s/(.*.)(.*).(.*)/1/p‘
for i in {0..255};do
for j in {1..254};do
{ ping -c1 -W1 ${SH}${i}.${j} &>/dev/null && echo ${SH}${i}.${j} is up; }&
done
wait
done
echo "scan host is finished"
打印九九乘法表
echo "******************九九乘法表******************"
for i in {1..9};do
for j in {1..9};do
echo -e "$j$i=`echo $j$i | bc` c"
if [ $i -eq $j ];then
echo ""
break
fi
done
done
在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
for (( i=1;i<=10;i++ ));do
RAN=tr -dc ‘[:alpha:]‘ </dev/urandom | head -c8
touch /testdir/${i}${RAN}.html -p
done
echo "Ten file mkdir successful."
打印等腰三角形
read -p "please input triangle line: " line
for ((j=1;j<=$line;j++));do
COL_LOG=‘e[1;5;‘
COL_AND=‘e[0m‘
for ((a=1;a<=line-j;a++));do
echo -e " c"
done
sum=echo 2*$j-1|bc
for ((i=1;i<=${sum};i++));do
RAND="$[$RANDOM%7+31]m"
echo -e "${COL_LOG}${RAND}* c"
done
echo -e "${COL_AND}"
done
猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
sum=1
for ((n=1;n<10;n++));do
sum=$[2*$[$sum+1]]
done
echo "所摘桃子数: $sum"
用while实现脚本
编写脚本,求100以内所有正奇数之和
sum=0
i=1
while [ $i -le 100 ];do
echo -e "$i c"
sum=$[${sum}+${i}]
i=$[$i+2]
done
echo
echo "100以内所有正奇数之和为:$sum"
编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
read -p "please input new(eg:192.168.0.0): " net
netid=echo $net|cut -d. -f1-2
i=0
up=0
down=0
while [ $i -le 254 ];do
j=1
while [ $j -le 254 ];do
if ping -c1 -w1 $netid.$i.$j &>/dev/null;then
echo "the $netid.$i.$j is up"
let up++
else
echo "the $netid.$i.$j is down"
let down++
fi
let j++
done
let i++
done
echo "the up is $up"
echo "the down is $down"
编写脚本,打印九九乘法表
i=1
while [ "$i" -le "9" ];do
j=1
while [ "$j" -le "$i" ];do
echo -e "$j$i=`echo $i$j|bc` c"
let j++
done
echo ""
let i++
done
编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值
i=1
while true;do
NUM=$RANDOM
if [ "$i" -eq "1" ];then
MAX=$NUM
MIN=$NUM
else
if [ "$MAX" -lt "${num[$i]}" ];then
MAX=${num[$i]}
elif [ "$MIN" -gt "${num[$i]}" ];then
MIN=${num[$i]}
else
true
fi
fi
let i++
done
echo "num is : ${num[@]}"
echo "最大值MAX:$MAX 最小值MIN:$MIN"
编写脚本,实现打印国际象棋棋盘
i=1
D_COLOR=‘ 33[1;41m‘
S_COLOR=‘ 33[1;47m‘
AND_COLOR=‘ 33[0m‘
while [ $i -le 8 ];do
if [ "$[$i%2]" -eq "0" ];then
j=1
while [ $j -le 8 ];do
if [ "$[$j%2]" -eq "0" ];then
echo -e "${S_COLOR} ${AND_COLOR}c"
else
echo -e "${D_COLOR} ${AND_COLOR}c"
fi
let j++
done
else
j=1
while [ $j -le 8 ];do
if [ "$[$j%2]" -eq "0" ];then
echo -e "${D_COLOR} ${AND_COLOR}c"
else
echo -e "${S_COLOR} ${AND_COLOR}c"
fi
let j++
done
fi
echo ""
let i++
done
后续六个字符串:efbaf275cd、 4be9c40b8b、 44b2395c46、f8c8873ce0、 b902c16c8b、 ad865d2f63是通过对随机数变量RANDOM随机执行命令: echo $RANDOM|md5sum|cut –c1-10 后的结果,请破解这些字符串对应的RANDOM值
RAN=1
cat test.txt | while read CHESS;do
{ while true;do
MD=echo $RAN|md5sum|cut -c1-10
if [[ "$MD" == "$CHESS" ]];then
echo $RAN
break
else
let RAN++
fi
done }&
wait
done
每隔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
随机?成10以内的数字,实现猜字游戏,提??较?或?,相等则退出
n=$[$RANDOM%11]
time=1
while read -p "input a 0-10 number : " num ;do
if [ $num -gt $n ];then
echo "$num is greater"
elif [ $num -lt $n ];then
echo "$num is lower"
else
echo "guess right!"
break
fi
done
??件名做为参数,统计所有参数?件的总?数
sum=0
while [ $# -gt 0 ];do
lines=cat $1|wc -l
shift
sum=$[$sum+$lines]
done
echo $sum
??个以上的数字为参数,显?其中的最?值和最?值
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"
编写函数,实现OS的版本判断
os () {
if [sed -r "[email protected]*([0-9]+)..*@[email protected]"
/etc/centos-release` -eq 6 ];then
echo "os is 6"
else
echo "os is 7."
fi
}
os
编写函数,实现取出当前系统eth0的IP地址
eth0ip () {
ip=ifconfig eth0|grep netmask|tr -s ‘ ‘|cut -d ‘ ‘ -f3
echo ip=$ip
}
eth0ip
编写函数,实现打印绿?OK和红?FAILED
redgreen(){
echo -e "