shell脚本练习实例
第一题:
编写脚本,求100以内所有正奇数之和
sum=0
for i in {1..100}
doif [ $[ $i%2 ] -ne 0 ];then sum=$[ $i+$sum ]else continuefi
done
echo $sum
第二题:
编写一个脚本能打印9*9乘法表如下:
for i in {1..9}
do for j in `seq 1 $i`doecho -ne "$j*$i=$((i*j))\t" doneecho
done
第三题:
随机生成一个10以内的数字,然后写一个猜数字的游戏,若输入的值比随机的值大,则提示大,再输入,若小,则提示小,若相等则提示win。
while true
doread -p "please input a number: " numif [ $num -gt $i ]thenecho bigerelif [ $num -lt $i ]thenecho smallerelse echo WIN;breakfi
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
elseecho you will create new useruseradd $name
fi
#input passwd
read -p "weather input your passwd, input yes or no :" flag;
if [ `echo $?` -ne 0 ]
thenecho "timeout"exit 1
fi
case $flag in
yes|YES);;
Y|y);;
*) echo input error ;exit 2
esac
while true
doerror_cnt=1#check the number of errors with this variablewhile truedo if [ $error_cnt -gt 3 ]then echo too many errorsexit 3firead -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++ ))continuefi#check passwd complexityif `grep -w $passwd /usr/share/dict/words &>/dev/null` thenecho passwd is a simple word,input again(( error_cnt++ ))continue else breakfidone#check passwd2read -p'please your passwd again:' -s passwd2if [ $passwd != $passwd2 ]thenecho -e "\nthe two passwords do not match"continueelsebreakfi
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`
doif `echo $usr_name |grep $i &>/dev/null`thenecho modifyflag_exit=0fi#if it doesn't meet requirements,exitsif [ $flag_exit -eq 0 ]then echo contains exiting usr_names exit 1elsecontinuefi
done
#check UPPER
if ` echo Centos|egrep "^[[:upper:]].{4,}" &>/dev/null `
thentrue
elseecho the first should capitalized and the number of characters should more than 5exit 1
fi
#-------------usr_sex_input-------
flag_usr_sex=1;
while true
doread -p 'please input your usr_sex :' usr_sexcase $usr_sex in male|female) flag_usr_sex=0;;*)echo you should input male or female;;esacif [ $flag_usr_sex -eq 0 ]thenecho $usr_sexbreakfi
done
#----------------birthday check------------
flag_birth=1
while true
doread -p'please input your birthday:' usr_birthyear=`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 `thenflag_birth=0fiif [ $flag_birth -eq 0 ]thenecho your birthday is suitablebreakelseecho your birthday is unsuitablefi
done
#-------------check phonenumber------------
flag_phone=1
while true
doread -p'please input your phone number:' usr_phoneif ` echo $usr_phone|egrep "^1[3857][0-9]{9}" &>/dev/null `then flag_phone=0fiif [ $flag_phone -eq 0 ]thenecho the phonenumber is suitablebreakelseecho '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 18if ` echo $usr_id|egrep "^[0-9]{17}[0-9x]"&>/dev/null `thenusr_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 birthdateif [ $year=$usr_year -a $month=$usr_month -a $day=$usr_day ]thenecho your CARD_id is suitable to your birthdateelseecho your CARD_id is not match your birthdatecontinuefi#check your sexid_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' ]thenecho your sex_check is rightbreakelif [ $(($id_sex%2 )) -eq 1 -a $usr_sex='male' ]thenecho your sex_check is right,and your sex is malebreakelseecho your sex_check is wrong ,and you shoule input againcontinue fielseecho "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
shell脚本练习实例相关推荐
- shell脚本编程 实例讲解
shell脚本编程 实例讲解 1.键盘输入三个数字,按照从大到小的顺序输出! 排序题 a b c 2 10 9 a=2 b=10 v=9 第一步: 两两相互进行比较,比较三次 第二步:不论谁大谁小,最 ...
- Linux shell脚本编程实例
1.编写一个shell脚本,它把第二个位置参数及其以后的各个参数指定的文件拷贝到第一个位置参数指定的目录中. #!/bin/bashdir=$1 shift #将位置参数移动一位(即原先的第二个位置参 ...
- datetime报错 sql脚本_Linux中Mysql数据库备份shell脚本编写实例
学了段时间的Linux,也学习了shell脚本编写的基本命令与语法,现做一个综合案例来详细讲解. 要求:1).每天凌晨备份数据库shaoxiao到/data/backup/db中 2).备份开始和备份 ...
- 超硬核,11个非常实用的 Python 和 Shell 脚本实例
原文地址: https://developer.51cto.com/article/712305.html Python 脚本部分实例:企业微信告警.FTP 客户端.SSH 客户端.Saltstack ...
- 什么是Shell?Shell脚本基础知识详细介绍
这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...
- Android脱离USB执行Shell脚本的方法
前言: 安卓自动化测试过程中,经常需要测试机脱离 Usb执行相关 shell 脚本,之前在网上看了很多的例子,没有真正可以直接断开 USB执行 shell 脚本的实例,所以一直也没太搞明白,最近有时间 ...
- 详解Linux交互式shell脚本中创建对话框实例教程
详解Linux交互式shell脚本中创建对话框实例教程 本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一 ...
- 用shell脚本监控进程是否存在 不存在则启动的实例
用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/sh ps -fe|grep processString |grep -v grep if [ $? -ne 0 ] ...
- shell脚本执行php文件_分享两个shell脚本实例--批量生成随机字符文件名和批量改名...
概述 在计算机科学中,for循环(英语:for loop)是一种编程语言的迭代陈述,能够让程式码反复的执行. 它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或是循环变数.这使得 ...
- linux shell命令行及脚本编程实例详解_超全整理!这些Shell编程必备知识你都掌握了吗?...
正文最近很多粉丝咨询我,被问到了一些Shell编程的问题,看看大家能否答出来: 1.shell脚本千千万,不知道从哪入手 2.没经验缺方法,面试通不过.做事没头绪 3.野路子.没人教自动化,做了几年基 ...
最新文章
- matplotlib命令与格式:图像(figure)与子区域(axes)布局与规划
- nodejs获取当前url和url参数值
- java 参数 exception_java – 在异常的参数中使用泛型
- C#中使用代码动态改变配置文件信息
- AS插件-Android Parcelable code generator.
- 被美国主流投资平台看好,虎牙缘何能在上市大军中脱颖而出?
- MongoDB 教程七: MongoDB和PHP结合开发
- python分为哪几个模块_干货:入门Python重点学哪几个模块才能成为高手?
- IFIX上位机网络测试画面
- 如何探测局域网中某台主机是否开机_「渲染」3Dmax 分布式渲染,局域网渲染,联机渲染教程...
- 基于Gsoap 的ONVIF C++ 库
- 高德地图只显示某个省份
- 思科交换机基本配置命令
- linux中find查找文件和查找文件内容
- 【STM32基础】第四篇、控制PWM占空比
- 【Linux】如何查找命令及历史记录history
- 如何分辨一个公司是玩你还是爱你?
- ES6笔记(完整详细版)
- Java面试题库(一)
- python计算机二级考试时间