N22-第九周作业

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

#!/bin/bash

#

declare -i count1=0

declare -i count2=0

for i in $(awk -F : '{print $7}' /etc/passwd); do

        if [[ $i == "/sbin/nologin" ]]; then

                let count1++

        else

                let count2++

        fi

done

echo "total nologin:$count1"

echo "total longin: $count2"

脚本执行效果:

[root@localhost ~]# bash file1.sh

total nologin:19

total longin: 5

2、写一个脚本

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

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

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

#!/bin/bash

#

hostname=$(hostname)

if [[ $hostname == "localhost" ]]; then

    hostname www.magedu.com

else

    echo $hostname

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh

[root@localhost ~]# hostname

www.magedu.com

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

if [ -b $1 ]; then

    fdisk -l $1

else

    echo "no block device or no exist"

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh /dev/sda

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x000864ff

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048      196607       97280   83  Linux

/dev/sda2          196608   273661951   136732672   8e  Linux LVM

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

   脚本能够接受一个参数;

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 1

if [[ $1 == "quit" ]]; then

    echo "quit script" && exit 0

elif [[ $1 == "yes" ]]; then

    echo "contine run script"

else

    echo "except quit"

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh quit

quit script

[root@localhost ~]# bash file1.sh yes

contine run script

[root@localhost ~]# bash file1.sh no

except quit

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

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

if [[ $1 == "gzip" ]]; then

    tar -zcf /backups/etc-$(date +"%Y%m%d").tar.gz /etc

elif [[ $1 == "bzip2" ]]; then

    tar -jcf /backups/etc-$(date +"%Y%m%d").tar.bz2 /etc

elif [[ $1 == "xz" ]]; then

    tar -Jcf /backups/etc-$(date +"%Y%m%d").tar.xz /etc

else

    echo "error compress utils" && exit 2

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh gzip

tar: Removing leading `/' from member names

[root@localhost ~]# bash file1.sh bzip2

tar: Removing leading `/' from member names

[root@localhost ~]# bash file1.sh xz

