第九周作业

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
下一篇 2017-03-05

相关推荐

  • N25_第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@test ~]# who root     tty1         2016-12-03&nb…

    Linux干货 2016-12-16
  • lamp平台 php解析器基于模块和php-fpm

    首先,我先介绍一下实验环境: http服务器:192.168.236.128(php解析器基于modules) mysql服务器:192.168.236.129 编译和配置http服务器,http版本是2.4以上的。 由于http依赖于apr apr-util这两个包,但是我们系统上的rpm包版本比较低,我们也需要下载这两个源码包来编译,解决依赖关系。 还要…

    Linux干货 2015-08-31
  • Linux的终端类型

    Linux的终端类型       终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备,linux终端大致有设备终端,物理终端、虚拟终端、图形终端、串行终端、伪终端等。 一.设备终端   设备终端的是一些看的见摸得着的一些实物,比如鼠标、键盘、显示器、打印机等之类的实物。 二.…

    Linux干货 2016-10-14
  • Linux系统上命令的使用格式与十二个常用命令详解

    Linux系统上命令的使用格式 命令的语法通用格式: ~]# COMMAND OPTIONS ARGUMENTS 例如: ls -ld /var COMMAND(命令): ls ls命令用来显示目标列表 OPTIONS(选项): -ld -ld 是 -l -d 的简写 -l 以详细格式列表 -d 仅列目录 ARGUMENTS(参数): /var 命令对这个/…

    2018-02-26
  • ☞Linux进程管理与性能分析

    Linux进程管理与性能分析 概述 系统维护的一个重要工作就是监控Linux系统的运行状态是否正常,分析系统资源的使用情况,进而对系统进行优化,提高其性能。在实际环境中,系统稳定性尤为重要,可以说系统的稳定性关系到企业的存亡。因此,第一时间掌握CPU、Memory、IO、Network的等系统资源的状态则可以在系统出现问题时及时解决,把损失降到最低。&nbs…

    Linux干货 2016-09-11
  • 子网掩码快速划分-心算

    问题状况: 不管作为一名网络工程师还是一名运维工程师,快速心算划分出网络掩码是必备得技能,      为什么要进行子网掩码划分? 纯二层环境不能隔离广播 有安全 管理方面的问题 解决办法:在二层环境下增加三层设备 将原本的一个主类网络号划分成多个子网     掩码基础知识:     注意:此步没记住不要往下进行     死记硬背1:     2^0=1   …

    Linux干货 2017-03-26

评论列表(1条)

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

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