第八周作业

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

    在线的主机使用绿色显示;
    不在线的主机使用红色显示;
#!/bin/bash
#
for i in {1..254};do
        ping -c 3 -f 172.16.250.$i &> /dev/null
        if [ $? -eq 0 ];then
                echo -e “\033[32m 172.16.250.$i is on line \033[0m”
        else
                echo -e “\033[31m 172.16.250.$i is offline \033[0m”
        fi
done
2. 如何给网络接口配置多个地址,有哪些方法
– ifconfig家族
# ifconfig eno33554984 192.168.10.111
– ip家族
# ip address add 192.168.10.112 dev eno33554984 
– nmtui或setup
编辑响应接口,添加ip
– 编辑/etc/sysconfig/network-scripts/ifcfg-eno33554984文件,添加IP地址
3. 写一个脚本,完成以下功能
(1)假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
(2)显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
(3)显示所有以S开头的文件名,并且给其附加一个start字符串;
(4)分别统计S开头和K开头的文件各有多少;
#!/bin/bash
#
declare -i n=0
declare -i m=0
for file in $(ls /etc/rc.d/rc3.d);do
        if [[ $file =~ ^K ]];then
                echo -e “$file\bstop”
                let n+=1
        elif [[ $file =~ ^S ]];then     
                echo “start$file”
                let m+=1
        fi
done
echo “K starting file: $n”
echo “S starting file: $m”
4. 写一个脚本,完成以下功能
(1)脚本能接受用户名作为参数;
(2)计算此些用户的ID之和;
#!/bin/bash
#
for i in $*;do
        let m+=$(grep $i /etc/passwd | cut -d: -f3)
done
echo “The sum of userid is $m.”
5. 写一个脚本
(1)传递一些目录给此脚本;
(2)逐个显示每个目录的所有一级文件或子目录的内容类型;
(3)统计一共有多少个目录;且一共显示了多少个文件的内容类型;
#!/bin/bash
#
for i in $*;do
        name=$(ls -ld $i/*)
        num=$(echo “$name” | grep -o “/.*/.*$”)
        cat /dev/null > /tmp/type 
        echo “$num”>/tmp/num     
        while read line;do
                type=$(file $line | cut -d’ ‘ -f2,3)
                linetype=$line:$type
                echo “$linetype”
                echo “$type” >> /tmp/type
        done < /tmp/num  
        echo -e “\nThe total type number of directory and file $i is descritped here:”
        sort /tmp/type | uniq -c | sed ‘s@^[[:space:]]\+@@’
done
6. 写一个脚本
    通过命令行传递一个参数给脚本,参数为用户名
    如果用户的id号大于等于500,则显示此用户为普通用户;
#!/bin/bash
#
let i=$(grep $1 /etc/passwd | cut -d: -f3)
if [ $i -gt 500 ];then
        echo “$1’s id is $i, and is a normal user.”
else
        echo “$1’s id is $i, and is not a normal user”
fi
7. 写一个脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来
#!/bin/bash
#
declare -i uphosts=0
declare -i downhosts=0
for i in {20..100}; do
        if ping -W 1 -c 1 172.16.250.$i &> /dev/null; then
                echo “172.16.250.$i is up.”
                let uphosts+=1
        else
                echo “172.16.250.$i is down.”
                let downhosts+=1
        fi
done
echo “Up hosts: $uphosts, Down hosts: $downhosts.” 

8. 打印九九乘法表
#!/bin/bash
#
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


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

(0)
allenallen
上一篇 2017-02-24 14:23
下一篇 2017-02-24 15:13

相关推荐

  • awk学习笔记

    一、linux的文本处理器三剑客   文本过滤器:grep、egrep、fgrep   行编辑器:sed(模式空间;保持空间)   报表生成器:awk(格式化文本输出)gawk awk的工作流程: 从文本中读取一行内容,根据指定的分隔符;将读入的一行内容分隔成字段,然后格式化输出指定的字段 二、awk的命令相关的参数及用法 &n…

    Linux干货 2015-06-23
  • TCP连接的状态详解以及故障排查

    我们通过了解TCP各个状态,可以排除和定位网络或系统故障时大有帮助。(总结网络上的内容) 1、TCP状态 linux查看tcp的状态命令: 1)、netstat -nat  查看TCP各个状态的数量 2)、lsof  -i:port  可以检测到打开套接字的状况 3)、 &nbs…

    Linux干货 2015-04-03
  • Python线程指南

    本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例。 注意:本文基于Python2.4完成,;如果看到不明白的词汇请记得百度谷歌或维基,whatever。 尊重作者的劳动,转载请注明作者及原文地址 >.< 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如…

    2015-03-13
  • 三剑客之Sed

    sed:stream editor(流编辑器) 工作特性:并不直接处理文本文件本身,处理机制为每当处理一个文件的时候,它会逐行读取,每次把一行读取到内存空间中去,而后在模式空间(pattern space)中完成编辑.并把编辑好的结果输出到屏幕上  功      能:数据替换、删除、增加、等,数据为关键字或者一整行, …

    Linux干货 2016-08-08
  • 文本处理工具grep

    正则表达式:Regular Expression REGEXP         由一类特殊字符及文本字符所编写的模式,表示控制或通配的功能 两类:     基本正则表达式BRE:     扩展正则表达式ERE: *** grep    GLoble searc…

    Linux干货 2016-08-04
  • 基于NFS服务的wordpress站点

    实验要求:             (1) nfs server导出/data/web,在目录中提供wordpress;     (2) nfs client挂载nfs server导出的文件系统至/data/web; …

    2017-06-11

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-15 01:20

    赞~完成的不错,几个题考虑到了多种方法~~继续加油~