shell脚本练习实例

第一题:
编写脚本,求100以内所有正奇数之和

sum=0
for i in {1..100}
do
        if [ $[ $i%2 ] -ne 0 ];then 
                sum=$[ $i+$sum ]
        else 
                continue
        fi
done
echo $sum

第二题:
编写一个脚本能打印9*9乘法表如下:

for i in {1..9}
do 
        for j in `seq 1 $i`
        do
                echo -ne "$j*$i=$((i*j))\t" 
        done
        echo 
done 

第三题:
随机生成一个10以内的数字,然后写一个猜数字的游戏,若输入的值比随机的值大,则提示大,再输入,若小,则提示小,若相等则提示win。

while true
do
        read -p "please input a number: " num
        if [ $num -gt $i ]
        then
                echo biger
        elif [ $num -lt $i ]
        then
                echo smaller
        else 
                echo WIN;break
        fi
done

第五题
编写一个创建用户的脚本,要求以下功能
a.提示用户输入希望创建的用户名(当用户超时8秒不输入,提示超时并退出)
b.检测用户名是否已存在,若用户不存在,则进入第c步骤,若用户存在则提示用户是否要对该用户设置密码,若用户输入yes、YES、y、Y时则跳至d步骤,如输入其他,或超时8秒均退出。
c.用户创建完成后提示用户是否要继续设置密码,若用户输入yes、YES、y、Y时则继续下一步,如输入其他,或超时8秒均退出。
d.接上一步,为用户设置密码,首先提示用户输入要设置的密码,密码的输入过程不可见。
e.对用户上一步所输入的密码进行长度检测,若少于5位(包含5位),则提示用户密码太短,请重新输入。
f.对用户在第d步所输入的密码进行复杂度检测,若所输入的内容与/usr/share/dict/words 字典中的某一行条目完全一致,则提示密码是一个常见单词,请重新输入。若第e/f两步的密码错误次数一共超过3次,则提示错误次数过多,并退出。
g.再一次让用户输入密码,若第二次输入的密码与在第d步输入的不一致,则提示用户两次密码不一致,请重新输入,则跳回至第d步。
h.d至g步骤全部通过后,为用户设置密码后,并提示密码已成功设置。最后正确退出。
以上的所有退出情况应给出不同的返回值。

#!/bin/bash
# ------------------------------------------
# Filename:    NO.5
# Revision:    1.0
# Date:        2018-02-10
# Author:      Hodge 
# Email:       123@163.com
# ------------------------------------------
# Copyright:   2018 Hodge
# License:     GPL
#in order to create user and change passwd
read -t3 -p "please input your username:" name
if [ $? -ne 0 ]
then 
        echo -e "\ntimeout"
        exit 1
fi
#check username exits
if `id $name &>/dev/null` 
then 
        echo the username has already exits
else
        echo you will create new user
        useradd $name
fi
#input passwd
read -p "weather input your passwd, input yes or no :" flag;
if [ `echo $?` -ne 0 ]
then
    echo  "timeout"
    exit 1
fi
case $flag in
yes|YES);;
Y|y);;
*) echo input error ;exit 2 
esac
while true 
do
        error_cnt=1
        #check the number of errors with this variable
        while true
        do  
                if [ $error_cnt -gt 3 ]
                then 
                        echo too many errors
                        exit 3
                fi
                read  -p'please input your passwd:' -s passwd;echo
                #check passwd length 
                if [ `echo $passwd|wc -L` -lt 6 ] 
                then 
                        echo passwd too short,please input again
                        (( error_cnt++ ))
                        continue
                fi
                #check passwd complexity
                if `grep -w $passwd /usr/share/dict/words &>/dev/null` 
                then
                        echo passwd is a simple word,input again
                        (( error_cnt++ ))
                        continue 
                else 
                        break
                fi
        done
        #check passwd2
        read -p'please your passwd again:' -s passwd2
        if [ $passwd != $passwd2 ]
        then
                echo -e  "\nthe two passwords do not match"
                continue
        else
                break
        fi
done
#set passwd 
echo $passwd |passwd --stdin $name&>/dev/null
echo -e "\nsuccessully set passwd"
exit

