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
下一篇 2017-03-25

相关推荐

  • Linux第三周总结

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 ~]# who | cut -d ” -f1 | uniq 2、取出最后登录到当前系统的用户的相关信息。 ~]# who|tail -1 3、取出当前系统上被用户当作其默认shell的最多的那个shell ~]# cut -d’:’…

    2017-07-16
  • MongoDB

    Edit MongoDB 手册 MongoDB 手册 第一章 Introduction MongoDB入门学习目录(建议) Databases Collections Documents 第二章 部署安装 1. Import the MongoDB public key 2. Configure the package management system (…

    Linux干货 2017-04-08
  • 正则表达式

    正则表达式是一种以一些字符赋予特殊意义之后,用来表达字符串,用以筛选各种形式的字符串用来进行查找、替换、删除等各种文本编辑作用的一种表示方式。 正则表达式的特殊字符 字符表示 .   可以表示任意可打印字符 [] 中括号范围内任意单个字符 [^] 中括号范围外任意单个字符 (相对所有可打印字符) [:space:] 表示任意单个空白字符 [:dig…

    2017-08-03
  • week5:grep命令和find命令的应用

    1.显示当前系统上root、fedora或user1用户的默认shell;     ~]# grep -E "^(root|fedora|user1):" /etc/passwd|cut -d/ -f4 2.找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hel…

    Linux干货 2016-11-28
  • 文本处理三剑客-grep及正则表达式初

    什么是正则表达式 基本正则表达式的元字符 扩展正则表达式 grep的基本理解 grep的基本选项 grep的应用实例 什么是正则表达式   正则表达式,又称正规表示法、常规表示法。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式的文本。(…

    Linux干货 2016-08-07
  • bash脚本编程之select语句、函数

    概述     承接上篇,继续介绍一下另一个循环语句select,还有脚本中函数的相关内容,分为三个部分:         1、select语句的介绍和效果演示       &nbs…

    Linux干货 2016-08-21

评论列表(1条)

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

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