马哥教育网络班22期+第九周课程练习

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别统计这两类用户的个数;通过字符串比较来实现;

#!/bin/bash
declare -i sum1=0
declare -i sum2=0
shells=$(cat /etc/passwd | cut -d: -f7)
for i in $shells;do
    if [[ $i == "/sbin/nologin" ]];then
        sum1+=1
    fi
    
    if [[ $i != "/sbin/nologin" ]];then
        sum2+=1    
    fi
done
echo "可登录shell用户有$sum1个"
echo "不可登录shell用户有$sum2个"

2、写一个脚本
    (1) 获取当前主机的主机名,保存于hostName变量中;
    (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
    (3) 否则,则显示当前主机名;

#!/bin/bash
hostName=$(hostname)
if [[ $hostName == "localhost" ]];then
    hostname www.magedu.com
    echo "当前主机名修改为了:www.magedu.com"
else
    hostname
fi

3、写一个脚本,完成如下功能
    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
    (2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
read -p "Enter a disk path(/dev/[sh]d[a-z]): " disk_path
if fdisk -l $disk_path &> /dev/null;then
    fdisk -l $disk_path
else
    echo "this disk is not existed!"
fi

4、写一个脚本,完成如下功能
   脚本能够接受一个参数;
   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
   (2) 如果参数1为yes,则显示继续执行脚本;
   (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
while true;do
    read -p "Enter an argument(yes or quit): " arg1
    if [ -z "$arg1" -o "$arg1" == "yes" ];then
        continue
    elif [[ $arg1 == "quit" ]];then
        echo "Normal exit!"
        break
    else
        echo "Abnormal exit!"
        exit 1        
    fi
done

5、写一个脚本,完成如下功能
   传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;
   (1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;
   (2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;
   (3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;
   (4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

#!/bin/bash
while true;do
    read -p "Enter an argument(gzip,bzip2,xz): " arg1
    if [ -z $arg1 ];then
        continue
    elif [[ $arg1 == "gzip" ]];then
        tar zcf /backups/etc-20160613.tar.gz /etc &> /dev/null
        echo "Finished"
        break
    elif [[ $arg1 == "bzip2" ]];then
        tar jcf /backups/etc-20160613.tar.bz2 /etc &> /dev/null
        echo "Finished"
        break
    elif [[ $arg1 == "xz" ]];then
        tar Jcf /backups/etc-20160613.tar.xz /etc &> /dev/null
        echo "Finished"
        break
    else
        echo "Error compression tool!"
        exit 1
    fi
done

6、写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;
   (4) 其它为无法判断;

#!/bin/bash
while true;do
    read -p "Enter a filepath: " path_arg
    if [ -z $path_arg ];then
        continue
    elif [ -L $path_arg ];then
        echo "This is a access path!"
    elif [ -d $path_arg ];then
        echo "Can use the 'cd' command!"
    elif [ -f $path_arg ];then
        echo "Can be accessed normally!"
    else
        echo "Error, can't judge!"
        break
    fi
done

7、写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
   (2) 否则,显示现有的主机名即可;

#!/bin/bash
hostName=$(hostname)
if [ -z $hostName -o $hostName == "localhost" -o $hostName == "(none)" ];then
    hostname mail.magedu.com
    echo "当前主机名修改为了:mail.magedu.com"
else
    hostname
fi

8、写一脚本,接受一个用户名为参数;
   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
   (3) 否则,则显示其为普通用户;

#!/bin/bash
[ -z $1 ] && echo "at least one username!" && exit 1
if id $1 &> /dev/null;then
    id_num=$(id -u $1)
    if [ $id_num -eq 0 ];then
        echo "This is administrator!"
    elif [ $id_num -gt 0 -a $id_num -lt 500 ];then
        echo "This is systemt user!"
    else
        echo "This is common user!"
    fi
else
    echo "user $1 is not existed!"
fi

9、写一个脚本,传递一个用户名参数给脚本;
   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
   (2) 否则,则显示无法登录系统;

#!/bin/bash
[ -z $1 ] && echo "at least one username!" && exit 1
if id $1 &> /dev/null;then
    if [ $(id -u $1) -gt 500 ];then
        if grep "^$1.*sh$" /etc/passwd &> /dev/null;then
            echo "This user can login system!"
        else
            echo "This user can't login system!"
        fi
    fi
else
    echo "user $1 is not existed!"
fi

10、写一个脚本,完成如下任务 :
   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;
   (5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
declare -i n=1
for i in /var/log/*;do
    [ -d /tmp/test$n ] || mkdir /tmp/test$n
    if [ -L $i ];then
        cp -d $i /tmp/test$n
    elif [ -d $i ];then
        cp -r $i /tmp/test$n
    elif [ -f $i ];then
        cp $i /tmp/test$n
    else
        cp -a $i /tmp/test$n
    fi
    let n++
done
echo "Copy finished!"

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

(0)
萝卜萝卜
上一篇 2016-11-28 14:10
下一篇 2016-11-28 14:47

相关推荐

  • Zabbix介绍、安装配置

    Zabbix介绍、安装配置 我们为什么需要监控? 常用的开源监控系统有哪些? Zabbix架构 Zabbix的安装和配置 总结 前言 本篇文章转自我的个人博客 http://anyisalin.com 欢迎大家访问 我们为什么需要监控? 没有监控就将系统上线, 就是在耍流氓; 在生产环境中, 监控是必不可少的, 因为我们需要实时了解业务的…

    2016-05-13
  • 马哥教育网络班22期+第十二周课程练习

    1、请描述一次完整的http请求处理过程。    (1) 客户端与服务端通过TCP三次握手建立或处理连接:接收请求或拒绝请求    (2) 接收请求:接收来自于网络上的主机请求报文中对某特定资源的一次请求的过程    (3) 处理请求:对请求报文进行解析,获取客户端请求…

    Linux干货 2016-12-29
  • 磁盘管理2

        centos6 实现loop 设备和文件关联         dd if=/dev/zero of=/testdir/bigfile bs=1M count=500      &nb…

    Linux干货 2016-09-01
  • HAProxy七种调度方法的简单示意图

    看了三个月,中间因出差和其他事没有看估计也有十几天,刚把35天的视频看完。很多内容都记不住,待第一次看完后再回头看吧。 现在想,对内容进行简单的画图,把基本的体现出来,对记忆和回顾应该有帮助。

    Linux干货 2016-07-26
  • 计算机组成、服务器类型及几个简单的小命令

    计算机是1946年冯诺依曼提出的,被称为冯诺依曼体系: 计算机的组成 1 运算设备 2 控制设备 3 储存设备 4 输入设备 5 输出设备 服务器类型 1 塔式服务器 2 机架式服务器 3 刀片式服务器 服务器系统分为三大类 1 Windows 2 Linux 3 Uinx 命令 #ls           …

    Linux干货 2017-07-13
  • 使用pyenv管理不同版本的python

    安装: 安装: $ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash 在你的shellrc文件中添加: export PATH=”$HOME/.pyenv/bin:$PATH” eval “$(pyenv init …

    Linux干货 2015-03-12

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 22:20

    第7题通过hostname修改主机名只是临时的,如果持久生效需要修改配置的,可以完善一下。