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、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 2、取出最后登录到当前系统的用户的相关信息。 3、取出当前系统上被用户当作其默认shell的最多的那个shell。 4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存 至/tmp/maxusers.txt文件中。 5、取出当前主机…

    Linux干货 2016-11-21
  • shell脚本之数组

    认识数组:    变量是存储单个元素的内存空间,而数组就是多个变量的合集,是一串连续的空间,但是,整个数组只能有一个名字。    数组内的数据都有指定的索引,以找到数组内指定的数据。索引的编号是从0开始,依次递增(0,1,2,3…),属于数值索引。索引也支持自定义的格式,而不仅是数值格式的索引,即为关联索引…

    Linux干货 2016-08-26
  • 网络班N22期第二周博客作业

    一、Linux常用文件管理命令及用法。  (1)、cat,由第一行开始显示内容,并将所有内容输出     用法:cat [OPTION]… [FILE]…      常用选项:     &nbsp…

    Linux干货 2016-08-22
  • 计算机基础知识及服务器介绍

    现代计算机组成 根据冯·诺依曼结构体系计算机有五大部件组成: 运算器、控制器、存储器、输入设备、输出设备 CPU=运算器+控制器 运算器由算术逻辑单元(ALU)、累加器、状态寄存器、通用寄存器组等组成。 算术逻辑运算单元(ALU)的基本功能为加、减、乘、除四则运算,与、或、非、异或等逻辑操作,以及移位、求补等操作。 运算器只能进行二进制运算、逻辑运算 控制器…

    Linux干货 2016-06-09
  • 13-高级文件系统管理-Quota,RAID,LVM

    配置配额系统     综述         在内核中执行         以文件系统(磁盘分区)为单位启用         对不同组或者用户的策略不同         根据块或者节…

    2017-03-16

评论列表(1条)

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

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