N26-第八周作业-邢岩

马哥门徒-N26-邢岩


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

     在线的主机使用绿色显示;

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


#!/bin/bash

# Author: jeque
# Description: pingtest

for ((i=1;i<255;i++));do
  ping -w 1 -c 1 172.16.250.$i &> /dev.null
  if [ $? -eq 0 ];then
    echo -e “\e[1;32m 172.16.250.$i \e[0m”
  else
    echo -e “\e[1;31m 172.16.250.$i \e[0m”
  fi
done

N26-第八周作业-邢岩


2、如何给网络接口配置多个地址,有哪些方式?


   1) ip addr add IFADDR dev IFACE

          例如:ip addr add 192.168.10.99 dev eth0

   2) ifconfig IFACE_LABEL IPADDR/NETMASK

          例如:ifconfig eth0:0 192.168.1.0/24

    3) /etc/sysconfig/network-scripts/ifcfg-IFACE

例如:

N26-第八周作业-邢岩


3、写一个脚本,完成以下功能

   (1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;

   (2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;

   (3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;

   (4) 分别统计S开头和K开头的文件各有多少;


#!/bin/bash

# Author: jeque
# Description: pingtest

declare Ssum=0 Ksum=0

for i in $(ls /etc/rc.d/rc3.d); do
    if [ $( echo “$i” | cut -c 1-1) == “S” ];then
       echo “start_$i”
       let Ssum++
    elif [ $( echo “$i” | cut -c 1-1) == “K” ];then
       echo “stop_$i”
       let Ksum++
    fi
done
echo -e “K head file is $Ksum.\nS head file is $Ssum.”

N26-第八周作业-邢岩


4、写一个脚本,完成以下功能

   (1) 脚本能接受用户名作为参数;

   (2) 计算这些用户的ID之和;


#!/bin/bash

# Author: jeque
# Description: userid sum

while read line;do

    userid=$(echo $line | cut -d: -f3)

    let sum+=$userid

done < /etc/passwd

echo “User id sum is:$sum.”

N26-第八周作业-邢岩


5、写一个脚本

   (1) 传递一些目录给此脚本;

   (2) 逐个显示每个目录的所有一级文件或子目录的内容类型;

   (3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;


#!/bin/bash

# Author: jeque
# Description:

declare -i sumd=0
declare -i sumf=0
if [ $# -lt 1 ];then
   echo “usage $0 arg1 arg2 …”
   exit 1
fi

for i in $*; do
     [ ! -d $i ] && continue
     file $i/*
     dirtype=$(file $i/* | grep -c “directory$”)
     filetype=$(file $i/* | awk ‘{print $0}’ | sort | uniq | wc -l)
     let sumd+=dirtype
     let sumf+=filetype
done

echo “dir:$sumd,file:$sumf.”

N26-第八周作业-邢岩

N26-第八周作业-邢岩


6、写一个脚本

  通过命令行传递一个参数给脚本,参数为用户名;

  如果用户的id号大于等于500,则显示此用户为普通用户;


#!/bin/bash

# Author:jeque
# Description: username type
username=$1
if id “$username” &> /dev/null;then
    userid=$(grep “^$username\>” /etc/passwd |cut -d: -f3)

    if [ $userid -ge 500 ];then
echo “$username is common user.”
   else
        echo “$username is sys user.”
   fi
else
    echo “$username not exists!”
    exit 2
fi

N26-第八周作业-邢岩

7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;


#!/bin/bash

# Author:jeque
# Description:pingtest
for i in {20..100};do
    if ping -c 1 172.16.250.$i &> /dev/null;then
         echo “172.16.250.$i is UP”
    else
        continue
    fi
done

N26-第八周作业-邢岩


8、打印九九乘法表;


#!/bin/bash

# Author:jeque
# Description: multi table
for j in {1..9};do
    for i in $(seq 1 $j);do
        echo -n -e “${i}X${j}=$[${i}*${j}]\t “
    done
echo
done

N26-第八周作业-邢岩


今天就到这里了,bye!N26-第八周作业-邢岩

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

(0)
jequejeque
上一篇 2017-03-23 20:05
下一篇 2017-03-25 13:12

相关推荐

  • ldirectord 结合ipvsadm 配置nat,dr模型

    ldirectord 结合ipvsadm 配置nat,dr模型  一、nat模型 1、 drector # wget ftp://172.16.0.1/pub/Sources/7.x86_64/crmsh/ldirectord-3.9.6-0rc1.1.1.x86_64.rpm # yum -y install nginx (同时用于做为sorry…

    Linux干货 2014-01-03
  • 8月5日作业

    课堂练习: 1、找出ifconfig命令结果中本机的所有IPv4地址 [root@localhost ~]# ifconfig | tr -s " " |head -2 | tail -1 |cut …

    Linux干货 2016-08-15
  • linux入门第二课

    **linux入门基础** linux 命令类别 内部命令和外部命令 首先我们可以用echo $SHELL 来查询shell 里面会显示我们当前在用的shell 上图是echo $SHELL 也可以用cat /etc/shells来查询系统里面的shell bash其实是一个文件是一个程序 存在于 /bin/bash 目录,这是真是存在的文件,是一个可以执行…

    Linux干货 2017-05-20
  • 处理文本的工具sed,vim 编辑器的使用 Linux版

    处理文本的工具sed Stream EDitor 行编辑器 sed一次处理一行的内容,处理时,将当前处理的行存储在临时缓冲区,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,将缓冲区的内容送往屏幕,接着处理下一行不断重复,直到末尾。            一次处理一行 文件&#82…

    Linux干货 2016-08-11
  • 学习Linux的第一周 之screen

         1.来到马哥的感想:      已经接触Linux已经一周,在紧张的而又条理的学习当中,我学到了很多东西单说量我就很佩服自己,在大学的两年当中已经快把自己学习新知识的渴望忘记了!来到马哥这短短一周我感觉我真的长大了很多,我把手机王者荣耀卸载了。只想对自己说加油坚持下去。…

    Linux干货 2017-07-16
  • Nginx及其相关配置详解(二)

    与套接字相关的配置: 1、server { … }  #配置一个虚拟主机;         Default:—         Context:http server { # 配…

    2017-07-14

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-30 14:25

    测试了部分代码,功能都没有问题,但作为博客来讲,代码可读性也是非常重要的,再接再励。