四、shell编程练习题(1-20)
1、写一个脚本 脚本可以接受一个以上的文件路径作为参数; 显示每个文件所拥的行数; 显示本次共对多少个文件执行了行数统计。 #!/bin/bash forfilein$*;do lines=`wc-l$file|cut-d''-f1` echo"$filehas$lineslines." done echo"$#files." 运行结果: [root@mylinuxtest]#sh1.sh/etc/inittab /etc/inittabhas26lines. 1files. 2、分别计算100以内所有偶数之和和奇数之和 #!/bin/bash eve=0 odd=0 for((i=1;i<=100;i++));do if`leti%2`;then letodd=odd+i else leteve=eve+i fi done echo"奇数和为$odd,偶数和为$eve" 运行结果: [root@mylinuxtest]#sh2.sh 奇数和为2500,偶数和为2550 3、计算当前系统上所有用户的ID之和 #!/bin/bash declare-iidsum=0 foriin`cut-d:-f3/etc//passwd`;do#由于51cto关键字限制,去掉一个'/' letidsum=idsum+i done echo$idsum 运行结果: [root@mylinuxtest]#sh3.sh 68489 4、新建10个用户tuser401-tuser410,并求他们的ID之和 #!/bin/bash declare-iidsum=0 foriin{401..410};do useraddtuser$i userID=`id-utuser$i` letidsum=idsum+userID done echo"IDsum:$idsum" 运行结果: [root@mylinuxtest]#sh4.sh IDsum:5045 [root@mylinuxtest]#tail/etc//passwd#由于51cto关键字限制,去掉一个'/' tuser401:x:500:500::/home/tuser401:/bin/bash tuser402:x:501:501::/home/tuser402:/bin/bash tuser403:x:502:502::/home/tuser403:/bin/bash tuser404:x:503:503::/home/tuser404:/bin/bash tuser405:x:504:504::/home/tuser405:/bin/bash tuser406:x:505:505::/home/tuser406:/bin/bash tuser407:x:506:506::/home/tuser407:/bin/bash tuser408:x:507:507::/home/tuser408:/bin/bash tuser409:x:508:508::/home/tuser409:/bin/bash tuser410:x:509:509::/home/tuser410:/bin/bash 5、写一个脚本 创建用户tuser501-tuser510; 创建目录/tmp/dir-当前日期时间; 在/tmp/dir-当前日期时间,目录中创建10个空文件file101-file110 将file101的属主改为tuser501,依次类推,一直将file110的属主改为tuser510。 #!/bin/bash mkdir-p/tmp/dir/`date+%Y-%m-%d` cd/tmp/dir/`date+%Y-%m-%d` foriin{01..10};do useraddtuser5$i mkdirfile1$i chowntuser5$ifile1$i done 运行结果: [root@mylinuxtest]#sh5.sh [root@mylinuxtest]#tail/etc//passwd#由于51cto关键字限制,去掉一个'/' tuser501:x:510:510::/home/tuser501:/bin/bash tuser502:x:511:511::/home/tuser502:/bin/bash tuser503:x:512:512::/home/tuser503:/bin/bash tuser504:x:513:513::/home/tuser504:/bin/bash tuser505:x:514:514::/home/tuser505:/bin/bash tuser506:x:515:515::/home/tuser506:/bin/bash tuser507:x:516:516::/home/tuser507:/bin/bash tuser508:x:517:517::/home/tuser508:/bin/bash tuser509:x:518:518::/home/tuser509:/bin/bash tuser510:x:519:519::/home/tuser510:/bin/bash [root@mylinuxtest]#ls-l/tmp/dir/2017-07-03/ 总用量40 drwxr-xr-x2tuser501root40967月314:40file101 drwxr-xr-x2tuser502root40967月314:40file102 drwxr-xr-x2tuser503root40967月314:40file103 drwxr-xr-x2tuser504root40967月314:40file104 drwxr-xr-x2tuser505root40967月314:40file105 drwxr-xr-x2tuser506root40967月314:40file106 drwxr-xr-x2tuser507root40967月314:40file107 drwxr-xr-x2tuser508root40967月314:40file108 drwxr-xr-x2tuser509root40967月314:40file109 drwxr-xr-x2tuser510root40967月314:40file110 6、分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/inittab文件中以#开头的行的行数和空白行数 #!/bin/bash forfilein/etc/rc.d/rc.sysinit/etc/rc.d/init.d/functions/etc/inittab;do echo"Thelinescontain#in$fileis`grep-E"^#"$file|wc-l`." echo"Thespacelinesin$fileis`grep-E"^[[:space:]]*$"$file|wc-l`." done 运行结果: [root@mylinuxtest]#sh6.sh Thelinescontain#in/etc/rc.d/rc.sysinitis44. Thespacelinesin/etc/rc.d/rc.sysinitis103. Thelinescontain#in/etc/rc.d/init.d/functionsis43. Thespacelinesin/etc/rc.d/init.d/functionsis106. Thelinescontain#in/etc/inittabis25. Thespacelinesin/etc/inittabis0. 7、显示当前系统上所有默认shell为bash的用户的用户名、UID及其所有此类用户的UID之和 #!/bin/bash grep"/bin/bash$"/etc//passwd|cut-d:-f1,3#由于51cto关键字限制,去掉一个'/' declare-isum=0 foruserIDin`grep"/bin/bash$"/etc//passwd|cut-d:-f3`;do #由于51cto关键字限制,去掉一个'/' letsum=sum+userID done echo"$sum" 运行结果: [root@mylinuxtest]#sh7.sh root:0 0 8、显示当前系统上有附加组的用户的用户名;并统计共有多少个此类用户 [root@mylinuxtest]#egrep'[^:]$'/etc/group|cut-d:-f4|sort-u|egrep-o'[[:alnum:]]*'|sort-u 9、接受一个参数,这个参数是用户名;如果此用户存在,则显示其ID号 #!/bin/bash read-p"请输入用户名:"username ifid$username&>/dev/null;then echo"$username的id为`grep"^&;$username&;"/etc//passwd|cut-d:-f3`" else#由于51cto关键字限制,去掉一个'/' echo"无此用户" fi 运行结果: [root@mylinuxtest]#sh9.sh 请输入用户名:mysql mysql的id为496 [root@mylinuxtest]#sh9.sh 请输入用户名:123 无此用户 10、通过命令行传递两个整数参数给脚本,脚本可以返回其大者 #!/bin/bash read-p"请输入两个整数:"a1a2 if[$a1-gt$a2];then echo$a1 else echo$a2 fi 运行结果: [root@mylinuxtest]#sh10.sh 请输入两个整数:1116 16 [root@mylinuxtest]#sh10.sh 请输入两个整数:7816 78 11、通过命令行传递任意个整数给脚本,脚本可以返回其大者 #!/bin/bash declare-inum=0 foriin$@;do if[$i-gt$num];then letnum=i fi done echo"最大数为:$num" 运行结果: [root@mylinuxtest]#sh11.sh797899131332132 最大数为:7978 [root@mylinuxtest]#sh11.sh7899131332132 最大数为:3321 12、通过命令行给定一个文件路径,而后判断:如果此文件中存在空白行,则显示其空白行的总数;否则,则显示无空白行 #!/bin/bash ifgrep"^[[:space:]]*$"$1&>/dev/null;then echo"$1has$(grep"^[[:space:]]*$"$1|wc-l)blanklines." else echo"Noblanklines." fi 运行结果: [root@mylinuxtest]#sh12.sh/etc//passwd#由于51cto关键字限制,去掉一个'/' Noblanklines. [root@mylinuxtest]#sh12.sh/etc/inittab Noblanklines. [root@mylinuxtest]#sh12.sh/etc/rc.sysinit /etc/rc.sysinithas103blanklines. 13、传递一个参数给脚本: 如果参数为quit,则显示说你要退出; 如果参数为yes,则显示说你要继续; 其它任意参数,则说无法识别。 #!/bin/bash case$1in quit|Q) echo"退出程序" ;; yes|Y) echo"继续" ;; *) echo"无法识别" ;; esac 运行结果: [root@mylinuxtest]#sh13.sh 无法识别 [root@mylinuxtest]#sh13.shquit 退出程序 [root@mylinuxtest]#sh13.shyes 继续 14、传递一个用户名给脚本: 如果此用户的id号为0,则显示说这是管理员; 如果此用户的id号大于等于500,则显示说这是普通用户 否则,则说这是系统用户; #!/bin/bash ifid$1&>/dev/null;then userid=`grep"^&;$1&;"/etc//passwd|cut-d:-f3`#由于51cto关键字限制,去掉一个'/' if[$userid-eq0];then echo"$1是管理员." elif[$userid-ge500];then echo"$1是普通用户." else echo"$1是系统用户." fi else echo“无此用户.” fi 运行结果: [root@mylinuxtest]#sh14.shmysql mysql是系统用户. [root@mylinuxtest]# [root@mylinuxtest]#sh14.shmysql mysql是系统用户. [root@mylinuxtest]#sh14.shroot root是管理员. [root@mylinuxtest]#sh14.shnfsnobody nfsnobody是普通用户. [root@mylinuxtest]#sh14.shnfsnobo “无此用户.” 15、给定一个用户,如果其shell为/bin/bash且其ID号大于等于500,则说这是一个可登录普通用户;否则,则显示其为非登录用户或管理员。 #!/bin/bash ifid$1&>/dev/null;then userid=`grep"^&;$1&;"/etc//passwd|cut-d:-f3`#由于51cto关键字限制,去掉一个'/' usershell=`grep"^&;$1&;"/etc//passwd|cut-d:-f7`#由于51cto关键字限制,去掉一个'/' if[$userid-ge500]&&["$usershell"=="/bin/bash"];then echo"$1是可登录普通用户" else echo"$1是非登陆用户或管理员" fi else echo"无此用户." fi 运行结果: [root@mylinuxtest]#sh15.shroot root是非登陆用户或管理员 [root@mylinuxtest]#sh15.shmysql mysql是非登陆用户或管理员 [root@mylinuxtest]#sh15.shnfsnobody nfsnobody是可登录普通用户 [root@mylinuxtest]#sh15.shnfsnobo 无此用户. 16、写一个脚本,如果某用户不存在,就添加 #!/bin/bash ifid$1&>/dev/null;then echo"$1已存在." else useradd$1 echo"添加用户$1." fi 运行结果: [root@mylinuxtest]#sh16.shmysql mysql已存在. [root@mylinuxtest]#sh16.shmylinux 添加用户mylinux. [root@mylinuxtest]#tail-1/etc//passwd#由于51cto关键字限制,去掉一个'/' mylinux:x:500:500::/home/mylinux:/bin/bash 17、添加10个用户:tuser501-tuser510;如果用户不存在,才添加;如果存在,则显示已经有此用户;显示一共添加了多少个用户。 #!/bin/bash declare-inum=0 foriin{501..510};do ifidtuser$i&>/dev/null;then echo"tuser$i已存在" else useraddtuser$i echo"添加用户tuser$i" letnum++ fi done echo"添加$num个用户." 运行结果: [root@mylinuxtest]#sh17.sh 添加用户tuser501 添加用户tuser502 添加用户tuser503 添加用户tuser504 添加用户tuser505 添加用户tuser506 添加用户tuser507 添加用户tuser508 添加用户tuser509 添加用户tuser510 添加10个用户. [root@mylinuxtest]#sh17.sh tuser501已存在 tuser502已存在 tuser503已存在 tuser504已存在 tuser505已存在 tuser506已存在 tuser507已存在 tuser508已存在 tuser509已存在 tuser510已存在 添加0个用户. 18、添加10个用户:tuser601-tuser610;如果用户不存在,才添加,并以绿色显示添加成功;如果存在,则以红色显示已经有此用户;显示一共添加了多少个用户。 #!/bin/bash # declare-icount=0 foriin{601..610};do ifidtuser$i&>/dev/null;then echo-e" 33[31mtuser$i 33[0mexists." else useraddtuser$i echo-e"adduser 33[32mtuser$i 33[0msuccessfully." letcount++ fi done echo"Totaladd$countusers." 运行结果: [root@mylinuxtest]#sh18.sh addusertuser601successfully. addusertuser602successfully. addusertuser603successfully. addusertuser604successfully. addusertuser605successfully. addusertuser606successfully. addusertuser607successfully. addusertuser608successfully. addusertuser609successfully. addusertuser610successfully. Totaladd10users. [root@mylinuxtest]#sh18.sh tuser601exists. tuser602exists. tuser603exists. tuser604exists. tuser605exists. tuser606exists. tuser607exists. tuser608exists. tuser609exists. tuser610exists. Totaladd0users. 19、传递用户名给脚本 判断此用户的shell是否为/bin/bash,如果是,则显示此用户为basher 否则,则显示此用户为非basher #!/bin/bash # userShell=`grep"^$1&;"/etc//passwd|cut-d:-f7`#由于51cto关键字限制,去掉一个'/' if["$userShell"=='/bin/bash'];then echo"basher" else echo"notbasher" fi 运行结果: [root@mylinuxtest]#sh19.shmysql notbasher [root@mylinuxtest]#sh19.shmylinux basher 20、给定一个文件路径 判断此文件是否存在;不存在,则说明文件不存,并直接结束脚本; 如果文件是否普通文件,则显示为“regular file”; 如果文件是目录,则显示为“directory”; 如果文件是链接文件,则显示为“Symbolic file"; 否则,则显示为“unknown type.” #!/bin/bash if[!-e$1];then echo"filenotexit." exit5 fi if[-L$1];then echo"Symbolicfile." elif[-d$1];then echo"directory." elif[-f$1];then echo"regularfile." else echo"unknowntype." fi 运行结果: [root@mylinuxtest]#sh20.sh/etc/ directory. [root@mylinuxtest]#sh20.sh/etc/rc.d/rc1.d/K92iptables Symbolicfile. [root@mylinuxtest]#sh20.sh12312 filenotexit. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |