第八周作业

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
下一篇 2017-02-24

相关推荐

  • 编程真难啊

    上周,在Sun的Java论坛上出现了一个这样的帖子,这个贴子的链接如下:http://forums.sun.com/thread.jspa?threadID=5404590&start=0&tstart=0 LZ的贴子翻译如下: 大家好,我是一个Java的新手,我有一个简单的问题:请问我怎么才能反转一个整数的符号啊。比如把-12转成+12。是…

    Linux干货 2015-04-03
  • N22-第四周

    1、复制/etc/skel目录到/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 ~]# cp -r /etc/skel /home/tuser1~]# ll -d /home/tuser1drwxr-xr-x. 3 root root 74 Aug 31 10:13 /home/tuser1~]# ch…

    Linux干货 2016-09-07
  • 初来乍到

    坐上了去往北方的火车,我不知道自己为什么会颤抖,也许是耳朵里那首汪峰的《北京,北京》震撼到了我,接着满脑子便是灯红酒绿的大街道和浮华的高楼大厦,我幻想着有一天能在这样的大城市中闯出一片天。梦醒了 ! 30个小时的路程确实是让我满脑子都是未来的自己。 对于我这个从来没有见过世面的人来说,第一次来到北京这座一线大城市,内心充满着无比的欣喜和激动,但更多的还是那份…

    Linux干货 2018-03-26
  • 往期学员优秀博客范文,供大家参考结构框架和基本格式要求。

    http://123.57.218.140/16152http://123.57.218.140/15668http://123.57.218.140/9255http://123.57.218.140/16666

    Linux干货 2016-11-28
  • Linux Yum源的安装配置​‍

    Linux Yum源的安装配置     一、基本概念   Yum(全称为YellowdogUpdater,Modified)是一个在Fedora和RedHat以及SUSE、CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包…

    Linux干货 2015-05-11
  • sed用法预习总结

    SED用法sed:Stream EDitor    行编辑器 sed:模式空间     默认不编辑原文件,仅对模式空间的数据做处理,将处理后的结果输出至屏幕 sed [options] 'AdressCommand' file…   &n…

    Linux干货 2016-08-10

评论列表(1条)

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

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