第五题:
编写一个录入个人信息的脚本/root/bin/information.sh,要求如下:
1.提示用户输入username(需对名字进行字符要求检查,用户名中不能模糊包含系统中任何一个已在存在用户名,如不能为helloroot1,123bin,ggntp2等。且要求username要以大写字母开头,长度至少5个字符。
2.提示用户输入性别,仅能输入male或female,输入其他的提示用户重试。
3.提示用户输入生日,格式必须为yyyy-mm-dd,并对日期的合法性进行检查,若输入2018-02-30等与现实不符的日期,则提示日期错误,请重试。
4.提示用户输入手机号,长度仅能11位,且务必以13x,18x,15x,17x开头,若错误则提示用户重新输入。
5.提示用户输入身份证号,长度可为18位,格式前17位均为数字。要求第7位至第14位所填的内容与第3步的生日符合。同时要求第17位所填的数字若为奇数,那么第2位所填写的性别应为male,反之偶数应为female。最后1位可为数字或x。
6.将以上输入的信息以:为分隔符,保存至/root/info.txt中,每次输入一个用户,会向下追加一行。
如Zhangsan:male:1999-12-20:13588291219:4101011999122030051

#!/bin/bash
# ------------------------------------------
# Filename:    NO.6
# Revision:    1.0
# Date:        2018-02-10
# Author:      Hodge 
# Email:       123@163.com
# ------------------------------------------
# Copyright:   2018 Hodge
# License:     GPL
# shell name :information.sh
read -p " please input usr_name :" usr_name
#----------------check usr_name-------------
flag_exit=1
#use this variable to check if the name already exits 
for i in ` cat /etc/passwd|cut -d: -f1`
do
        if `echo $usr_name |grep $i &>/dev/null`
        then
                echo modify
                flag_exit=0
        fi
        #if it doesn't meet requirements,exits
        if [ $flag_exit -eq 0 ]
        then 
                echo contains exiting usr_names 
                exit 1
        else
                continue
        fi
done
#check UPPER
if ` echo Centos|egrep "^[[:upper:]].{4,}" &>/dev/null `
then
        true
else
        echo the first should capitalized and the number of characters should more than 5
        exit 1
fi
#-------------usr_sex_input-------
flag_usr_sex=1;
while true
do
        read -p 'please input your usr_sex :' usr_sex
        case $usr_sex in 
                male|female) flag_usr_sex=0;;
                *)echo you should input male or female;;
        esac
        if [ $flag_usr_sex -eq 0 ]
        then
                echo $usr_sex
                break
        fi
done
#----------------birthday check------------
flag_birth=1
while true
do
        read -p'please input your birthday:' usr_birth
        year=`echo $usr_birth|cut -d- -f1`
        month=`echo $usr_birth|cut -d- -f2`
        day=`echo $usr_birth|cut -d- -f3`
        if ` cal $day $month $year &>/dev/null `
        then
                flag_birth=0
        fi
    if [ $flag_birth -eq 0 ]
    then
                echo your birthday is suitable
                break
        else
                echo your birthday is unsuitable
    fi
done
#-------------check phonenumber------------
flag_phone=1
while true 
do
        read -p'please input your phone number:' usr_phone
        if ` echo $usr_phone|egrep "^1[3857][0-9]{9}" &>/dev/null `
        then 
                flag_phone=0
        fi
        if [ $flag_phone -eq 0 ]
        then
                echo the phonenumber is suitable
                break
        else
                echo 'It is only 11 bits and start with 13x ,18x,15x,17x '
        fi
done
#--------check Id-------------
while true 
do 
        read -p'please input your ID:' usr_id
        #check the number of bits is only 18
        if ` echo $usr_id|egrep "^[0-9]{17}[0-9x]"&>/dev/null  `
        then
                usr_year=`echo $usr_id|cut -c 7-10`
                usr_month=` echo $usr_id|cut -c 11-12`
                usr_day=` echo $usr_id|cut -c 13-14`
                #check your birthdate
                if  [ $year=$usr_year -a $month=$usr_month -a $day=$usr_day ]
                then
                        echo your CARD_id is suitable to your birthdate
                else
                        echo your CARD_id is not match your birthdate
                        continue
                fi
                #check your sex
                id_sex=`echo $usr_id|cut -c 17`
        #       if      [ $(($id_sex%2 )) -eq 0 -a $usr_sex='female']
                if      [ $(($id_sex%2 )) -eq 0 -a $usr_sex='male' ]
                then
                        echo your sex_check is right
                        break
                elif [  $(($id_sex%2 )) -eq 1 -a $usr_sex='male' ]
                then
                        echo your sex_check is right,and your sex is male
                        break
                else
                        echo your sex_check is wrong ,and you shoule input again
                        continue 
                fi
        else
                echo "your id number is not match your requirements" 
        fi
done
echo $usr_name:$usr_sex:$usr_birth:$usr_phone:$usr_id >> /root/info.txt
echo "you have already write something to info.txt"

第六题:
利用第六题的脚本,输入20个用户信息。
编写一个抽奖系统的脚本 ,可对/root/info.txt文件中的手机号进行随机抽奖,将中奖的手机号打印出来,要求第4-7位显示为*,即135****1219

#!/bin/bash
# ------------------------------------------
# Filename:    NO.7
# Revision:    1.0
# Date:        2018-02-23
# Author:      Hodge 
# Email:       123@163.com
# ------------------------------------------
# Copyright:   2018 Hodge
# License:     GPL
rand=$[RANDOM%20+1]
#the range of random number is 1 to 20
sed -n "$rand p" /root/info.txt |cut -d: -f4|sed -r "s@(^[[:digit:]]{3}).*([[:digit:]]{4}$)@\1****\2@"
#replace with sed tools 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值