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

相关推荐

  • ansible进阶(roles应用)

    ansible 进阶 一、roles简介 一个项目从开始到结束,不是简单几十个playbook就可以完事了,当文件数很多,有上百个的话,仅通过简单的includes不停的引用,那最终的结果错综复杂。这个时候ansible roles就可以很好的发挥它的作用了。 roles,字面意思是角色的含义,可以理解为有相互关联功能的集合。我们把安装ntp、mem、ngi…

    2017-01-05
  • LB Cluster 负载均衡集群 —-lvs 配置详解

    lvs : ipvs scheduler: 根据其调度时是否考虑各RS当前的负载状态,可分为静态方法和动态方法两种: 1、静态方法:仅根据算法本身进行调度; RR:roundrobin,轮询; WRR:Weighted RR,加权轮询; SH:Source Hashing,实现session sticy,源IP地址hash;将来自于同一个IP地址的请求始终发…

    Linux干货 2016-10-30
  • Linux下用户属性的更改

      Linux下更改用户的属性一般使用usermod命令   基本格式如下:            Usermod[options]  login      [options]中具体参数如下:    -u  U…

    Linux干货 2017-05-30
  • iptables使用示例

    iptables规则

    2017-10-14
  • for、while、until循环

    一、for循环          for 变量名 in 列表;do             循环体     &nbsp…

    Linux干货 2016-09-19
  • SHELL网络爬虫实例剖析

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1552472        前天简单分享了用 shell 写网络爬虫的一些见解,今天特地把代码发出来与51博…

    Linux干货 2016-08-15

评论列表(1条)

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

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