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

相关推荐

  • 系统启动及恢复

    一、知识整理 1、modinfo命令:显示模块的详细描述信息: -n 只显示模块文件路径 -p 显示模块参数 -a auther -d description -l license协议 modprobe命令:装载或卸载内核 -r卸载内核,同rmmod 配置文件:/etc/modprobe.d/*.conf depmod命令:内核模块依赖关系文件及系统信息映射…

    Linux干货 2016-09-22
  • TCP协议详解

    TCP协议详解。     I,TCP数据段报文解释 1,tcp数据段头部20(固定)+40(可变)字节构成,此数据由报头偏移位构成,计算单位为四个字节 表示TCP报文段的首部长度,共4位,由于TCP首部包含一 个长度可变的选项部分,需要指定这个TCP报文段到底有多长。它指 出 TCP 报文段的数据起始处距离 TCP 报文段的起始处有多远。…

    Linux干货 2017-06-26
  • N25-第九周作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别统计这两类用户的个数;通过字符串比较来实现; #!/bin/bash     echo “可登录类型有有$(awk -F: ‘/[^\<nologin\>…

    Linux干货 2017-03-11
  • 学习Linux的第一周 之screen

         1.来到马哥的感想:      已经接触Linux已经一周,在紧张的而又条理的学习当中,我学到了很多东西单说量我就很佩服自己,在大学的两年当中已经快把自己学习新知识的渴望忘记了!来到马哥这短短一周我感觉我真的长大了很多,我把手机王者荣耀卸载了。只想对自己说加油坚持下去。…

    Linux干货 2017-07-16
  • 关于shell脚本基础第二篇

                          shell脚本编程基础第二篇 read命令 使用read来把输入的值非配给一个或者多个shell变量,可以提示用户输入一些参数等,此时我们可以使用read命令来完成此功能 re…

    系统运维 2016-08-19

评论列表(1条)

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

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