马哥教育网络班21期-第九周课程练习

第九周作业

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

#!/bin/bash
#
declare -i nologin=0
declare -i other=0

    for i in $(cut -d: -f7 /etc/passwd);do
        if [ $i == /sbin/nologin ]; then
            let nologin++
        else
            let other++
        fi
    done

echo "nologin user numer:$nologin"
echo "other user number:$other"
~  

测试:
[root@qq scripts]# bash -x 9-1.sh 
+ declare -i nologin=0
+ declare -i other=0
++ cut -d: -f7 /etc/passwd
+ for i in '$(cut -d: -f7 /etc/passwd)'
+ '[' /bin/bash == /sbin/nologin ']'
+ let other++
+ for i in '$(cut -d: -f7 /etc/passwd)'
+ '[' /sbin/nologin == /sbin/nologin ']'
+ let nologin++
+ for i in '$(cut -d: -f7 /etc/passwd)'
+ '[' /sbin/nologin == /sbin/nologin ']'
+ let nologin++
……
+ echo 'nologin user numer:26'
nologin user numer:26
+ echo 'other user number:10'
other user number:10

2、写一个脚本

  • (1) 获取当前主机的主机名,保存于hostname变量中;

  • (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;

  • (3) 否则,则显示当前主机名;

    #!/bin/bash
    
    hostname=$(hostname)
    
    if [ $hostname == 'localhost' ];then
        hostname www.magedu.com && echo "hostname change:'www.magedu.com'"
    else
        echo 'hostname is:' $hostname
    fi
    
    测试:
    [root@qq scripts]# bash -x 9-2.sh 
    ++ hostname
    + hostname=qq.localdomain
    + '[' qq.localdomain == localhost ']'
    + echo 'hostname is:' qq.localdomain
    hostname is: qq.localdomain
    [root@qq scripts]#

3、写一个脚本,完成如下功能

  • (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

  • (2) 如果存在,则显示此设备上的所有分区信息;

    #!/bin/bash
    #
    
    read -p "enter dev path:" dev
    
    if [ -e $dev ];then
        parted $dev print
    else
        echo "This dev is not exist"
    fi
    
    测试:
    [root@qq scripts]# bash -x 9-3.sh 
    + read -p 'enter dev path:' dev
    enter dev path:/dev/sda
    + '[' -e /dev/sda ']'
    + parted /dev/sda print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 268GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start   End     Size    Type      File system     Flags
     1      1049kB  211MB   210MB   primary   ext4            boot
     2      211MB   52.6GB  52.4GB  primary   ext4
     3      52.6GB  94.6GB  41.9GB  primary   ext4
     4      94.6GB  268GB   174GB   extended
     5      94.6GB  131GB   36.7GB  logical   ext4
     6      131GB   152GB   21.0GB  logical   ext4
     7      152GB   168GB   15.7GB  logical   ext4
     8      168GB   184GB   15.7GB  logical   ext4
     9      184GB   194GB   10.5GB  logical   ext4
    10      194GB   198GB   4295MB  logical   linux-swap(v1)
    
    [root@qq scripts]#

4、写一个脚本,完成如下功能

  • 脚本能够接受一个参数;

  • (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;

  • (2) 如果参数1为yes,则显示继续执行脚本;

  • (3) 否则,参数1为其它任意值,均执行非正常退出;

    #!/bin/bash
    #
    
    if [ $# -le 0 ];then
        echo "usage:bash 9-4.sh quit|yes|other"
        exit 2
    else
        case $1 in
        quit)
        echo "quiting"
        exit 0;;
        yes)
        echo "conutine";;
        *)
        echo "error..."
        exit 1
        esac
    fi
    测试:
    [root@qq scripts]# bash 9-4.sh 
    usage:bash 9-4.sh quit|yes|other
    [root@qq scripts]# 
    [root@qq scripts]# 
    [root@qq scripts]# bash 9-4.sh quit
    quiting
    [root@qq scripts]# bash 9-4.sh yes
    conutine
    [root@qq scripts]# bash 9-4.sh d
    error...

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
    #
    if [ $# -le 0 ]; then
        echo "usage:bash 9-5.sh gzip|bzip2|xz|..."
        exit 2
    else
        case $1 in
        gzip)
        tar czvf /backups/etc-20160613.tar.gz /etc;;
        bzip2)
        tar cjvf /backups/etc-20160613.tar.bz2 /etc;;
        xz)
        tar cJvf /backups/etc-20160613.tar.xz /etc;;
        *)
        echo "error..."
        exit 1;;
        esac
    fi
    测试:
    [root@qq scripts]# bash  9-5.sh xz
    [root@qq scripts]# ls /backups/
    etc-20160613.tar.xz
    [root@qq scripts]#

