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

相关推荐

  • 软件安装包的管理

    rpm安装包的管理 rpm的数据库( 公共) :/var/lib/rpm 程序包名称及版本 依赖关系 功能说明 包安装后生成的各文件路径及校验码信息 安装 rpm:{-i} -v:显示详细信息 -h:以#显示程序包管理执行进度 rpm -ivh PACKAGE_FILE … [install-options] –test :测试安装,但不真正执行安装过程 –…

    Linux干货 2017-04-23
  • if、case 语法

    1. 条件选择 if 语句         选择执行:              单分支      &nbs…

    Linux干货 2016-08-22
  • Bind编译安装详解

    Bind编译安装详解 Bind是一款开放源码的DNS服务器软件,由美国加州大学Berkeley分校开发和维护的,全名为Berkeley Internet Name Domain它是目前世界上使用最为广泛的DNS服务器软件,支持各种unix平台和windows平台。现今互联网上最常使用的DNS服务器软件,使用BIND作为服务器软件的DNS服务器约占所有DNS服…

    Linux干货 2016-07-22
  • 文本三剑客grep爵士与手下的血泪奋战

    文件查看命令: cat, tac,revcat [OPTION]… [FILE]…  -E: 显示行结束符$ -n: 对显示出的每一行进行编号 -A:显示所有控制符 -b: 非空行编号 -s:压缩连续的空行成一行 文件查看 分页查看文件内容more: 分页查看文件more [OPTIONS…] FILE…-d: 显示翻页及退出提示less:一页一页…

    Linux干货 2016-08-07
  • linux用户,组及权限管理

      写在前面: 本博客详解命令如下: useradd, userdel,usermod, passwd,  chage, groupadd,  groupdel, groupmod, gpasswd ,newgrp, chsh, id, su,  chmod,  chowm,  chgrp, &nb…

    Linux干货 2015-12-19
  • iptables

    一、机制 部队大院、高档私人住宅区都是在一个被保护起来的范围内,要想进入只能从各个大门进入,在进入的时候还在门口出示证件、或者是指纹识别、或者是保安身份认证等,只有通过了这些检查才能进入到大院、住宅区内部。 Linux系统也是这么一个大院,netfilter就是这个大院的围墙,prerouting、input、output、forward、postrouti…

    Linux干货 2017-01-13

评论列表(1条)

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

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