Linux基础之shell脚本编程(一)

本文主要是shell脚本编程练习

1、写一个脚本

   (1)、获取并列出当前系统上的所有磁盘设备;

   (2)、显示每个磁盘设备上每个分区相关的空间使用信息;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 echo " all disk and partitions are: "
  5 for i in $(fdisk -l | grep -o "^/dev/sd.*" | cut -d" " -f1);
  6 do
  7         echo "$i"
  8         df -h $i
  9         echo -e "\n"
 10 done

2、写一个脚本

   (1) 接受一个以上文件路径作为参数;

   (2) 显示每个文件拥有的行数;

   (3) 总结说明本次共为几个文件统计了其行数;

  1 #!/bin/bash
  2 #author : BaoZhang
  3 #
  4 if [ $# -ne 1 ];then
  5   echo "usage : file_count /path/to/somedir/"
  6   exit 1
  7 fi
  8 cd $1 &>/dev/null
  9 if [ $? -ne 0 ];then
 10   echo "usage: file_count /path/to/somedir/"
 11   exit 2
 12 else
 13   file_number=$(ls -l $1 | wc -l)
 14   for i in $(ls $1);
 15   do
 16     echo "line number of $i is: $(cat $i |  wc -l)"
 17   done
 18 fi
 19 echo "all the file in $1 are : $file_number"
 20

3、写一个脚本

   (1) 传递两个以上字符串当作用户名;

   (2) 创建这些用户;且密码同用户名;

   (3) 总结说明共创建了几个用户;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #    
  4 new_user=0
  5 exist_user=0
  6 if [ $UID -ne 0 ];then
  7   echo "only root can add user"
  8   exit 1
  9 else
 10   if [ $# -lt 2 ];then
 11     echo "usage : user_add.sh username1 username2....,arguments must more then 2 username"
 12     exit 2
 13   else
 14     for i in $*;
 15     do
 16       id $i &>/dev/null
 17       if [ $? -eq 0 ];then
 18         echo "$i exist"
 19         exist_user=$[$exist_user+1]
 20         echo $i | passwd --stdin $i &>/dev/null
 21         echo "user $i changed the password"
 22       else
 23         useradd $i
 24         new_user=$[$new_user+1]
 25         echo $i | passwd --stdin $i &>/dev/null
 26         echo "user $i add"
 27       fi
 28     done
 29   fi
 30 fi
 31 echo "in the total , $new_user created, $exist_user change the password"

4、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 if [ $UID -ne 0 ];then
  5   echo "only root can add user"
  6   exit 1
  7 else
  8   for i in {1..20};
  9   do
 10     id visitor$i &>/dev/null
 11     if [ $? -eq 0 ];then
 12       echo "visitor$i exist "
 13     else
 14       useradd visitor$i
 15       echo "visitor$i add finished"
 16     fi
 17     #let sum+=$[$(cat /etc/passwd | awk -F: '{if ($1=="visitor$i") print $3}')]
 18     let sum+=$[$(id -u visitor$i)]
 19   done
 20 fi

5、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 file1=/etc/rc.d/rc.sysinit
  5 file2=/etc/rc.d/init.d/functions
  6 file3=/etc/fstab
  7 for i in {$file1,$file2,$file3};
  8 do
  9   count1+=$[$(grep "^#" $i | wc -l)]
 10   count2+=$[$(grep "^[[:space:]]$" $i  | wc -l)]
 11 done
 12 echo " start with #  total line is: $count1 "
 13 echo " total  blank line is : $count2 "

6、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 echo "all users are :" 
  5 grep  "/bin/bash$" /etc/passwd | cut -d: -f1,3
  6 for i in $( grep  "/bin/bash$" /etc/passwd | cut -d: -f1,3 | cut -d: -f2);
  7 do
  8   let sum+=$[$i]
  9 done
 10 echo "the sum of  UID is: $sum"

7、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 echo "all users are:"
  5 grep "," /etc/group | cut -d: -f1
  6 echo "total user count  are : $(grep "," /etc/group | cut -d":" -f1 | wc -l  )"

原创文章,作者:zhangbao,如若转载,请注明出处:http://www.178linux.com/57832

(0)
zhangbaozhangbao
上一篇 2016-11-11 08:42
下一篇 2016-11-11 14:31

相关推荐

  • 第六周作业-Vim总结

                    Vim总结 vim功能比较多,这里我不总结vim多窗口,只对基本常用的介绍. 一.模式说明 vim和记事本或WORD不一样,不是一打开后就可以输入文字,此时它处于正常模…

    Linux干货 2017-01-16
  • Linux基础知识(一)

     本文的主要内容是:  1.计算机基本组成及其功能  2.linux的发行版,以及他们之间的联系和区别  3.Linux的哲学思想  4.Linux的上的命令的使用格式  5.基础命令 ifconfig,echo,tty,startx,export,pwd,history,shutdown,powe…

    Linux干货 2016-09-17
  • 马哥教育21期网络班—第15周课程+练习—-sed 总结

    sed:编辑器 流编辑器,文本流编辑 ed: Stream EDitor, 行编辑器; 介绍:sed是 一个非交换性文本流编辑器,它编辑文件或标准输入导出的文本拷贝。标准输入可能来自键盘、文件重定向、字符串或变量,或者管道的文本。 sed可以干什么? 别忘了vi也是一个文本编辑器。sed可以随意编辑小或大的文件,有许多…

    Linux干货 2016-11-14
  • 马哥教育网络班22期+第6周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; cp /etc/rc.d/rc.sysinit /tmp vim /tmp/rc.sysinit :%s@^[[:space:]]\+@…

    Linux干货 2016-09-19
  • Linux网络属性管理

    Linux网络属性管理 linux的网络配置方法有多种,而且随发行版及版本而略微不同。我目前的测试环境为CentOS6和CentOS7。 常用的网络配置命令或方法有:ifconfig,route,netstat,ip,ss,nmcli,配置文件。 首先:命令方法介绍 一、命令简介 ifconfig:是个比较传统的命令,可以配置网络接口的启用及关闭,IP地址等…

    系统运维 2016-05-29
  • grep

    1. 文本处理工具的使用:   cat 连接文件并打印到标准输出设备,但是文件较大时,翻屏太快,与more或者less连用 命令反着输入(tac)将会反向输出文本文件 用法: cat 选项 参数   选项: -A 显示不可打印字符 -b 对行进行编号,空白行不编号 -s 压缩空白行 -n 对行进行编号,包括空白航  参数: &n…

    Linux干货 2016-08-08