tar: Removing leading `/' from member names

[root@localhost ~]# bash file1.sh cat

error compress utils

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

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

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

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

   (4) 其它为无法判断;

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

if [ -L $1 ]; then

    ls -l $1

elif [ -d $1 ]; then

    cd $1;pwd

elif [ -f $1 ]; then

    cat $1

else

    echo "unknow"

fi

脚本运行效果:

[root@localhost ~]# bash file1.sh /etc/rc.d/rc3.d/K50netconsole 

lrwxrwxrwx. 1 root root 20 Aug  6 06:14 /etc/rc.d/rc3.d/K50netconsole -> ../init.d/netconsole

[root@localhost ~]# bash file1.sh /etc/issue

\S

Kernel \r on an \m

[root@localhost ~]# bash file1.sh /etc/rc.d

/etc/rc.d

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

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

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

#!/bin/bash

#

hostname=$(hostname)

if [ $hostname == "" -o $hostname == "localhost" -o $hostname == "(none)" ]; then

        hostname mail.magedu.com

else

        echo $hostname

fi

脚本运行效果:

[root@localhost ~]# bash file2.sh

[root@localhost ~]# hostname

mail.magedu.com

                                               

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

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

 ! id $1 &>/dev/null && echo "user no exist" && exit 3

if [ $(id -u $1) -eq 0 ]; then

        echo "Is root"

elif [ $(id -u $1) -gt 0 -a $(id -u $1) -lt 500 ]; then

        echo "Is system"

else

        echo "Is user"

fi

脚本执行结果:

[root@localhost ~]# bash file1.sh root

Is root

[root@localhost ~]# bash file1.sh bin

Is system

[root@localhost ~]# bash file1.sh user10

user exist

[root@localhost ~]# bash file1.sh join

Is user

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

 ! id $1 &>/dev/null && echo "user no exist" && exit 3

if [[ $(id -u $1) -ge 500 && $(grep $1 /etc/passwd |cut -d : -f 7) =~ sh$ ]]; then

        echo "a user can log system."

else

        echo "login faild"

fi

脚本运行效果:

[root@localhost ~]# bash file1.sh join

a user can log system.

[root@localhost ~]# bash file1.sh root

login faild

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

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

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

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

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

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

#!/bin/bash

#

for i in /var/log/*; do

        if [ -d $i ]; then

                cp -r $i /tmp/test1/

        elif [ -f $i ]; then

                cp $i /tmp/test2/

        elif [ -L $i ]; then

                cp -d $i /tmp/test3/

        else

                cp -a $i /tmp/test4/

        fi

done

脚本执行结果:

[root@localhost ~]# ll /tmp/test1

total 4

drwxr-xr-x. 2 root root 4096 Oct 15 00:35 anaconda

drwxr-x—. 2 root root   22 Oct 15 00:35 audit

drwx——. 2 root root    6 Oct 15 00:35 httpd

drwx——. 2 root root    6 Oct 15 00:35 ppp

[root@localhost ~]# ll /tmp/test2

total 3040

-rw-r–r–. 1 root root   8406 Oct 15 00:35 boot.log

-rw——-. 1 root root      0 Oct 15 00:35 btmp

-rw——-. 1 root root    384 Oct 15 00:35 btmp-20161005

-rw——-. 1 root root  34326 Oct 15 00:35 cron

[root@localhost ~]# ll /tmp/test3

total 0

[root@localhost ~]# ll /tmp/test4

total 0

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

(0)
heianyangguoheianyangguo
上一篇 2016-10-17 08:36
下一篇 2016-10-17 08:36

相关推荐

  • linux系统监控 sar命令详解

    sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、内存使用状况、进程活动及IPC有关的活动等。 本文主要以CentOS 6.3 x64系统为例,介绍s…

    Linux干货 2015-06-17
  • 使用keepalive实现nginx反向代理高可用

    简介: 在网站架构中,为了分散客户端对服务器的访问压力,可以使用nginx作为反向代理。但是使用一个nginx作为代理服务器必定会面对单点故障的情况,所以一般使用多台nginx反代服务器,而使用多台nginx服务器还要面对如何协调调度的问题。在此,我给大家介绍使用keepalive协调调度nginx反代服务器的方法。   keepalive简介 说…

    2017-05-15
  • lvs初探

    1,lvs是什么 lvs是Linux Virtual Server缩写,linux虚拟服务器,担任负载调度器(load balance)的角色,它不提供任何服务,用户请求到这里的时候,它是将客户需求转发至后端真正提供服务的服务,LVS分为两部分组成,ipvsadm管理集群服务的命令行工具,ipvs,是内核模块。由于ipvs采用基于ip负载均衡技术,所以具有很…

    Linux干货 2016-10-30
  • Docker之~集群配置

    一、前言 Kubernetes 是Google开源的容器集群管理系统,基于Docker构建一个容器的调度服务,提供资源调度、均衡容灾、服务注册、动态扩缩容等功能套件,目前最新版本为0.6.2。 本文介绍如何基于Centos7.0构建Kubernetes平台,在正式介绍之前,大家有必要先理解Kubernetes几个核心概念及其承担的功能。以下为Kubernet…

    2015-03-23
  • 干货 | 20 行 Python 代码创建可视化地图

    当我开始建造Vincent时, 我的一个目的就是使得地图的建造尽可能合理化. 有一些很棒的python地图库-参见Basemap 和 Kartograph能让地图更有意思. 我强烈推荐这两个工具, 因为他们都很好用而且很强大. 我想有更简单一些的工具,能依靠Vega的力量并且允许简单的语法点到geoJSON文件,详细描述一个投影和大小/比列,最后输出地图. …

    2017-09-08
  • MySQL系列之一键安装脚本—-单实例/多实例

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1440414       最近在搞MySQL,由于经常测试一些东西。因此,就写了一个一键安装脚本。 脚本用途: 用于在…

    Linux干货 2016-08-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-25 13:33

    每一个脚本都可以看到实现的效果,很好,下次能将排版美化一下