第八周练习脚本部分

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

#!/bin/bash
#
for((i=1;i<=254;i++));do
    site="172.16.250.${i}"
    ping -w1 -c1 $site &> /dev/null
    if [[ "$?" == "0" ]];then
        echo -e "\033[32m${site}\033[0m"
    else
        echo -e "\033[35m${site}\033[0m"
    fi
done

2.写一个脚本,完成以下功能

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

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

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

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

#!/bin/bash
#
for i in $(ls /etc/rc.d/rc3.d/);do
    if [[ "$i" =~ ^S  ]];then
        S=$(echo $i | wc -l)
        let Ssum+=$S
        echo ${i}start
    fi
    if [[ "$i" =~ ^K  ]];then
        K=$(echo $i | wc -l)
        let Ksum+=$K
        echo ${i}stop
    fi
done
    echo "S Begin:$Ssum"
    echo "K Begin:$Ksum"

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter,try the script again,please!"
    exit 1
fi
for i in $*;do
    id $i &> /dev/null && uid=$(grep -E "^${i}" /etc/passwd | cut -d: -f3) || echo "This user isn't existing"
    let sum+=$uid
done
    echo "ID_SUM is $sum"

4、写一个脚本

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter,try the script again,please!"
    exit 1
fi
for i in $*;do
    if [ -d $i ];then
        ls $i
        dnum=$(ls $i | wc -l)
        echo "The Directory has $dnum files"
    else
        ls $i
        echo "This is a commom file:$i"
    fi
done

5、写一个脚本

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter,try the script again,please!"
    exit 1
fi
for i in $*;do
    id $i &> /dev/null && uid=$(grep -E "^${i}" /etc/passwd | cut -d: -f3) || echo "This user isn't existing"
        if [[ "$uid" > "500" ]];then
            echo "$i is a common user"
        fi
done

6、写一个脚本

(1) 添加10用户user1-user10;密码同用户名;

(2) 用户不存在时才添加;存在时则跳过;

(3) 最后显示本次共添加了多少用户;

#!/bin/bash
#
x=0
for i in {1..10};do
    id user$i &> /dev/null && continue || useradd user$i && echo "user$i" | passwd --stdin user$i && let x++
done
    echo "add $x users"

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

与第一题相似,就不写啦~

8、打印九九乘法表;

#!/bin/bash
#
for((i=1;i<=9;i++));do
    for((j=1;j<=i;j++));do
        echo -e -n "$j*$i=$[$i*$j]\t"
    done
    echo ""
done

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

(0)
N24_涩味N24_涩味
上一篇 2016-12-15 19:43
下一篇 2016-12-16 09:50

相关推荐

  • gdisk用法

    gdisk用法   gdisk  – Interactive GUID partition table (GPT) manipulator GPT  fdisk (aka gdisk) is a text–mode menu–driven program for creation and ma…

    Linux干货 2017-04-24
  • Linux系统基础命令

    Linux下一些基本常用命令:   1、Lscpu:显示CPU架构详细信息; [root@centos6 ~]#lscpu 2、Cat:查看合并文件到标准输出; [root@centos6 ~]#cat file    Tac:显示的文本与cat显示的文本相反; 3、Who:显示都有那些用户登录到了系统; [root@…

    Linux干货 2016-07-26
  • Linux系统命令格式及基础命令、帮助信息。

    一、Linux系统上的命令使用格式 命令的语法通用格式:                 # COMMAND OPTIONS ARGUMENTS     &nb…

    Linux干货 2016-10-30
  • Storm集群安装详解

    storm有两种操作模式: 本地模式和远程模式。 本地模式:你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来;  远端模式:你提交的topology会在一个集群的机器上执行。 本文以Twitter Storm官方Wiki为基础,详细描述如何快速搭建一个Storm集群,其中,项目实践中遇到的问题及经验总结,在相应章…

    Linux干货 2015-04-04
  • Linux的用户,组及文件权限管理

    Linux用户与组的创建,删除,属性修改,文件权限管理

    Linux干货 2018-02-24
  • if、case 语法

    1. 条件选择 if 语句         选择执行:              单分支      &nbs…

    Linux干货 2016-08-22

评论列表(1条)

  • 马哥教育
    马哥教育 2016-12-23 12:07

    赞~~从几个脚本来看,掌握的非常不错~~继续加油~