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)
N27_xiaoniN27_xiaoni
上一篇 2017-08-20 21:28
下一篇 2017-08-20 22:17

相关推荐

  • CentOS系统启动流程

        Linux系统的组成部分   1、动态视角:内核+根文件系统  2、静态视角:磁盘分区+文件 Linux运行中的系统环境   1、用户空间:应用程序(进程或线程)  2、内核空间:内核代码(系统调用) 内核设计流派:   *单内核设计:把所有功能集成于同一个程序,如:Linux  *微内核设计:每种功能使用一个单独的子系统实现,如…

    Linux干货 2016-12-30
  • Linux入门(二)

    linux基础命令及应用     shutdown:关机命令         -r:重启         -h:关机    &nb…

    Linux干货 2016-08-02
  • bash循环、函数、数组、内置的字符串处理、变量、trap信号捕捉

    流程控制 过程式编程语言: 顺序执行 如果是命令写错了,可以继续往下执行;但当语法错误时则不会往下继续执行; 选择执行 循环执行 条件选择:if语句 单分支 if 判断条件;then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件为真的分支代码 else 条件为假的分支代码 fi 多分支 if 判断条件1; then 条件为真的分支代码 …

    Linux干货 2017-05-21
  • 马哥教育网络班21期-第六周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1.复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加# [root@junfeng ~]# cp /etc/rc.d/rc.sysinit /tmp [root@junfeng&nbs…

    Linux干货 2016-08-15
  • CentOS7.3安装Jumpserver0.3.2

    CentOS7.3安装Jumpserver0.3.2 公司服务器前端增加堡垒机,选用开源的jumpserver 软件环境CentOS Linux release 7.3.1611 python 2.7.5 mysql5.7 安装git yum -y install git 克隆jumpserver # cd /opt # git clone https://…

    Linux干货 2017-07-11
  • week3

    一,列出当前系统上所有已经登录的用户的用户名,注意,同一个用户登录多次只显示一次即可 who | cut -d' ' -f1 | sort -u 二,取出最后登录到当前系统的用户相关信息 who | cut -d'&nb…

    Linux干货 2016-11-15

评论列表(1条)

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

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