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

相关推荐

  • Hadoop新增datanode与SecondaryNameNode

    无论是新增namenode还是SecondaryNameNode,操作方法大致相同 一、如果新增datanode,需要保证namenode能无密码ssh连接到新datanode 如果是添加SecondaryNameNode,则需保证其能无密码ssh连接至各datanode和namenode,namenode也需要能无密码连接到新SecondaryNameNo…

    Linux干货 2015-03-08
  • zabbix部署(Linux上部署/监控端/被监控端)

    zabbix部署 一、监控系统的简单介绍 (1)cacti:存储数据能力强,报警性能差 (2)nagios:报警性能差,存储数据仅有简单的一段可以判断是否在合理范围内的数据长度,储存在内存中。比如,连续采样数据存储,有连续三次不在合理范围内的数据就报警 (3)zabbix:结合上面两种工具的优点,又可以存储数据,又可以报警 二、zabbix特性 (1)数据采…

    Linux干货 2017-01-06
  • N26 第二周博客作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 常用的文件管理工具有: cp mv rm cp 命令 : copy 的简写 语法格式:    1) 单源复制 cp [OPTION]… [-T] SOURCE DEST      …

    Linux干货 2017-02-24
  • lvm的应用

    前言    lvm是logical volume manager(逻辑卷管理器)的简称,通过将若干个磁盘分区连接成一个整块的卷组(volumegroup),形成一个存储池,管理员可以在卷组上随意创建逻辑卷(logicalvolumes),并进一步在逻辑卷组上创建文件系 统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按…

    Linux干货 2016-05-23
  • 正则表达式基础

    正则表达式正则表达式    元字符:        .       点表示任意单个字符 最少一次        \w      匹配字母、数字、下划线、或汉字。        \s      匹配任意空白字符        \d      匹配数字        \b      匹配字符的开始或结束        ^       匹配首字符        $…

    Linux干货 2017-11-14
  • bash功能特性三 命令的引用、展开和补全

    一、bash的补全功能     1、命令补全     根据内建命令或外部命令的查找方式,查找以用户指定的字符串开头的命令,如果用户指定的开头字符串能惟一标识某命令,则tab键可以补全此命令,否则,则两次tab链可显示所有以指定字符串开头的命令。   &nbsp…

    Linux干货 2015-04-15

评论列表(1条)

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

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