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

相关推荐

  • 基于heartbeat v1+ldirectord实现LVS集群高可用

    前言 高可用集群,High Availability Cluster,简称HA Cluster,是指以减少服务中断时间为目的的服务器集群技术。通过上文可以看出,LVS集群本身并不能实现高可用,比如Director Server不能检测Real Server的健康度,一旦其中一台或全部Real Server宕机,Director Server还会继续转发请求,…

    Linux干货 2015-06-08
  • Hello World 集中营

    编程的人应该都知道什么是Hello World。这是一个最简单的程序,其只在屏幕上输出“Hello World”字样,这通常是初学者的在学习编程时的第一个示例。把打印出 “Hello World” 作为第一个范例程序,现在已经成为编程语言学习的传统。“Hello World”起源于Brian Kernighan 和Dennis MacAlistair Rit…

    2016-05-17
  • Linux系统命令格式及基础命令、帮助信息。

    一、Linux系统上的命令使用格式 命令的语法通用格式:                 # COMMAND OPTIONS ARGUMENTS     &nb…

    Linux干货 2016-10-30
  • Linux第八周小结

    1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态 在线的主机使用绿色显示 不在线的主使用红色显示 #!/bin/bash # for i in {1..254};do if ping -c 6 -w 1 192.168.1.$i &> /dev/null;then echo -e…

    Linux干货 2017-08-20
  • sed基础

    sed 是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ),接着用sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed 主要用来自动编辑一个或多个文件, 简化对文件的反复操…

    Linux干货 2017-05-01
  • linux运维

    linux运维大纲,学习路线图

    Linux干货 2017-10-21

评论列表(1条)

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

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