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

相关推荐

  • Linux基础学习总结(一)

    Linux 安装运行步骤: 进入\\172.16.0.1\python10          用户名:python10          密码:python10magedu 进入目录   ftp://172.16.0.1/pub/ISOs/ 下载linux系统镜像文件           CentOS-6.9-x86_64-bin-DVD1.iso     …

    Linux干货 2018-03-16
  • FTP服务

        FTP是一个非常古老的协议,其主要的作用是用于文件的传输。FTP采用明文的方式传输,极其不安全,但在局域网等环境中使用还是比较方便的。 一、工作原理     FTP的工作方式分为主动模式与被动模式。     1、主动模式 &nbsp…

    Linux干货 2015-07-08
  • 【26期】Linux第二周学习小总结

    关于用户组的一些小小的整理     本周学习很多的知识,一些文件管理的技巧和命令,重定向和管道的实用技巧和拓展,最后则是到了我们的用户组的权限和管理,既然说到了管理,我们都知道在Linux中,老师讲的最多的一句话就是一切皆文件,既然是文件就会有用户去用,在里面进行各种的操作,比如增删改查啊,对文件的重新定义啊,那我们就…

    2017-07-21
  • DNS服务器搭建

    1. 配置正向解析 1.安装bind yum install bind bind-utils -y 2.配置/etc/named.conf,需要修改的地方 listen-on port 53 { 127.0.0.1; 192.168.42.135; }; allow-query { any; }; dnssec-enable no; dnssec-valid…

    Linux干货 2017-05-24
  • 马哥教育网络班第21期+第六周课程作业

    1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的1、以至少一个空白字符开头的行的行首加#; :%s@^[[:space:]]\+@#@   2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符; :%s@^[[:space…

    Linux干货 2016-08-22
  • shell进阶之循环

    循环执行,将某代码段重复运行多次

    重复运行多少次:

    循环次数事先已知

    循环次数事先未知

    有进入条件和退出条件

    for, while, until

    Linux干货 2017-12-24

评论列表(1条)

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

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