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

相关推荐

  • linux中用ACL实现灵活的权限管理

    ACL是什么?? ACL英文原意是Access Control List(访问控制列表).它能够实现灵活的权限管理,除了文件的所有者,所属组和其他人,设置相应的权限外,ACL允许你给任何用户或是用户组设置任何文件/目录的访问权限(注意的是有些不支持数字模式的权限给定) ACL有什么用?? 作为UGO权限管理的补充,acl有GUO办不到或者是难以办到的功能 &…

    Linux干货 2016-08-05
  • iptables/netfilter入门到进阶

    一、iptables/netfilter的简介: iptables:规则管理工具;该系统工具有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。防火墙在做信息包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的信息包过滤表中,而这些表集成在 Linux 内核中。在信息包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfi…

    Linux干货 2017-05-03
  • Linux中的文本处理工具—sed

    sed 处理文本工具 一、概述   Stream EDitor,行编辑器    sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文…

    Linux干货 2016-08-10
  • sed工具的基本用法

    sed -n(不输出模式,不自动打印) sed -d(删除匹配到的内容) sed-e(多点编辑) sed-r(支持扩展正则表达式) sed#,#(地址定界,在此范围中匹配) sed 1~2   sed 2~2 (找奇数行和偶数行) sed =(为打印到的内容编辑行号)  sed!(取反) sed s///(查找替换,支持使用其他…

    Linux干货 2017-05-02
  • 20 shell脚本编程1

    20 shell脚本编程1 一、杂项知识整理 1、脚本文件格式:     #!/bin/bash     (注释信息:)     #description:say hello     #vesion:…

    Linux干货 2016-08-11
  • CentOS 6.5 LVM磁盘管理学习笔记(二)

    ACTIVE            '/dev/vg51cto/LogVol04' [22.80 GiB]inherit  ACTIVE       …

    Linux干货 2016-08-29

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 22:20

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