马哥教育网络班22期-第九周课程作业

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

#!/bin/bash
#

declare -i count=0
declare -i bash_num=0
declare -i nologin_num=0

count=$(awk -F: '{print $NF}' /etc/passwd |wc -l)       //统计一共多少个用户
for i in `seq 1 $count`;do
        shelltype=`awk -F: '{print $NF}' /etc/passwd |sed -n ${i}p`     //每一行依次显示
        if [ $shelltype == "/sbin/nologin" ];then
                let nologin_num++
        else
                let bash_num++
        fi
done
echo "nologin_num is ${nologin_num}"
echo "bash_num is ${bash_num}"

[root@localhost test-scripts]# bash shelltype.sh 
nologin_num is 28
bash_num is 19

2、写一个脚本

(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;

#!/bin/bash
#

if [ `hostname` == "localhost" ];then
        hostname www.magedu.com
        echo "The current hostname is localhost"
        echo "The modification hostname is www.magedu.com"
else
        hostname
fi
[root@localhost test-scripts]# bash hostname.sh 
The current hostname is localhost
The modification hostname is www.magedu.com

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

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
#!/bin/bash
#
ls $1 &>/dev/null
if [ `echo $?` -eq 0 ];then
        fdisk -l $1
else
        echo "The blockfile is not exist"
fi

[root@localhost test-scripts]# bash bfile.sh /dev/sda

Disk /dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0x0006bb86

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          66      524288   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              66        1371    10485760   83  Linux
/dev/sda3            1371        2676    10483898   82  Linux swap / Solaris
/dev/sda4            2677       10443    62388427+   5  Extended
/dev/sda5            2677        3982    10490413+  83  Linux
/dev/sda6            3983        5288    10490413+  83  Linux
/dev/sda7            5289        5550     2104483+  83  Linux
/dev/sda8            5551        5812     2104483+  83  Linux
/dev/sda9            5813        6074     2104483+  83  Linux
/dev/sda10           6075        7380    10490413+  83  Linux
/dev/sda11           7381        8686    10490413+  83  Linux
[root@localhost test-scripts]# bash bfile.sh /dev/sdb
The blockfile is not exist

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

脚本能够接受一个参数; (1) 如果参数1为quit,则显示退出脚本,并执行正常退出; (2) 如果参数1为yes,则显示继续执行脚本; (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
#
if [ $1 == "quit" ];then
        echo "exit script"
        exit 0
elif [ $1 == "yes" ];then
        echo "do script"
else
        echo "not normal exit"
        exit 1
fi
[root@localhost test-scripts]# bash exit.sh yes
do script
[root@localhost test-scripts]# bash exit.sh 1
not normal exit
[root@localhost test-scripts]# bash exit.sh quit
exit script

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 [ $1 == "gzip" ];then
        tar -zcf /backup/etc-20160613.tar.gz /etc &>/dev/null
elif [ $1 == "bzip2" ];then
        tar -jcf /backup/etc-20160613.tar.bz2 /etc &>/dev/null
elif [ $1 == "xz" ];then
        tar -Jcf /backup/etc-20160613.tar.xz /etc &>/dev/null
else
        echo "error compress tools"
fi
exit 0

[root@localhost test-scripts]# bash tar.sh gzip
[root@localhost test-scripts]# bash tar.sh bzip2
[root@localhost test-scripts]# bash tar.sh xz
[root@localhost test-scripts]# ll /backup/
总用量 24596
-rw-r--r-- 1 root root  8948924 11月  8 16:46 etc-20160613.tar.bz2
-rw-r--r-- 1 root root 10208056 11月  8 16:46 etc-20160613.tar.gz
-rw-r--r-- 1 root root  6024840 11月  8 16:47 etc-20160613.tar.xz
[root@localhost test-scripts]# bash tar.sh x
error compress tools

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

   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;
   (4) 其它为无法判断;

#!/bin/bash
#
if [ -d $1 ];then
        echo "directorypath cd $1"
elif [ -f $1 ];then
        echo "accessd is normal "
elif [ -h $1 ];then
        echo "This is accesspath"
else
        echo "no aduge"
fi
[root@localhost test-scripts]# bash filepath.sh /etc/fstab 
accessd is normal 
[root@localhost test-scripts]# bash filepath.sh /etc
directorypath cd /etc

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

   (1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
   (2) 否则,显示现有的主机名即可;
如第2题

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

   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
   (3) 否则,则显示其为普通用户;
#!/bin.bash
#
id -u $1 & >/dev/null

if [ $? -eq 0 ];then
        if [ `id -u $1` -lt 500 && `id -u $1` -gt 0 ];then
                echo "$1 is system user"
        elif [ `id -u $1` -eq 0 ]
                echo "$1 is administrator"
        else
                echo "$i is common user"
        fi
else
        echo "user is not exist"
fi

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

   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
   (2) 否则,则显示无法登录系统;
#!/bin/bash
#
declare -i id=0
id=`id -u $1`
shell=`grep $1 /etc/passwd |awk -F: '{print $NF}'`
if [ $id -ge 500 -a "$shell" == "/bin/bash" ];then
        echo "a user can log system"
else
        echo "not login system"
fi

[root@localhost test-scripts]# bash usertype2.sh xuc
a user can log system
[root@localhost test-scripts]# bash usertype2.sh root
not login system

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

   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;
   (5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
#

mkdir /tmp/test1-testn
file=/var/log/*
for i in $file;do
        if [ -d $i ];then
                cp -r $i /tmp/test1-testn
        elif [ -f $i ];then
                cp $i /tmp/test1-testn
        elif [ -L $i ];then
                cp -d $i /tmp/test1-testn
        else
                cp -a $i /tmp/test1-testn
        fi
done

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

(0)
上一篇 2016-11-21 10:12
下一篇 2016-11-21 10:55

相关推荐

  • linux文件权限练习(0803)

    1、当用户xiaoming对/testdir 目录无执行权限时,意味着无法做哪些操作?     不能cd到该目录,不能ls -l查看目录下文件的详细信息      2、当用户xiaoqiang对/testdir 目录无读权限时,意味着无法做哪些操作?…

    Linux干货 2016-08-04
  • 第四周作业2

    #### 1、 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 首先,利用cp命令将/etc/skel目录复制为/home/tuser1: “` [root@localhost ~]# cp -r /etc/skel /home/tuser1 “` 随…

    Linux干货 2018-03-24
  • 系统基础之权限管理作业题

    1.问题:  在/data/testdir里创建的新文件自动属于g1组,组g2的成员如: alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@wen-7 testdir]# mkdir -p /data/…

    Linux干货 2016-08-04
  • 马哥教育网络班21期+第12周课程练习

    1、请描述一次完整的http请求处理过程; 建立或处理请求:接受请求或拒绝请求; 接收请求:接收来自于网络的请求报文中对某资源的一次请求的过程; 处理请求:对请求报文进行解析,并获取请求的资源及请求方法等相关信息; 访问资源:获取请求报文中请求的资源; 构建相应报文; 发送响应报文; 记录日志 2、httpd所支持的处理模型有哪些,他们分别适用于哪些环境。 …

    Linux干货 2016-10-24
  • 系统管理之Selinux详解

    SELinux: SELinux概念SELinux配置启用SELinux管理文件安全标签管理端口标签管理SELinux布尔值开关管理日志查看SELinux帮助 SELinux概述 SELinux: Secure Enhanced Linux, 是美国国家安全局「NSA=The National Se…

    Linux干货 2016-09-21
  • 超全超详细的HTTP状态码大全

    本部分余下的内容会详细地介绍 HTTP 1.1中的状态码。这些状态码被分为五大类:  100-199 用于指定客户端应相应的某些动作。 200-299 用于表示请求成功。 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息。 400-499 用于指出客户端的错误。 500-599 用…

    Linux干货 2015-03-20

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 23:03

    作业完成的不错,赞