6、写一个脚本,接受一个路径参数:

  • (1) 如果为普通文件,则说明其可被正常访问;

  • (2) 如果是目录文件,则说明可对其使用cd命令;

  • (3) 如果为符号链接文件,则说明是个访问路径;

  • (4) 其它为无法判断;

    #!/bin/bash
    #
    if [ $# -le 0 ];then
        echo "Usage:bash 9-6.sh /PATH/FILE."
        exit 2
    fi
    
    if [ -h $1 ]; then
        echo "This is accessable path."
    elif [ -d $1 ]; then
        echo "This is directory command 'cd' can be use."
    elif [ -f $1 ]; then
        echo "This file can be access."
    else
        echo "Unknow File."
    fi
    
    测试:
    [root@qq scripts]# bash 9-6.sh /tmp/2.txt 
    This is accessable path.
    [root@qq scripts]# bash 9-6.sh /tmp/1.txt 
    This file can be access.
    [root@qq scripts]# bash 9-6.sh /tmp/
    This is directory command 'cd' can be use.

7、写一个脚本,取得当前主机的主机名,判断

  • (1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;

  • (2) 否则,显示现有的主机名即可;

    #!/bin/bash
    #
    HostName=`hostname`
    if [ -z "HostNmae" -o "$HostName" == "localhost" -o "$HostName" == "(none)" ]; then
        hostname mail.magedu.com
    else
        echo "$HostName"
    fi
    测试:
    [root@qq scripts]# bash 9-7.sh 
    qq.localdomain

8、写一脚本,接受一个用户名为参数;

  • (1) 如果用户的id号为0,则显示其为管理员;

  • (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

  • (3) 否则,则显示其为普通用户;

    #!/bin/bash
    #
    
    read -p "Please enter one user:" user
    uid=`id -u $user`
    
    if [ $uid -eq 0 ]; then
        echo "$user is admin."
    elif [ $uid -gt 0 -a $uid -lt 500 ]; then
        echo "$user is system user."
    else
        echo "$user is common user."
    fi
    测试:
    [root@qq scripts]# bash 9-8.sh 
    Please enter one user:root
    root is admin.
    [root@qq scripts]# bash 9-8.sh
    Please enter one user:mysql
    mysql is system user.
    [root@qq scripts]# bash 9-8.sh
    Please enter one user:centos
    centos is common user.

10、写一个脚本,传递一个用户名参数给脚本;

  • (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

  • (2) 否则,则显示无法登录系统;

    #!/bin/bash
    #
    if [ $# -le 0 ]; then
        echo "Usage:bash 9-10.sh username"
        exit 2
    fi
    
    if id $1 &> /dev/null; then
        userid=`grep ^$1 /etc/passwd | grep sh$ | cut -d: -f3`
        if [ $userid -ge 500 ]; then
        echo "a user can login system."
        else
        echo "a user can not login system."
        fi
    fi
    测试:
    [root@qq scripts]# bash 9-10.sh root
    a user can not login system.
    [root@qq scripts]# bash 9-10.sh gg
    a user can login system.
    [root@qq scripts]# cat /etc/passwd | grep gg
    gg:x:603:603::/home/gg:/bin/sh

11、写一个脚本,完成如下任务 :

  • (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;

  • (2) 复制目录时,才使用cp -r命令;

  • (3) 复制文件时使用cp命令;

  • (4) 复制链接文件时使用cp -d命令;

  • (5) 余下的所有类型,使用cp -a命令;

    #!/bin/bash
    dir="/var/log/"
    des="/tmp/test1-testn"
    mkdir $des
    for i in `ls $dir`; do
        if [ -c $i ]; then
            cp -r $dir$i $des/
        elif [ -f $i ]; then
            cp $dir$i $des/
        elif [ -h $i ]; then
            cp -d $dir$i $des/
        else
            cp -a $dir$i $des/
        fi
    done

原创文章,作者:Net21_仲樂,如若转载,请注明出处:http://www.178linux.com/45878

(2)
Net21_仲樂Net21_仲樂
上一篇 2016-09-19
下一篇 2016-09-19

相关推荐

  • linux进程和计划任务

    内核的功用:进程管理、文件系统、网络功能、内存管理、驱动程序、 安全功能等 进程管理:     系统优先级:数字越小,优先级越高    实时优先级: 99-0,值最大优先级最高    nice值:-20 到19 ,对应系统优先级100-139或99 Linux 内核:抢占式多任务  &nb…

    Linux干货 2017-03-23
  • Shell脚本编程入门

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

    Linux干货 2016-08-15
  • Linux基础入门命令

    在经过一个星期的入门学习后,对于LINUX的认识也从陌生到了基本了解。总结了一下一周所学习的基本命令。 一、想要查看你当前所在的终端?看下面几条命令: 查看当前登陆的终端名:tty 、whoami ;   查看当前登陆的终端名的详细信息:who am i ; 查看当前所有登陆的终端的详细信息: w ; 二、SHELL 的相关用法 &nbs…

    2017-07-14
  • 马哥教育网络21期+第七周练习博客

    马哥教育网络21期+第七周练习博客 1、创建一个10G分区,并格式为ext4文件系统; 首先挂载一个新的硬盘使用fdisk /dev/sdb进入相应的磁盘空间划分一个10G的磁盘空间; Command (m for help): n Command action    …

    Linux干货 2016-08-22
  • 苦上半年时间

    这是一种享受

    Linux干货 2016-08-08
  • iptables

    一、机制 部队大院、高档私人住宅区都是在一个被保护起来的范围内,要想进入只能从各个大门进入,在进入的时候还在门口出示证件、或者是指纹识别、或者是保安身份认证等,只有通过了这些检查才能进入到大院、住宅区内部。 Linux系统也是这么一个大院,netfilter就是这个大院的围墙,prerouting、input、output、forward、postrouti…

    Linux干货 2017-01-13

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-19 18:14

    写脚本的时候,不要用系统的函数名作为变量名