马哥教育网络班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 第五天: (08月01日) Linux用户组管理

    Linux 第五天: (08月01日) Linux用户组管理         管理员 root,0普通用户 1-65535系统用户 1-499(centos6), 1-999(centos7)登录用户 500(centos6)+, 1000(centos7)+   /etc/passwd 用户及属性/etc/…

    Linux干货 2016-08-08
  • linux 文本处理工具 grep cut sort等

    linux day 7 间歇性回忆 自动属于这个组 是  SGID 的功能 chmod g+s /data/testdir setfacl —m g:g2:rwx /data/testdir setfacl -m b:g:g2:rwx /data/testdir setfacl -m d:g:g3:r testdir chmod o= testdi…

    Linux干货 2016-08-08
  • Linux 入门(二)

    又一个周的时间过去了,觉得时间过的好快的呢,大概是因为沉迷于学习吧(害羞脸),在这一周里学习了不少东西呢,下面就来总结一下吧 (1)    cp 复制 如果只是单纯的敲cp复制文件而不加任何选项的时候,如果目的目录文件已经存在,就会直接覆盖,而不会出现任何提示信息,而当在实际操作时,就算不加 –i选项,也会提示要不要覆盖文件 原…

    2017-07-22
  • 马哥教育网络班21期+第六周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;  ~]# cp /etc/rc.d/rc.sysinit /tmp/  ~]# vim /tmp…

    Linux干货 2016-08-22
  • 软链接和硬链接的区别

    软链接和硬链接的区别 什么是链接: 文件都是文件名和数据组成的,在linux中被分成两部分:数据和元数据。数据是记录文件的真实内容的地方;元数据是记录文件属性的地方:创建者、大小、创建时间等信息。元数据中的inode号这是唯一标识文件身份的属性。在linux中,文件的inode号可以通过ls –i命令查看。在linux中为了解决文件共享使用,引入了两种链接:…

    Linux干货 2016-10-20
  • 内部命令与外部命令——Linux基本命令(3)

    1.     内部命令与外部命令 内部命令:由shell自带的,而且通过某命令形式提供 内部命令在系统启动时就调入内存,是常驻内存的,所以执行效率高。 外部命令:在文件系统路径下有对应的可执行程序文件 外部命令是系统的软件功能,用户需要时才从硬盘中读入内存。 2.     命令类…

    2017-07-13

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 23:03

    作业完成的不错,赞