第九周作业

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

#!/bin/bash
declare -i sum_login=0
declare -i sum_nologin=0
while read line;do
    shell=$(echo $line|awk -F: '{print $NF}')
    if [ "$shell" == "/sbin/nologin" ];then
        let sum_nologin++
    else
        let sum_login++
    fi
done </etc/passwd
echo "Can login: $sum_login"
echo "Cannot login: $sum_nologin"
执行结果:
[root@centos6 script]# ./chkuser.sh   
Can login: 8
Cannot login: 17

2、写一个脚本
    (1) 获取当前主机的主机名,保存于hostname变量中;
    (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;

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

#!/bin/bash
hostname=$(hostname)
if [ "$hostname" == "localhost" ];then
    hostname www.magedu.com
else
    hostname
fi
[root@centos6 script]# ./chhn.sh 
www.magedu.com

3、写一个脚本,完成如下功能
    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

if [ -b $1 ];then
    fdisk -l $1
else
    echo "Invalid file!"
fi
执行结果:
[root@centos6 script]# ./devinfo.sh /dev/tty0
Invalid file!
[root@centos6 script]# ./devinfo.sh /dev/sda1

Disk /dev/sda1: 524 MB, 524288000 bytes
255 heads, 63 sectors/track, 63 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

4、写一个脚本,完成如下功能
   脚本能够接受一个参数;
   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
   (2) 如果参数1为yes,则显示继续执行脚本;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

while true;do
    case $1 in
        quit)
            echo "Exit in 2 second..."
            sleep 2
            exit 0
            ;;
        yes)
            echo "Continue..."
            break
            ;;
        *)
            echo "Unknown error,exit!"
            exit 1
    esac
done
echo "Execute complete!"
执行结果:
[root@centos6 script]# ./argu.sh 
Usage ./argu.sh arg1
[root@centos6 script]# ./argu.sh quit
Exit in 2 second...
[root@centos6 script]# ./argu.sh yes
Continue...
Execute complete!
[root@centos6 script]# ./argu.sh haha
Unknown error,exit!

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 [ $# -ne 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

[ -d /backups ] || mkdir -p /backups
case $1 in
    gzip)
        tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc
        ;;
    bzip2)
        tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc
        ;;
    xz)
        tar Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc
        ;;
    *)
        echo "Invalid compress mode!"
        exit 1
esac
执行结果:
[root@centos6 script]# ll /backups
total 22904
-rw-r--r-- 1 root root 8395375 Feb 13 19:34 etc-20170213.tar.bz2
-rw-r--r-- 1 root root 9527374 Feb 13 19:32 etc-20170213.tar.gz
-rw-r--r-- 1 root root 5523792 Feb 13 19:34 etc-20170213.tar.xz

6、写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;

   (4) 其它为无法判断;

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

if [ -L $1 ];then
    echo "$1 is symbolic file."
elif [ -f $1 ];then
    echo "$1 is regular file,you can read and write it."
elif [ -d $1 ];then
    echo "$1 is directory,you can use the command cd to enter it."
else
    echo "Unknown file type!"
fi
执行结果:
[root@www script]# ./chktype.sh /etc/hosts
/etc/hosts is regular file,you can read and write it.
[root@www script]# ./chktype.sh /etc/redhat-release 
/etc/redhat-release is symbolic file.
[root@www script]# ./chktype.sh /etc/
/etc/ is directory,you can use the command cd to enter it.
[root@www script]# ./chktype.sh ddd
Unknown file type!

7、写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为”(none)”,则将其命名为mail.magedu.com;

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

#!/bin/bash
if [ -z "$(hostname)" ] || [ "$(hostname)" == "localhost" ];then
    hostname mail.magedu.com
    echo "set 'mail.magedu.com' as new hostname."
else
    hostname
fi
执行结果:
[root@www script]# ./sethn.sh 
set 'mail.magedu.com' as new hostname.
[root@www script]# hostname
mail.magedu.com

8、写一脚本,接受一个用户名为参数;
   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

if ! id $1 &>/dev/null;then
    echo "$1 not exists!"
    exit 1
fi

uid=$(id -u $1)
if [ $uid -ge 500 ];then
    echo "$1 is regular user."
elif [ $uid -gt 0 ];then
    echo "$1 is system user."
else
    echo "$1 is root."
fi
执行结果:
[root@www script]# ./chkuser2.sh magedu
magedu is regular user.
[root@www script]# ./chkuser2.sh mysql
mysql is system user.
[root@www script]# ./chkuser2.sh root
root is root.
[root@www script]# ./chkuser2.sh hahaha
hahaha not exists!

