第九周

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

#!/bin/bash
for i in `cut -d':' -f7 /etc/passwd`;do
    if [ $i == "/sbin/nologin" ];then
        let x++
    else
        let y++
    fi
done
echo "nologin user number: $x"
echo "login user number: $y"

2、写一个脚本

(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
#!/bin/bash
hostname=`hostname -s`
if [ $hostname == "localhost" ];then
    hostname www.magedu.com
else
    echo `hostname`
fi

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

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
#!/bin/bash
if [ $# -le 0 ];then
    echo "must have one argument"
    exit 1
elif [ -b $1 ];then
    fdisk -l $1
else
    echo "argument is not block file"
fi

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

脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
if [ $# -le 0 ];then
    echo "must have one argument"
    exit 1
elif [ $1 = "quit" ];then
    echo "quit script"
    exit 0
elif [ $1 = "yes" ];then
    echo "continue"
else
    echo "error"
    exit 1
fi

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
[ -d /backups ] || mkdir /backups
if [ $# -le 0 ];then
    echo "must have one argument"
    exit
fi
case $1 in
    gzip) 
        tar -zcf /backups/etc-20160613.tar.gz /etc
        ;;

    bzip2)
        tar -jcf /backups/etc-20160613.tar.bz2 /etc
        ;;

    xz)
        tar -Jcf /backups/etc-20160613.tar.xz /etc
        ;;
    *)
        echo "error compression tools"
        ;;
esac

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

(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
#!/bin/bash
if [ $# -le 0 ];then
    echo "must have one argument"
    exit
fi
if [ -f $1 ];then
    echo "text file"
elif [ -d $1 ];then
    echo "to use the CD command"
elif [ -L $1 ];then
    echo "It is a access path"
else
    echo "not determinable"
fi

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

(1) 如果主机名为空或为localhost,或为""(none)"",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
#!/bin/bash
hostname=`hostname -s`
if [ -z "$hostname" -o "$hostname" = "(none)" -o "$hostname"==*localhost ];then
    hostname www.magedu.com
else
    echo `hostname`
fi

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

(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
#!/bin/bash
if [ $# -le 0 ];then
    echo "must have one argument"
    exit
fi

id $1 &> /dev/null
if [ $? -eq 0 ];then
    if [ `id -u $1` -eq 0 ];then
        echo "user is a administrator" 
    elif [ `id -u $1` -gt 0 -a `id -u $1` -lt 500 ];then
        echo "user is a system consumer" 
    else
        echo "user is a domestic consumer"
    fi
fi

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

(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
(2) 否则,则显示无法登录系统;
#!/bin/bash
if [ $# -le 0 ];then
    echo "must have one argument"
    exit
fi

id $1 &> /dev/null
if [ $? -eq 0 ];then
    userid=`grep ^$1 /etc/passwd | grep .*sh$ | cut -d: -f3`
    if [ $userid -ge 500 ];then
        echo "user can loggin system" 
    else
        echo "user can not loggin system"
    fi
fi

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

(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
(2) 复制目录时,才使用cp -r命令;
(3) 复制文件时使用cp命令;
(4) 复制链接文件时使用cp -d命令;
(5) 余下的所有类型,使用cp -a命令;
#!/bin/bash
[ -d /tmp/test1-testn ] || mkdir -p /tmp/test1-testn
for file in `ls /var/log`;do
    if [ -d $file ];then
        cp -r /var/log/$file /tmp/test1-testn
    elif [ -f $file ];then
        cp  /var/log/$file /tmp/test1-testn
    elif [ -L $file ];then
        cp -d /var/log/$file /tmp/test1-testn
    else
        cp -a /var/log/$file /tmp/test1-testn
    fi
done

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

(0)
N21_smileN21_smile
上一篇 2016-09-26 07:43
下一篇 2016-09-26 07:43

相关推荐

  • 管道重定向笔记作业

      标准i/o和管道 Vim f1 [root@centos7~]#]ps aux|grep vim root      10967(进程编号)  0.1  0.4 151196  4828 pts/0    S+   11:10   0:00 vim f1 root      11028  0.0  0.0 112660   968 pts/1 …

    Linux干货 2017-11-20
  • Linux基础之shell脚本编程(三)

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

    Linux干货 2016-11-20
  • Btrfs文件系统

    btrfs文件系统特性: 1、多物理卷支持 Btrfs可由多个底层物理卷组成:支持raid,以联机添加,移除,修改 2、写时复制机制(cow)     复制,更新及替换指针,而非就地更新。在文件进行修改的时候,首先将文件复制一份出来,在复制的文件中进行修改,修改完成后,将指向原有文件的指针指向到修改完成的文件上,若修改完成的文件出现错误,则我们可以通过源文件…

    2016-04-10
  • 分布式文件系统之fastDFS部署

    fastDFS下载地址:https://github.com/happyfish100/fastdfs部署参考文档:http://joelhy.github.io/2015/01/27/FastDFS-v5-06-deploy/ 我们接下来来部署fastDFS 部署结构: 192.168.42.150 node1 [Tracker]192.168.42.15…

    Linux干货 2017-06-16
  • Nginx作为web服务器的使用配置

    概述     Nginx是一款免费开源的web服务器,同时也可以作为http、imap/pop3协议进行反代服务器,本篇介绍一些nginx作为web服务器方面的相关配置,具体包含:     1、nginx基础概念介绍     2、nginx…

    Linux干货 2016-10-27
  • bash工作特性之命令执行状态返回值与命令行展开概述

    bash工作特性之命令执行状态返回值与命令行展开概述 bash是什么? shell是一种用户接口程序,提供了用户与内核进行交互的接口。shell可以分为两类:一类为GUI,即用户图形界面;另一类为CLI,即命令行接口,将用户的命令送入到内核去执行,大多数发行版中最常用的就是bash。 bash特性 命令执行状态返回值 bash通过状态返回值来输出结果: 命令…

    Linux干货 2017-07-09

评论列表(2条)

  • 马哥教育
    马哥教育 2016-09-27 09:25

    第2 7写的不对,自己测试一下看看主机名可以改过来吗?

    • N21_smile
      N21_smile 2016-09-27 10:19

      @马哥教育老师,我又都测了一遍,可以改的呀