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)
上一篇 2016-10-17 08:36
下一篇 2016-10-17 08:36

相关推荐

  • Mariadb数据库复制系列(二):主主复制

       实验二:MySQL双主复制架构的实现 双主的实现方式与主/从类似,仅是两个主机即使主节点,又是对方的从节点双主模型容易造成数据的不一致性,因此要慎用! 1、实验环境 2、在两个节点上安装mariadb-server服务 3、修改两个节点的服务器配置文件 4、两个节点上各自启动服务,各授权一个具有复制权限的用户 5、查看两个节点的二进制日志的状态 6、在…

    Linux干货 2016-11-24
  • 学习笔记–文本处理工具

    Linux下有很多文本处理工具,本文将介绍几款比较常用的文本处理工具,比如对文本查看的工具:cat和less,文本截取工具:head和tail,按列抽取文本工具:cut,按关键字查询文本工具:grep,下面将具体介绍各种工具的使用。 1、文本查看命令cat 用法: cat [OPTION]… [FILE]… cat命令一次性…

    Linux干货 2016-08-07
  • TCP常见问题总结

    TCP协议和UDP协议的区别是什么 TCP协议是有连接的,有连接的意思是开始传输实际数据之前TCP的客户端和服务器端必须通过三次握手建立连接,会话结束之后也要结束连接。而UDP是无连接的 TCP协议保证数据按序发送,按序到达,提供超时重传来保证可靠性,但是UDP不保证按序到达,甚至不保证到达,只是努力交付,即便是按序发送的序列,也不保证按序送到。 TCP协议…

    Linux干货 2017-09-02
  • Nginx lnmp环境及https的实现

    一、http事务简明  request: <method> <URL> <VERSION> MHADERS <body> response: <version><status><reason phrase> <HEADERS> … <body&…

    2014-09-25
  • 第一周学习内容

    1、LINUX的三大主流版本:debian slackware redhat 2、一个完整的操作系统是由内核+运行在内核之上的应用程序组成。      LINUX操作系统是由linux内核+GNU开源组织编写的应用程序组成(GNU/Linux) 3、CPU架构:X86(X64)、摩托罗拉的m68k、arm、IBM的POWER(性…

    Linux干货 2016-12-03
  • 习题

    1.当用户xiaoming对/testdir 目录无执行权限时,意味着无法做哪些操作? 2.当用户xiaoqiang对/testdir 目录无读权限时,意味着无法做哪些操作? 3.当用户wangcai 对/testdir 目录无写权限时,该目录下的只读文件file1是否可修改和删除? 4.复制/etc/fstab文件到/var/tmp下,设置文件所有者为wa…

    Linux干货 2016-08-04

评论列表(1条)

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

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