马哥教育网络班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

相关推荐

  • 第二周练习与作业

    第二周作业 1、Linux上的文件管理类命令有哪些,其常用的使用方法及其相关示例演示          文件管理类命令:cp,mv,rm cp: 源文件;目标文件          [root@loc…

    Linux干货 2017-08-09
  • 周期性计划任务crond讲解

    crond是Linux或者unix系统的作业调度程序。运用它,在设定的时间段周期性执行某个命令或脚本。下文的例子均在centos 7.3上测试。一、crond组件 如果centos7.3最小化安装,不一定存在crond服务,需要手动安装,安装之后手动启动并设置以后自行开机启动。 主要有cronie、cronie-anacron、crontabs三个程序包。 …

    Linux干货 2017-03-27
  • grub知识与故障排除

    知识点回顾 at 任务的存放位置:/var/spool/at/ crond 任务存放位置:/var/spool/cron/username 查看服务有没有运行 centos6:  service  atd  status    chkconfig –list atd chkconfig atd o…

    Linux干货 2016-09-13
  • 初涉Linux基本要点概括(2)

    linux上的文件查看与管理类命令 mkdir 作用: 创建目录 语法: mkdir [OPTION]… DIRECTORY… 示例: mkdir -p /tmp/x/y/z             &nb…

    Linux干货 2016-10-15
  • 正则表达式简述

    正则表达式简述 什么是正则表达式: 正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。 正则表达式分类: 标准正…

    Linux干货 2016-04-05
  • Linux软件包管理工具yum详解

    yum介绍 yum客户端的使用 配置文件项作说明: 案例repo配置文件。 创建yum源命令(yum-config-manager) yum list相关命令 yum安装 yum更新和升级 yum查找和显示 yum删除程序 查看仓库 实战演练本地yum仓库(光盘) YUM内置变量 创建YUM仓库(http) yum介绍 yum命令是在Fedora和RedHa…

    Linux干货 2016-09-07

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 22:20

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