bash脚本编程实例

bash脚本编程实例

  • 1.写一个脚本

    • 接受一个以上文件路径作为参数
    • 显示每个文件拥有的行数
    • 总结说明本次共为几个文件统计了其行数

      #!/bin/bash
      #
      read -p "please input some paths:" paths
      if [ -z $paths ];then
       echo "There are not any paths inputting."
       exit 1
      fi
      declare -i files=0
      for i in $paths;do
       if [ ! -e $i ];then
           echo "$i is not existing."
           continue
       elif [ ! -f $i ];then
           echo "$i is not a file"
           continue
       else
           count=`wc -l $i|cut -d' ' -f2`
           echo "$i has $count lines."
           files=$[$files+1]
       fi
      done
      echo "counting lines for $files files."
  • 2.写一个脚本

    • 传递两个以上字符串当做用户名
    • 创建这些用户,且密码同为用户名
    • 总结说明共创建了几个用户

      #!/bin/bash
      #
      read -p "please input some usernames:" usernames
      if [ -z $usernames ];then
       echo "There are not any usernames inputting."
       exit 1
      fi
      declare -i users=0
      for i in $usernames;do
       if ! id $i &> /dev/null;then
           useradd $i
           echo $i|passwd --stdin $i
           users=$[$users+1]
       else
           echo "$i has existed."
           continue
       fi
      done
      echo "createing users for $users users."
  • 3.写一个脚本,新建20个用户,visitor1-visitor20,计算他们的ID之和

    #!/bin/bash
      #
      declare -i ids=0
      for i in {1..20};do
          if ! id visitor$i &> /dev/null;then
              useradd visitor$i
              echo "create a new user named visitor$i."
          else
              echo "this user named visitor$i has existed."
          fi
          id=`id -u visitor$i`
          ids=$[$ids+$id]
      done
      echo "id count for these users are $ids."

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

(0)
N27_xiaoniN27_xiaoni
上一篇 2017-08-14 09:44
下一篇 2017-08-14 09:52

相关推荐

  • 文本编辑器—sed

    一、sed介绍 sed 一种流式编辑器。一个流式编辑器通常对来自输入流(一个文件或者是管道的输入)的文本进行转换处理。在某些方面类似支持脚本编辑的编辑器,sed在多输入情况下只开放一个通道工作,因此更加效率。sed与其他编辑器最大的区别在于,能对管道输入的文本进行过滤处理。 二、sed工作机制 sed保持两个数据缓冲区:主要活动的模式空间,以及辅助性的保持空…

    Linux干货 2016-08-12
  • bash特性之基础

    bash shell 命令历史history 环境变量: HISTSIZE :命令历史记录的条数 HISTFILE :~/.bash_history HISTFILESIZE: 显示环境变量:echo    环境变量的修改:export  history history [-c] [-d&…

    Linux干货 2016-11-22
  • 基于Cobbler实现多版本操作系统自动部署

    前言     在生产环境中,当需要批量部署几十甚至上百台服务器时,实现自动化安装操作系统尤为重要,按照传统的光盘引导安装是不可想象的;此前我们通过pxe+kickstart简单实现了自动化安装,但只能实现单一版本安装,当需要部署不同版本或不同引导模式(BIOS、EFI)时,此种方式就不够灵活。而Cobbler正是为了解…

    Linux干货 2015-08-11
  • 马哥教育网络班22期+第9周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;            [root@test ~]# vim&nbsp…

    Linux干货 2016-10-24
  • if case语句练习

     1、 写一个脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息 [root@localhost bin]# cat createuser.sh #!/bin/bash # Date…

    Linux干货 2016-08-15
  • PHP的类自动加载机制

    在PHP开发过程中,如果希望从外部引入一个class,通常会使用include和require方法,去把定义这个class的文件包含进来。这个在小规模开发的时候,没什么大问题。但在大型的开发项目中,这么做会产生大量的require或者include方法调用,这样不因降低效率,而且使得代码难以维护,况且require_once的代价很大。 在PHP5之前,各个…

    Linux干货 2015-04-10

评论列表(1条)

  • 马哥教育
    马哥教育 2017-08-20 19:12

    这几个脚本还是用到蛮多知识点的,很不错,再接再励。