037-shell习题02

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

 

转载于:https://my.oschina.net/u/3635512/blog/1528384

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值