马哥教育网络班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
下一篇 2016-11-21

相关推荐

  • LINUX下用户管理命令简述

    LINUX下用户管理命令简述 添加用户并设置密码 useradd [用户名] 创建用户 [root@localhost ~]# useradd jack [root@localhost ~]# cat /etc/shadow | grep jack jack:!!:17257:0:99999:7::: passwd [用户名] 设置密码 [root@loca…

    Linux干货 2017-04-05
  • LAMP的分布式实现——安装wordpress、phpMyAdmin

    题目:分别用三台centos 7主机分别部署 httpd, php-fpm, mariadb 第一台 10.1.43.101 –>httpd 第二台 10.1.43.102 –>php-fpm    分别在2台虚拟主机上部署wordpress和phpmyadmin 第三台 10.1.43.103 &#8…

    Linux干货 2016-10-12
  • 马哥教育网络21期+第九周练习博客

    马哥教育网络21期+第九周练习博客 1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; [root@localhost bin]# cat 1.sh  #!/bin/bash # while&nbsp…

    Linux干货 2016-09-05
  • LVS DR模式

    一、测试环境说明 操作系统:CentOS6.7-X64 IP_VS版本:1.2.26 DR:10.10.10.130 VIP:10.10.10.140 RS1:10.10.10.131 RS2:10.10.10.132 二、LVS-DR模式原理 a)客户端发送一个请求(源地址为CIP,目标地址为VIP,我们简称为CIP:VIP)到LVS的DR b)通过在调度…

    Linux干货 2016-09-19
  • 系统管理之作业管理与计划任务

    作业管理: Linux的作业控制(job )     前台作业:通过终端启动,且启动后一直占据终端;     后台作业:可通过终端启动,但启动后即转入后台运行(释放终端) 如何让作业运行于后台?     (1) 运行中的…

    Linux干货 2016-09-13
  • 进程管理

    Process Manager 工作管理  jobs ctrl+z,& bg,fg nohup 进程,资源管理  查看:pstree,ps,top,vmstat,pmap 查询:pgrep,pidof,fuser,lsof 管理:kill,nice,renice 管理软件or命令  htop glances dstat …

    Linux干货 2016-04-11

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 23:03

    作业完成的不错,赞