码迷,mamicode.com
首页 > 系统相关 > 详细

SHELL脚本练习

时间:2018-05-08 16:26:36      阅读:865      评论:0      收藏:0      [点我收藏+]

标签:脚本练习

1、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参 数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户 的id号等信息
#!/bin/bash
read -p "please input your username: " n
useradd $n &> /dev/null
if [ "$?" == "0" ]
then
echo "user info: cat /etc/passwd | grep -E "\<^$n\>""
else
echo "user already exists"
fi

2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的 是yes还是no,或是其它信息
#!/bin/bash
read -p "please input yes or no: " n
case $n in
[Yy][Ee][sS]|[Yy])
echo "you input is yes"
;;
[Nn][oO]|[Nn])
echo "you input is no"
;;
*)
echo "you input is other info"
;;
esac

3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型 (普通,目录,链接,其它文件类型)

#!/bin/bash
read -p "please input file path: " n
if [ -f "$n" ]
        then
                echo "$n is 普通文件";
        elif [ -b "$n" ]
        then
                echo "$n is 块设备文件";
        elif [ -c "$n" ]
        then
                echo "$n is 字符设备文件";
        elif [ -h "$n" ]
        then
                echo "$n is 符号链接文件"
        elif [ -d "$n" ]
        then
                echo "$n is 目录文件";
        elif [ -p "$n" ]
        then
                echo "$n is 管道文件";
        elif [ -s "$n" ]
        then
                echo "$n is 套接字文件";
        else
                echo "$n unknown"
fi

4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数

#!/bin/bash
read -p "please input your charactor: " n
m=`echo $n | sed -n -r ‘s/[0-9]//gp‘`
if [ ! "n$m" == "n" ]
        then
                echo "please restart input"     
        elif [ "$n" == "0" ]
                then
                        echo "your input is 0"
else
        echo "your input is 正整数"
fi

