文档章节

037-shell习题02

侠客行之石头
 侠客行之石头
发布于 2017/09/04 09:07
字数 1526
阅读 66
收藏 0

1. 计算“/etc”目录中所有“*.conf”形式的配置文件所占用的总空间大小。

[root@Carlton scripts]# vim mmm.sh 
#!/bin/bash
#cd /etc
Number=$(ls -l $(find /etc -type f -name *.conf)| awk '{print $5}' )
Total=0
for i in $Number
do
Total=`expr $Total + $i`
done
echo "The Total size is $Total kb"

"mmm.sh" 10L, 184C written                                                                                                                                                                                               
[root@Carlton scripts]# ./mmm.sh 
The Total size is 362579 kb

2.由用户从键盘输入一个大于1的整数(如50),并计算从1到该数之间各整数的和。

#/bin/bash
read -p "Please input one number(>1): " UP
sum =0
i=1
#while [ $UP -ge $i ]
#do
  # sum=$[ $sum + $UP]
  # UP=$[$UP-1]
   #i=`expr $i + 1`
#done
while [ $i -le $UP ]
do
   sum=`expr $sum + $i`
   i=`expr $i +  1`
done
 
echo "The sum of 1- $UP is : $sum "

"lll.sh" 20L, 303C written                                                                                                                                                                                               
[root@Carlton scripts]# ./lll.sh 
Please input one number(>1): 8
sum: =0: No such file or directory
The sum of 1- 8 is : 36 
上面两种方法都可以

3. 批量添加20个系统用户账号,用户名称依次为“stu1”、“stu2”、“stu3”、……“stu20”,各用户的初始密码均设置为“123456”。继续编写一个批量删除用户的脚本程序,将添加的20个用户删除。

[root@Carlton September]# ./addstu20.sh 

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456
[root@Carlton September]# grep "stu" /etc/passwd
stu1:x:504:504::/home/stu1:/bin/bash
stu2:x:505:505::/home/stu2:/bin/bash
stu3:x:506:506::/home/stu3:/bin/bash
stu4:x:507:507::/home/stu4:/bin/bash
stu5:x:508:508::/home/stu5:/bin/bash
stu6:x:509:509::/home/stu6:/bin/bash
stu7:x:510:510::/home/stu7:/bin/bash
stu8:x:511:511::/home/stu8:/bin/bash
stu9:x:512:512::/home/stu9:/bin/bash
stu10:x:513:513::/home/stu10:/bin/bash
stu11:x:514:514::/home/stu11:/bin/bash
stu12:x:515:515::/home/stu12:/bin/bash
stu13:x:516:516::/home/stu13:/bin/bash
stu14:x:517:517::/home/stu14:/bin/bash
stu15:x:518:518::/home/stu15:/bin/bash
stu16:x:519:519::/home/stu16:/bin/bash
stu17:x:520:520::/home/stu17:/bin/bash
stu18:x:521:521::/home/stu18:/bin/bash
stu19:x:522:522::/home/stu19:/bin/bash
stu20:x:523:523::/home/stu20:/bin/bash
[root@Carlton September]# ./deluser20 
[root@Carlton September]# grep "stu" /etc/passwd
[root@Carlton September]# cat addstu20.sh 
#!/bin/bash
i=1
while [ $i -le 20 ]
do
  useradd stu$i
  echo "123456" |passwd --stdin  stu$i
 i=`expr $i + 1 ` 

done
[root@Carlton September]# cat deluser20 
#!/bin/bash
i=1
while [ $i -le 20 ]
do
  userdel -r stu$i
  i=`expr $i + 1` 

done
cat /etc/passwd |grep stu

4.由用户从键盘输入一个字符,并判断该字符是否为字母、数字或者其他字符,并输出相应的提示信息。

[root@Carlton September]# cat information.sh 
#!/bin/bash
read -p "Please input any words in this screen : " XX
case $XX in
[a-z]|[A-Z])
   echo "this is english words"
;;
[0-9])

   echo "NUmber"
;;
*)
  echo "It is not number or words"
esac


