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

相关推荐

  • 内核编译实际效果演示

    内核编译实际操作效果演示     环境:CentOS7.2,自带内核版本为3.10.0-327.el7.x86_64,下载3.18.41版本进行编译     步骤1:确保开发工具包组已安装     [root@localhost …

    Linux干货 2016-09-13
  • 软件包管理

    rpm 与 yum 的用法 源代码:name-VERSION.tar.gz|bz2|xz VERSION: major.minor.release rpm包命名方式: name-VERSION-release.arch.rpm 例:bash-4.2.46-19.el7.x86_64.rpm VERSION: major.minor.release rele…

    Linux干货 2016-09-01
  • 0804正则表达式作业

    用正则表达式表示IP地址         首先来分析一下,制IP地址是一个32位的二进制数,通常被分割为4个“8位二进制数”(也就是4个字节)。IP地址通常用“点分十进制”表示成(a.b.c.d)的形式,其中,a,b,c,d都是0~255之间的十进制整数。例:点分十进IP地址(1…

    Linux干货 2016-08-10
  • 根DNS域名解析的实现

    一、实现从根,com,rj.com 模拟互联网的DNS架构 DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。 接下来就一起开始搭建吧 首先,我们需要计划好实验环境,包括实验的步骤思路 1)实验环境(最好是画图展示,能使思路清…

    2015-02-10
  • 数据流重导向

    数据流重导向(redirect):就是将某个指令执行后应该要出现在屏幕上的数据,传输到其他的位置。 standard output:标准输出是指指令执行回传正确的讯息。 standard error output : 指令执行失败后,所回传的错误讯息。 1标准输入(stdin):代码为0,使用< <<2标准输出(stdout),代码为1,使…

    Linux干货 2016-08-08
  • Linux基础知识之软硬链接

    系统环境:    该博文以CentOS6.8_x86_64系统为基础,Xshell5远程登录CentOS6.8系统,以root身份登录系统。 为什么要学习符号(软)链接和硬链接?    符号(软)链接和硬链接是Linux文件系统中的一个重要的概念,软硬链接的学习过程中会涉及一些文件系统中的索引节点(inode),索引节…

    Linux干货 2016-08-02

评论列表(1条)

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

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