脚本练习

 

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

    #!/bin/bash
    #Test host whether online 
    #on-line Green "\033[32m * \033[0m "
    #not online red "\033[31m * \033[0m"

    for i in {1..10};do
        if ping -W 1 -c 1 10.1.8.$i &> /dev/null;then
            echo -e "\033[32m 10.1.8.$i is online \033[0m"
        else
            echo -e "\033[31m 10.1.8.$i is not online \033[0m"
        fi
    done

执行结果:

q.png

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

    #!/bin/bash
    #
    if [ -z $(grep -o "^$1\>" /etc/passwd) ];then
            echo "Please enter the correct user name"
    elif [ $(grep "^$1\>" /etc/passwd | cut -d : -f 3) -ge 500 ];then
            echo "Ordinary user" 
    else
            echo "System user" 
    fi

3、写一个脚本,添加10个用户user1-user10,密码同用户名,用户不存在时才添加,存在时则跳过,最后显示本次共添加了多少用户。

    #!/bin/bash
    #
    num=o

    for i in {1..10};do
        if id user$i &> /dev/null;then
            continue
        else
            useradd user$i
            echo "user$i" | passwd --stdin user$i &> /dev/null
            let num++
        fi
    done

    echo "Add user $num"

 

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

(0)
641348038@qq.com641348038@qq.com
上一篇 2016-12-18 21:34
下一篇 2016-12-18 21:52

相关推荐

  • 软件包管理​

    1、 包管理器 包之间:可能存在依赖关系,甚至循环依赖    解决依赖包管理工具:yum(rpm包管理器的前端工具) 2、 库文件 查看二进制程序所依赖的库文件: #ldd /PATH/TO/BINARY_FILE 管理及查看本机装载的库文件: #ldconfig(安装程序一般都带有一些需要的库文件,要把相应的库文件配置于/etc/ld.…

    Linux干货 2016-09-01
  • linux基于密钥的认证

    生成密钥对儿: [root@Ams ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa):  Enter passphrase (empty for no passph…

    Linux干货 2016-08-02
  • CentOS7编译安装LAMP—php-fpm

    inux的环境是: [root@localhost ~]# lsb_release -a LSB Version:     :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description:     CentOS…

    Linux干货 2016-12-21
  • vsftpd

    vsftpd:     程序环境:         配置文件:/etc/vsftpd/vsftpd.conf         主程序:/usr/sbin/vsf…

    Linux干货 2016-12-05
  • 第二周作业

    由于图片粘贴复杂,请看链接。 http://note.youdao.com/noteshare?id=a78c3236bbf77232fcc3e2624a38ae12

    Linux干货 2016-09-19
  • 第五周着重练习扩展正则元字符及find命令

    1、显示当前系统上root、fedora或user1用户的默认shell; grep -E "^(root|hadoop|user1)\>" /etc/passwd |cut -d":" -f1,7 2、找出/etc/rc.d/init.d/functi…

    Linux干货 2016-12-13

评论列表(1条)

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

    赞,两个脚本写的都不错~继续加油~