5、判断/var/目录下所有文件的类型
#!/bin/bash
for n in /var/*
do
if [ -f "$n" ]
then
echo "$n is 普通文件";
elif [ -b "$n" ]
then
echo "$n is 块设备文件";
elif [ -c "$n" ]
then
echo "$n is 字符设备文件";
elif [ -h "$n" ]
then
echo "$n is 符号链接文件"
elif [ -d "$n" ]
then
echo "$n is 目录文件";
elif [ -p "$n" ]
then
echo "$n is 管道文件";
elif [ -s "$n" ]
then
echo "$n is 套接字文件";
else
echo "$n unknown"
fi
done

6、添加10个用户user1-user10,密码为指定字符

#!/bin/bash
    for n in user{1..10}
            do
            useradd $n
            if [ "$?" = "0" ]
                    then
                    echo "12345678" | passwd --stdin $n
            else
                    echo "用户存在"
            fi
    done 

7、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件, 以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start

#!/bin/bash
for?i?in?/etc/rc.d/rc3.d/[SK]*?;do
if?[?$(basename?$i?|?cut?-c1)?==?"K"?]?;then
????echo?"`basename?$i`?stop"
else
????echo?"`basename?$i`?start"
fi
done

8、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
#!/bin/bash
read -p "please input num: " n
for (( i=1;i<="$n";i++ ))
do
sum=$[$sum+$i]
done
echo $sum

9、计算100以内所有能被3整除的整数之和
#!/bin/bash
n=0
for i in {1..100};
do
if [ $[$i%3] -eq 0 ]
then
n=$[$n+$i]
fi
done
echo "sum=$n"

10、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
#!/bin/bash
f=mktemp /tmp/ping.XXXXXXX
read -p "please input you want to test: " ip
n=echo $ip | cut -d‘.‘ -f1-3
for i in {1..255}
do
{ m=$n.$i
ping -w1 -c1 $m &> /dev/null && echo "$m is up!" && echo $m >> $f
}&
done

11、打印九九乘法表
#!/bin/bash
for i in {1..9}
do
for j in $(seq 1 $i) ;do
echo -ne "${j}x${i}=$[i*j]\t"
done
echo
done

12、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字 母,如:1AbCdeFgH.html
#!/bin/bash
for i in {1..10}
do
n=openssl rand -base64 64 | tr -dc ‘[a-zA-Z]‘ | head -c8
touch /testdir/$i$n.html
done

13、打印等腰三角形
#!/bin/bash
until read -p "place input number " line ;do
if [[ "$line" =~ [0-9]+ ]] ;then
continue
fi
done
for i in seq 1 $line ;do
space=$[line-i]
star=$[2i-1]
for j in seq 1 $space ; do
echo -e " \c"
done
for k in seq 1 $star; do
clocr=$[RANDOM%7+31]
echo -e "\e[1;${clocr}m
\e[0m\c"
done
echo
done

14、编写脚本,求100以内所有正奇数之和
#!/bin/bash
n=0
for i in {1..100};
do
if [ $[$i%2] -eq 1 ]
then
n=$[$n+$i]
fi
done
echo "sum=$n"

15、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机 在线状态,并统计在线和离线主机各多少
#!/bin/bash
f=mktemp /tmp/ping.XXXXXXX
read -p "please input you want to test: " ip
n=echo $ip | cut -d‘.‘ -f1-3
for i in {1..255}
do
{ m=$n.$i
ping -w1 -c1 $m &> /dev/null && { echo "$m is up!"; echo $m >> $f; }
}&
done
g=cat "$f" | wc -l
j=$[255-$g]
echo "up num: $g; down num: $j"

16、编写脚本,打印九九乘法表
#!/bin/bash
for i in {1..9}
do
for j in $(seq 1 $i) ;do
echo -ne "${j}x${i}=$[i*j]\t"
done
echo
done

17、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显 示其中的最大值和最小值
#!/bin/bash
let i=0,min=max=$RANDOM
echo "$min "
while [ $i -lt 9 ];do
ran=$RANDOM
echo "$ran "
if [ $ran -ge $max ];then
let max=ran
fi
if [ $ran -le $min ];then
let min=ran
fi
let i+=1
done
echo "max is: $max ,min is: $min"

18、编写脚本,实现打印国际象棋棋盘
#!/bin/bash
let x=y=1
h=8
while [ $x -le $h ];do
while [ $y -le $h ];do
if [ $[(x+y)%2] -eq 0 ] ;then
echo -en "\033[47m \033[0m"
else
echo -en "\033[40m \033[0m"
fi
let y+=1
done
echo
let x+=1,y=1
done

19、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机 执行命令: echo $RANDOM|md5sum|cut –c1-10 后的结果,请破解这些 字符串对应的RANDOM值
#!/bin/bash
ch=(efbaf275cd 4be9c40b8b 44b2395c46 f8c8873ce0 b902c16c8b ad865d2f63)
for num in seq 0 65535;do
chnum=echo $num | md5sum | cut -c 1-10
for n in ${ch[*]}; do
if [ "$chnum" == "$n" ];then
echo "$n --> $num"
fi
done
done

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

#!/bin/bash
username=cent
{
while?true?;do
????if?who?|?egrep?"^\b$username\b"?&>?/dev/null?;then
????????who?|?egrep?"^\b$username\b"?>>?/var/log/login.log
????????echo?"$username?已经登录"
????????echo?"fuck?,go?out?my?system"?|?write?$username
????else
????????echo?"$username?已经下线"
????fi
????sleep?3
done
}

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

#!bin/bash
lim=10
let?key=$RANDOM%lim
read?-p??"please?input?a?number?less?than?$lim:?"?num
until?false;do
????if?[?$num?-le?$lim?&>?/dev/null??]?&&?[?$num?-ge?0??&>?/dev/null?]?;then
????????if?[?$num?-lt?$key?];then
????????????read?-p?"is?small,please?input?again:?"?num
????????elif?[?$num?-gt?$key?];then
????????????read?-p?"is?big,please?input?again:?"?num
????????else
????????????echo?"you?are?right,you?are?very?clever"
????????????break
????????fi
????else
????????read?-p??"error,please?input?a?number?less?than?ten:?"?num
????fi
done

22、用文件名做为参数,统计所有参数文件的总行数
#!/bin/bash
let z=0
while read -p "please input file path: " n
do
m=cat "$n" | wc -l
z=$[$z+$m]
echo "sum line: $z"
if (("$z" > "5000"))
then
echo "cache will be full"
break
fi
done

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

    #!/bin/bash
    while read -p "please input two number: " n m
    do
            if (("$n" > "$m"))
                    then
                    max=$[$n]
                    min=$[$m]
                    echo "max=$max; min=$min"
            elif (("$n" < "$m"))
                    then
                    max=$[$m]
                    min=$[$n]
                    echo "max=$max; min=$min"       
            else
                    echo "two num equal"
            fi
done

SHELL脚本练习

标签:脚本练习

原文地址:http://blog.51cto.com/11010461/2113985

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!