N26-博客作业-week8

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

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

#!/bin/bash
#
for i in {1..254}; do
    if -W 1 -c 1 ping 172.16.250.$i &> /dev/null; then
        echo -e "\033[32mHost 172.16.250.$i is online.\033[0m"
    else
        echo -e "\033[31mHost 172.16.250.$i is offline.\033[0m"
    fi
done

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

~]# ip  addr  add  192.168.121.11/24 dev eno16777736:1
~]# ifconfig eth0:1
~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0

3、写一个脚本,完成以下功能
   (1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
   (2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
   (3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;

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

#!/bin/bash
#
declare -i k=0,s=0 

for i in $(ls /etc/rc.d/rc3.d/K* | grep -E -o "[^/]+$"); do
    echo "$i stop"
    let k++
done

for j in $(ls /etc/rc.d/rc3.d/S* | grep -E -o "[^/]+$"); do
    echo "$j start"
    let s++
done

echo "Filename begins with "s": $s"
echo "FIlename begins with "k": $k"

4、写一个脚本,完成以下功能
   (1) 脚本能接受用户名作为参数;
   (2) 计算此些用户的ID之和;

#!/bin/bash
#
declare -i sum=0

if [ $# -lt 1 ];then
    echo "At least one username!"
    exit 1
fi
for user in $@;do
    if id $user &> /dev/null; then
            sum=$[$sum+$(id -u $user)]
    else
            echo "No such user!"
            exit 2
    fi
done

echo "$sum"

5、写一个脚本
   (1) 传递一些目录给此脚本;
   (2) 逐个显示每个目录的所有一级文件或子目录的内容类型;
   (3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;

#!/bin/bash
#
declare -i dir=0,file=0,dir_all=0,file_all=0

for i in $@;do
    if [ -d $i  ]; then
        for j in $(ls $i);do
            echo "$j"
            if [ -f $i/$j ];then
                let file+=$file
            elif [ -d $i/$j ];then
                let dir+=$dir
            fi
        done
    fi
    file_all=$[$file_all+$file]
    dir_all=$[$dir_all+$dir]
done

echo "File num: $file_all"
echo "Directory num: $dir_all"

6、写一个脚本
  通过命令行传递一个参数给脚本,参数为用户名
  如果用户的id号大于等于500,则显示此用户为普通用户;

#!/bin/bash
#
if [ $# -eq 0 ];then
        echo "At least one username!" 
        exit 1
fi

id=$(id -u $1)

if [ $id -ge 500 ]; then
        echo "login user"
else
        echo "system user"
fi

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

#!/bin/bash
#
for((i=20;i<=100;i++)); do
    if ping -W 1 -c 1 172.16.250.$i &> /dev/null; then
        echo "172.16.250.$i is online"
    fi
done

8、打印九九乘法表;

#!/bin/bash
#
declare -i j=1
declare -i i=1
while [ $j -le 9 ];do
    while [ $i -le $j ];do
        echo -n -e "${i}X${j}=$[$j*$i]\t"
        let i++
    done
    let j++
    let i=1
    echo
done

原创文章,作者:浙江-咲,如若转载,请注明出处:https://www.178linux.com/71784

赞 (0)
浙江-咲浙江-咲
上一篇 2017-03-26 17:07
下一篇 2017-03-26 18:43

相关推荐

  • Shell脚本编程 流程控制—循环

    循环:重复执行所指定的代码片段,直到循环条件不满足,则退出循环 一、for循环     语法格式:     for 变量名 in 列表     do         循环体     done &nbs…

    Linux干货 2016-08-24
  • 三剑客-sed小结

     sed是一款流编辑器工具,通常我们用来对文本进行过滤与替换操作,特别是当你想要对几十个配置文件做统一更改时,你会感受到sed的魅力。它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到…

    系统运维 2016-07-26
  • 第3周作业

    一、列出当前系统上的所有已经登录的用户的用户名 [root@bogon tmp]# who | cut -d ” ” -f1 | sort -urootwing[root@bogon tmp]# who | cut -d ” ” -f1 | uniqrootwing 二、取出最后登录到当前系统的用户相关信息 […

    Linux干货 2017-07-25
  • shell编程if及find查找作业

    写一个脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息 [root@www sh.log]# cat createuser.sh  #!/bin/bash #author #使用一个用户名作为参数,如…

    Linux干货 2016-08-16
  • Centos6基于虚拟主机的Lamp配置bbs、Blog、PhpMyAdmin应用程序

    Centos6实现基于虚拟主机的各应用程序搭建: 一、配置三个基于名称的虚拟主机;       虚拟主机一、discuzX       虚拟主机二、wordpress       虚拟主机三…

    Linux干货 2016-10-09
  • Linux 登录趣事

    初入Linux运维,在虚拟机VMware12中装载CentOS6和CentOS7,均带有图形化界面。简单熟悉Linux,发现了一些关于登录的趣事,在此与大家分享。 运行环境:CentOS6和CentOS7 工具:系统里的terminal 登录趣事(以CentOS6为例): (1)自动登录图形化界面 每次启动CentOS6,短暂等待之后便会进入登录界面,然后选…

    Linux干货 2017-07-15

评论列表(1条)

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

    可以看出脚本运用的已经比较熟练了,脚本在手,天下我有,继续加油。