10、写一个脚本,传递一个用户名参数给脚本;
   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

if ! id $1 &>/dev/null;then
    echo "$1 not exists!"
    exit 1
fi

uid=$(id -u $1)
if [ $uid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then
    echo "$1 can login."
else
    echo "$1 cannot login."
fi
执行结果:
[root@www script]# ./chkuser3.sh magedu
magedu can login.
[root@www script]# ./chkuser3.sh nfc
nfc cannot login.

11、写一个脚本,完成如下任务 :
   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;

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

#!/bin/bash
declare -i j=1
for i in /var/log/*;do
    [ ! -d /tmp/test$j ] && mkdir -p /tmp/test$j
    if [ -L $i ];then
        cp -d $i /tmp/test$j
    elif [ -f $i ];then
        cp $i /tmp/test$j
    elif [ -d $i ];then
        cp -r $i /tmp/test$j
    else
        cp -a $i /tmp/test$j
    fi
    let j++
done
执行结果:
[root@www tmp]# tree
.
├── test1
│   └── anaconda.ifcfg.log
├── test10
│   └── btmp
├── test11
│   └── ConsoleKit
│       └── history
├── test12
│   └── cron
├── test13
│   └── dmesg
├── test14
│   └── dmesg.old
├── test15
│   └── dracut.log
├── test16
│   └── lastlog
├── test17
│   └── maillog
├── test18
│   └── messages
├── test19
│   └── mysqld.log
├── test2
│   └── anaconda.log
├── test20
│   └── secure
├── test21
│   └── spooler
├── test22
│   └── tallylog
├── test23
│   └── wtmp
├── test24
│   └── yum.log
├── test3
│   └── anaconda.program.log
├── test4
│   └── anaconda.storage.log
├── test5
│   └── anaconda.syslog
├── test6
│   └── anaconda.xlog
├── test7
│   └── anaconda.yum.log
├── test8
│   └── audit
│       └── audit.log
└── test9
    └── boot.log

原创文章,作者:N26-西安-方老喵,如若转载,请注明出处:http://www.178linux.com/70445

(0)
N26-西安-方老喵N26-西安-方老喵
上一篇 2017-03-05 09:57
下一篇 2017-03-05 11:35

相关推荐

  • Linux安全和加解密

    概述:     所有业务的正常运转,离不开一个安全的运行环境,系统安全性直接关系到业务稳定、可靠、以及可用性,本章就介绍一些系统安全相关的话题,具体包括:        1、加密基础概念     2、CA和证书的基础概念  &nb…

    Linux干货 2016-09-23
  • 浅述vim操作

    vim是一种比vi更加强大的模式化的全屏文本编辑器。vim在工作过程中有三种模式:编辑模式、插入模式、末行模式。 编辑模式(默认):又称命令模式,其工作内容包括移动光标、剪切、粘贴、删除等 输入模式:亦可称为插入模式,主要是在文件中修改文本内容 末行模式:亦称扩展命令模式,主要是执行vim内置命令的 vim三种模式之间的切换:如下图   (1)编辑…

    Linux干货 2016-08-11
  • Linux基础之正则表达式,用户、组管理

    Linux基础—正则表达式 正则表达式简单来说就是一种行字符串处理的方式,通过一些特殊符号的辅助,可达到搜索、删除、替换等某些特定字符串的处理方式 通配符(Globbing) 通配符与元字符类似,通配符主要用于文件名的匹配,而元字符则主要用在字符串的匹配上; 下面介绍几种常用的通配符: * 表示匹配任意位数的任意字符 ? 表示…

    Linux干货 2016-10-03
  • 学习宣言

     学习计划:参考N24学习时间与线路图,尽最大努力完成。 目标:学习好Linux,提升自我价值,找一份待遇好的工作。 宣言:每一次轻易的放弃,都是人生的一处败笔,为了梦想,加油!

    Linux资讯 2016-10-26
  • Centos 系列bind搭建DNS服务加固

        在centos系列版本上运用bind搭建dns服务教程已经有很多,先感谢前人做出的贡献,引用两篇博文,讲解的非常详细。 地址是: 主dns搭建:http://blog.csdn.net/reblue520/article/details/52537014 从dns搭建:http://blog.csdn.net/reblue520/…

    Linux干货 2017-04-16
  • MariaDB之MHA配置

    工作拓扑 一、MHA简明:     MHA(Master HA)是一款开源的MySQL的高可用程序,它为MySQL主从复制架构提供了    automating master failover 功能。MHA在监控到master节点故障时,会提升其中    拥有最新数据的…

    2014-10-08

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-07 11:52

    完成的很好,脚本的思路清晰,加油!