bash脚本编程实例

bash脚本编程实例

  • 1.写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态

    • 在线的主机使用绿色显示
    • 不在线的主机使用红色显示

      #!/bin/bash
      for i in {1..254};do
       if /bin/ping -W 1 -c 1 172.16.250.${i} >> /dev/null;then
           echo -e "\033[32m 172.16.250.${i} \033[0m"
       else
           echo -e "\033[31m 172.16.250.${i} \033[0m"
       fi
      done
  • 2.如何给网络端口配置多个地址,有哪些方式?

    • ifconfig IFACE_LABEL IPADDR netmask NETMASK
    • ip addr add IFADDR dev IFNAME label LABEL
    • cp /etc/sysconfig/network-scripts/ifcfg-IFACE /etc/sysconfig/network-scripts/ifcfg-IFACE_LABEL,然后修改该配置文件

      BOOTPROTO=none(必须设置为静态)
      DEVICE=IFACE_LABEL
      IPADDR=ip
      NETMASK=netmask
      GATEWAY=dateway
  • 3.写一个脚本,完成一下功能

    • 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头和S开头的文件若干
    • 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串
    • 显示所有以S开头的文件的文件名,并且给其附加一个start字符串

      #!/bin/bash
      for i in $(ls /tmp/njy/files/|grep "^s")
      do
       echo "${i} start"
      done
      
      for i in $(ls /tmp/njy/files/|grep "^k")
      do
       echo "${i} stop"
      done
  • 4.写一个脚本,完成以下功能

    • 脚本能接受用户名作为参数
    • 计算此些用户的ID之和

      #!/bin/bash
      echo -n "please input some usernames:"
      read -a usernames
      
      while true;do
      if [ ${#usernames[*]} -eq 0 ];then
      echo -n  "please input some usernames again:"
      read -a usernames
      else
      break
      fi
      done
      declare -i sum=0
      declare -i count=0
      for i in ${usernames[*]};do
       if !id $i &>> /dev/null;then
           echo "$i is not existing."
           continue
       else
          sum+=$(id -u $i)
          count+=1
       fi
      done
      echo "$count users id count is: $sum"
  • 5.写一个脚本

    • 传递一些目录给此脚本
    • 逐个显示每个目录的所有一级文件或者子目录的内容类型
    • 统计一个有多少个目录,并且一共显示了多少个文件的内容类型

      #!/bin/bash
      #
      read -p "please input some paths:" paths
      
      while true;do
       if [[ -z $paths ]];then
           read -p "please input some paths again:" paths
       else
           break
       fi
      done
      
      for i in $paths;do
        if [[ ! -e $i ]];then
            continue
        else
            pathfiles=$(ls -l $i|grep -v "^d"|awk -F' ' '{print $9}'|sed "/^$/d;s#^#$i/#")
            pathdirs=$(ls -l $i|grep "^d"|awk -F' ' '{print $9}|sed "/^$/d;s#^#$i/#"')
            if [ ${#pathfiles} -ge 1 ];then
                echo "file types and count below $i are:"
                file $pathfiles|cut -d: -f2|sed 's/^[[:space:]]*//g'|tr '\n' ';'|awk -F';' '{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}'
            else
                echo "there is no file belowing $i."
            fi
      
            if [ ${#pathdirs} -ge 1 ];then
                echo "dir types and count below $i are:"
                file $pathdirs|cut -d: -f2|sed 's/^[[:space:]]*//g'|tr '\n' ';'|awk -F';' '{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}'
            else
                echo "there is no dir belowing $i."
            fi
        fi
      done
  • 6.写一个脚本

    • 通过命令行传递一个参数给脚本,参数为用户名
    • 如果用户的id号大于等于500,则显示此用户为普通用户

      #!/bin/bash
      if [ $# -eq 0 ];then
       echo "need a user."
       exit 1
      fi
      
      id1=$(id -u $1)
      
      if [ $id1 -ge 500 ];then
        echo "this is a common user."
      else
        echo "this is system user or root user."
      fi
  • 7.写一个脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来

    #!/bin/bash
      for i in {20..100};do
          if /bin/ping -W 1 -c 1 172.16.250.${i} >> /dev/null;then
              echo  "172.16.250.${i}"
          else
              continue
          fi
      done
  • 8.打印九九乘法表

    #!/bin/bash
      for i in {1..9};do
          for ((j=1;j<=i;j++));do
               echo -e -n "${i}x${j}=$[$i*$j]\t"
          done
          echo
      done

原创文章,作者:N27_xiaoni,如若转载,请注明出处:http://www.178linux.com/85050

(0)
上一篇 2017-08-20 21:28
下一篇 2017-08-20 22:17

相关推荐

  • 有关shell脚本

    SHELL脚本 在Linux运维工作中有着大量的重复性质的工作,比如同时给很多台的服务器备份,查看多台的内存情况,等等。这样的情况下总不能一个一个服务器自己去看,那样耗时,费力,还容易出错。所以编写程序来做这些事情就成了必要的工作。而SHELL就是完成这项工作的。   脚本第一行的开头写上#!/bin/bash 这是属于格式性质的,作用在于加上执行…

    2017-08-12
  • Linux破解root口令

    Centos5、6版本破解口令方法 1 启动系统 2 在操作系统选择菜单界面选中要启动的内核 — 按a键 3 在行尾输入 1(或s或S或single)— 按回车键 4 使用passwd命令修改口令即可   Centos7版本破解口令方法 方法1 1 启动系统 2 在操作系统选择菜单界面选中要启动的内核 — 按e键…

    Linux干货 2018-01-08
  • 第五周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@centos6 ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf         &…

    Linux干货 2017-01-16
  • linux中rpm命令的妙用

    linux中rpm命令的妙用 CentOS系统上使用rpm命令管理程序包: 安装、卸载、升级、查询、校验、数据库维护 安装:         rpm {-i|–install} [install-options] PACKAGE_FILE…    …

    Linux干货 2016-08-24
  • 文件的压缩与解压

    文件的压缩与解压   常用命令既参数 1, gzip                        …

    Linux干货 2017-04-16
  • linux 学习笔记

    第二周

    Linux干货 2018-03-16

评论列表(1条)

  • 马哥教育
    马哥教育 2017-09-02 20:49

    脚本是运维的必备技能 也是运维自动化的切入点。作业完成 的非常 不错,再接再励。