[root@Carlton September]# ./information.sh 
Please input any words in this screen : 1
NUmber
[root@Carlton September]# ./information.sh 
Please input any words in this screen : e
this is english words
[root@Carlton September]# ./information.sh 
Please input any words in this screen : ^[[17~
It is not number or words

5.编写一个shell程序,计算多个整数值的和,需要计算的各个数值由用户在执行脚本时作为命令行参数给出。

[root@Carlton September]# vim kkk.sh
#!/bin/bash
Result=0
while [ $# -gt 0 ]             $#传参的个数,$1第一个传参的个数
do

    Result=`expr $Result + $1`   
    shift
done
echo "the sum is "$Result""

~
"kkk.sh" 9L, 118C written                                                                                                                                                                                                
[root@Carlton September]# ./kkk.sh  3 3 
the sum is 6
[root@Carlton September]# sh -x kkk.sh 34 55
+ Result=0
+ '[' 2 -gt 0 ']'
++ expr 0 + 34
+ Result=34
+ shift
+ '[' 1 -gt 0 ']'
++ expr 34 + 55
+ Result=89
+ shift
+ '[' 0 -gt 0 ']'
+ echo 'the sum is 89'
the sum is 89

6. 计算任意整数之和

[root@Carlton September]# ./kkk.sh  23 44
the sum is 67
[root@Carlton September]# cat kkk.sh 
#!/bin/bash
Result=0
while [ $# -gt 0 ]
do

    Result=`expr $Result + $1`
    shift
done
echo "the sum is "$Result""

7.在脚本中定义一个help函数,当用户输入的脚本参数不是“start”或“stop”时,加载该函数并给出关于命令用法的帮助信息,否则给出对应的提示信息

[root@Carlton September]# vim jjj.sh
#!/bin/bash
help() {
      echo "Usage: "$0" start|stop"
}
case "$1" in
   start)
      echo "Starting ..."
     ;;
stop)
   echo "stop..."
;;
*)
     help
esac


"jjj.sh" 15L, 162C written                                                                                                                                                                                               
[root@Carlton September]# ./jjj.sh 
Usage: ./jjj.sh start|stop
[root@Carlton September]# ./jjj.sh stop
stop...
[root@Carlton September]# ./jjj.sh sart
Usage: ./jjj.sh start|stop
[root@Carlton September]# ./jjj.sh start
Starting ...

8. 在脚本中定义一个加法函数,用于计算两个数的和,并调用该函数分别计算12+34、56+789的和。

[root@Carlton September]# vim uuu.sh
#!/bin/bash
adder() {
      echo `expr $1 + $2`
}
adder 12 34
adder 56 789

"uuu.sh" [New] 7L, 76C written                                                                                                                                                                                           
[root@Carlton September]# chmod +x uuu.sh 
[root@Carlton September]# ./uuu.sh 
46
845

9.  删除系统中的stu1~stu20各用户账号,但stu8、stu18除外。

#!/bin/bash
i=1
while [ $i -le 20 ]
do
    if [ $i -eq 8 ] || [ $i -eq 18 ] ; then
       let i++
       continue
    fi
    userdel -r stu$i
    let i++
done

"ggg.sh" [New] 12L, 160C written                                                                                                                                                                                         
[root@Carlton September]# chmod +x ggg.sh 
[root@Carlton September]# ./ggg.sh 
userdel: user 'stu1' does not exist
userdel: user 'stu2' does not exist
userdel: user 'stu3' does not exist
userdel: user 'stu4' does not exist
userdel: user 'stu5' does not exist
userdel: user 'stu6' does not exist
userdel: user 'stu7' does not exist
userdel: user 'stu9' does not exist
userdel: user 'stu10' does not exist
userdel: user 'stu11' does not exist
userdel: user 'stu12' does not exist
userdel: user 'stu13' does not exist
userdel: user 'stu14' does not exist
userdel: user 'stu15' does not exist
userdel: user 'stu16' does not exist
userdel: user 'stu17' does not exist
userdel: user 'stu19' does not exist
userdel: user 'stu20' does not exist
[root@Carlton September]# ./addstu20.sh 

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456

passwd read,is 123456
[root@Carlton September]# ./ggg.sh 
[root@Carlton September]# grep "stu" /etc/passwd
stu8:x:511:511::/home/stu8:/bin/bash
stu18:x:521:521::/home/stu18:/bin/bash
[root@Carlton September]# 

10.循环提示用户输入字符串,并将每次输入的内容保存到临时文件“/tmp/input.txt”中,当用户输入“END”字符串时退出循环体,并统计出input.txt文件中的行数、单词数、字节数等信息,统计完后删除临时文件。

[root@Carlton September]# vim fff.sh
#!/bin/bash
while true
do
      read -p "Input a string: " STR
      echo $STR >> /tmp/input.txt
      if [ "$STR" = "END" ] ;  then
           break
      fi
done
wc /tmp/input.txt
rm -f /tmp/input.txt

"fff.sh" [New] 12L, 204C written                                                                                                                                                                                         
[root@Carlton September]# chmod +x fff.sh 
[root@Carlton September]# ./fff.sh 
Input a string: 123
Input a string: 213
Input a string: 3254
Input a string: 3465
Input a string: sadfse
Input a string: END
 6  6 29 /tmp/input.txt

 

© 著作权归作者所有

共有 人打赏支持
侠客行之石头
粉丝 8
博文 180
码字总数 56156
作品 0
深圳
运维
MariaDB Connector/J 1.3.6 发布

MariaDB Connector/J 1.3.6 发布,该版本主要改进记录包括: CONJ-252 : Correction of charset parameter on Statement.setCharacterStream() and setClob. Revision #012b8c3 2016-02-19 C......

淡漠悠然
2016/03/02
816
0
Python-习题11~15

习题 11:提问 运行结果: 加分题: 1. raw_input 不管用户输入什么类型的都会转变成字符型 2. 另:input会根据用户输入变换相应的类型,而且如果要输入字符和字符串的时候必须要用引号包起来...

南风末
2016/11/13
10
0
qt界面崩溃问题,提示Access violation

最近在用qt开发一个简单的杀毒界面,发现有2%的崩溃率,但是不知道问题出在哪里?windbg分析只有系统的信息。。。 不知道有没有人遇到过,给点建议?感激不尽 EXCEPTION_RECORD: ffffffff -...

和平方舟2909
2015/12/28
596
0
Sql Server 中利用Convert 函数转换Datetime 到 String

CONVERT(varchar(10), date, 23) 1 02/22/06 CONVERT(varchar(10), date, 1) 0 Feb 22 2006 4:26PM CONVERT(CHAR(19), CURRENTTIMESTAMP, 0) 1 02/22/06 CONVERT(CHAR(8), CURRENTTIMESTAMP,......

菜鸟来了
2015/03/14
0
0
每日一道shell 练习(05)——批量打包文件

1. 习题 找到 /123 目录下所有后缀名为 .txt 的文件 批量修改.txt 为 .txt.bak 把所有.bak 文件打包压缩为 123.tar.gz 批量还原文件的名字,即把增加的.bak 再删除 2. 习题分析 首先要想办法...

hello_cjq
05/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

qduoj~前端~二次开发~打包docker镜像并上传到阿里云容器镜像仓库

上一篇文章https://my.oschina.net/finchxu/blog/1930017记录了怎么在本地修改前端,现在我要把我的修改添加到部署到本地的前端的docker容器中,然后打包这个容器成为一个本地镜像,然后把这...

虚拟世界的懒猫
今天
1
0
UML中 的各种符号含义

Class Notation A class notation consists of three parts: Class Name The name of the class appears in the first partition. Class Attributes Attributes are shown in the second par......

hutaishi
今天
1
0
20180818 上课截图

小丑鱼00
今天
1
0
Springsecurity之SecurityContextHolderStrategy

注:下面分析的版本是spring-security-4.2.x,源码的github地址是: https://github.com/spring-projects/spring-security/tree/4.2.x 先上一张图: 图1 SecurityContextHolderStrategy的三个......

汉斯-冯-拉特
今天
1
0
LNMP架构(Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl)

Nginx负载均衡 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器、分离出图片作为单独服务,这些是简单的数据的负载均衡,将压力分散到不同的机器上。有时...

蛋黄_Yolks
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部