马哥教育网络班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)
N22_熊宝N22_熊宝
上一篇 2016-11-21 10:12
下一篇 2016-11-21 10:13

相关推荐

  • shell脚本编程初步

    shell脚本编程初步    随着课程学习的深入,我们已经学习了一些常用的命令,会去解决了一部分简单的问题,但是这不能满足复杂的工作,它可以帮助我执行自动化的常用命令、执行系统管理和故障排除、创建简单的应用程序、处理文本或文件。所以我们开始了shell脚本的编程,帮助我们更好的去完成工作。简单地说,shell编程相当于帮我们之前…

    Linux干货 2016-08-15
  • 高可用keepalived+haproxy实现动静分离

    高可用keepalived+haproxy: 实验环境: clientIP为:172.18.254.149 VIRROUTEIP为:172.18.61.5 keepalived+haproxy机器1IP为172.18.61.1 keepalived+haproxy机器2IP为172.18.61.2 server1IP为172.18.61.3 server2I…

    2017-05-17
  • Linux用户和组管理

        登录Linux时我们都需要输入账号和密码,但Linux只会识别账号所对应用ID号,这个ID号就称为UID。     同理每个用户组也都拥有相对应的组ID号,即GID。     Linux根据/etc/passwd文件的内容来查找当前…

    Linux干货 2015-04-26
  • RAID学习总结

    RAID(Redundant Array of Independent Disks): 定义:独立硬盘冗余阵列,旧称廉价磁盘冗余阵列(Redundant Array of Independent Disks),简称磁盘阵列。 原理:把多个相对便宜的硬盘组合起来,成为一个硬盘阵列组,使其性能达到甚至超过价格昂贵,容量巨大的硬盘。 优势: RAID在容量和管理上…

    Linux干货 2016-08-22
  • shell变量的浅谈

    变量本质上是存储数据的一个或多个计算机内存地址,变量的命令规则包括: 1) 不能使用程序中的保留字,如if, for 2) 变量由字母、下划线和数字组成,且不能以数字开头 3) 要求风名知义 4) 统一命名规则:驼峰命名法 变量主要分为本地变量、环境变量、局部变量、位置变量和特殊变量 (1)本地变量:只对当前shell…

    2017-08-05
  • HSRP vs VRRP

    HSRP:(Hot Standby Router Protocol)-热备份路由协议 是cisco平台一种特有的技术,是cisco的私有协议。 VRRP:(Virtual Router Redundancy Protocol)-虚拟路由冗余协议 是国际标准,由IETF提出的解决局域网中配置静态网关出现单点失效现象的路由协议。 ----------------…

    Linux干货 2016-10-19

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 23:03

    作业完成的不错,赞