马哥教育网络班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

相关推荐

  • N21_第10周_作业

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) 系统启动流程:POST -> BootSequence(BIOS)-> BootLoader(MBR)-> Kernel(ramdisk)-> rootfs(readonly)-> /sbin/init() 1>,加电自检:POST  &…

    Linux干货 2016-12-05
  • bash 循环语句for、while、until

    循环执行 循环执行     定义:将某段代码有次数或无次数循环执行多次。     构成要素:退出条件和进入条件     循环的三种语句:for、while、until for循环:      &nbs…

    Linux干货 2016-08-21
  • Linux进程管理

    Linux进程及作业管理      内核的功用:          进程管理、文件系统、网络功能、内存管理、驱动程序、安全功能          Process: 运行中的程序的一个副本;      …

    Linux干货 2017-01-05
  • 第一周博客作业

    1、描述计算机的组成及其功能。
    2、按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别。
    3、描述Linux的哲学思想,并按照自己的理解对其进行解释性描述。
    4、说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date命令的使用,并配合相应的示例来阐述。
    5、如何在Linux系统上获取命令的帮助信息,请详细列出,并描述man文档的章节是如何划分的。
    6、请罗列Linux发行版的基础目录名称命名法则及功用规定

    Linux干货 2018-03-13
  • Linux用户和组管理

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

    Linux干货 2015-04-26
  • Python 数据结构

    python 内置数据结构python 内置数据结构数字的处理函数: round() 五舍六入       2.5  2     2.6    3 floor() 向下取整       2.5  2     2.6    2 ceil() 向上取整       2.5  3     2.6    3 min() 取最小的       1,2,3      1…

    Linux干货 2017-09-23

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 23:03

    作业完成的不错,赞