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

相关推荐

  • N25期第七周作业

    创建一个10G分区,并格式为ext4文件系统   要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳 fdisk /dev/sdb # size 10G type 83 mke2fs -t ext4 -b 20…

    Linux干货 2017-02-28
  • 实验:系统启动和内核管理

    实验:bootloader 破坏stage1: 备份:dd if=/dev/sda of=/app/mbr bs=1 count=512 破坏:dd if=/dev/zero of=/dev/sda bs=1 count=446;此时/boot/grub/下的文件没有任何改动; 表现:stage1破坏后系统会直接进行光盘引导 修复1:救援模式–&…

    Linux干货 2017-05-14
  • N22第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可 [root@localhost ~]# who |cut -d' ' -f1 |sort -u   root   xuc 2、取出…

    Linux干货 2016-08-29
  • Linux三剑客之sed命令

    一.sed命令概述 Stream EDitor ,行编辑器 sed是一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为”模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕.接着处理下一行,这样不断重复,直到文件末尾.文件内容并没有改变除非你使用重定向存储输出.sed主…

    Linux干货 2016-08-15
  • linux内核(kernel)版本号的意义

      在linux下有一个目录,即/usr/src/kernels/目录,下面记载着一个linux系统的内核文件, 例如:2.6.18-164.el5-x86_64、2.6.18-8.el5-x86_64和2.6.18-194.el5-x86_64等,这些文件编号意味着什么呢?例如2.6.18代表着什么?el5代表着什么?x86_64又代表着什么? …

    Linux干货 2016-01-14
  • Shell脚本编程入门

    什么是Shell      操作系统最外层的程序,shell通过提示符让用户输入,向操作系统解释该输入,然后处理来自操作系统的任何结果输出来,管理用户与操作系统之间的交互。      Shell是一个用户跟操作系统之间的一个命令解释器。Shell是用户与Linux操作系统之间沟通…

    Linux干货 2016-08-15

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 22:20

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