第九周作业

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

# awk -F: ‘{if($7!=”/sbin/nologin”) {printf “Logined user %s\n”,$1} else {printf “Nolginged user %s\n”,$1}}’ /etc/passwd | sort -t ‘ ‘ -k 1 | cut -d’ ‘ -f1-2 | uniq -c
2. 写一个脚本
   (1) 获取当前主机的主机名,保存于hostname变量中;
   (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
   (3) 否则,则显示当前主机名;
#!/bin/bash
#
hostname=$(hostname)
if [[ $(echo “$hostname”) =~ ^localhost ]];then
        echo “$hostname”
        sed -i s@^localhost.*@www.magedu.com@ /etc/hostname
        cat /etc/hostname
else
        echo “$hostname”
fi
3. 写一个脚本,完成如下功能
   (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
   (2) 如果存在,则显示此设备上的所有分区信息;
#!/bin/bash
#
[ $# -lt 1 ] && echo “At least one device name.” && exit 1
if [ $1 == $(fdisk -l | grep -E -o “^Disk /dev/(sd|hd)[a-z]” | cut -d’ ‘ -f2) ];then
        echo -e “The device is existed, and the device disk partition is here:\n$(fdisk -l $1)”
else
        echo “The device is not existed.”
fi
4、写一个脚本,完成如下功能
   脚本能够接受一个参数;
   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
   (2) 如果参数1为yes,则显示继续执行脚本;
   (3) 否则,参数1为其它任意值,均执行非正常退出;
#!/bin/bash
#
[ $# -lt 1 ] && echo “At least on device name.” && exit 1
if [ $1 == $(fdisk -l | grep -E -o “^Disk /dev/(sd|hd)[a-z]” | cut -d’ ‘ -f2) ];then
        echo -e “The device is existed, and the device disk partition is here:\n$(fdisk -l $1)”
else
        echo “The device is not existed.”
fi
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;
#!/bin/bash
#
[ $# -lt 1 ] && echo “At least one name.” && exit 1
case $1 in
gzip)
        tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /root/testdb &> /dev/null
        echo “Gzip backup has finished.”
        ;;
bzip2)
        tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /root/testdb &> /dev/null
        echo “Bzip2 backup has finished.”
        ;;
xz)
        tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /root/testdb &> /dev/null
        echo “Xz backup has finished.”
        ;;
*)
        echo “Error, please input gzip, bzip or xz.”
        exit 2
        ;;
esac
6. 写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;
   (4) 其它为无法判断;
#!/bin/bash
#
[ $# -lt 1 ] && echo “At least one path name.” && exit 1
if [ -L $1  ];then
        echo “It’s a chain.”
        ls -l $1
elif [ -f $1  ];then
        echo “It’s a file.”
        ls -l $1
elif [ -d $1 ];then
        echo “It’s a directory.”
        cd $1 && pwd
else
        echo “Can’t determinate it.”
fi

7. 写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为”(none)”,则将其命名为mail.magedu.com;
   (2) 否则,显示现有的主机名即可;
#!/bin/bash
#
echo “The original hostname is $(hostname).”
if [[ $(hostname) =~ ^localhost ]];then
        sed -i s@^localhost.*@mail.magedu.com@ /etc/hostname
elif [[ $(hostname) =~ ^$ ]];then
        echo “mail.magedu.com” > /etc/hosname
elif [[ $(hostname) =~ “none” ]];then
        sed -i s@.*none.*@mail.magedu.com@ /etc/hostname    
        echo “$hostname”
else
        echo “Hostname isn’t changed. The hostname is $(hostname)”
fi
echo “The current hostname is $(cat /etc/hostname).”

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

赞 (1)
allenallen
上一篇 2017-03-01 11:41
下一篇 2017-03-01 13:59

相关推荐

  • 进程管理和计划任务

    进程管理使用的工具以及命令,计划任务的创建和执行,以及工作中需要的注意事项

    2017-12-21
  • ☞CentOS安装程序{ 源码包安装;rpm包安装;}&&恢复rpm功能

    ☞CentOS安装程序{ 源码包安装;rpm包安装;}&&恢复rpm功能 本文是继上一篇文章“CentOS程序安装的3种方式{ 源码包安装 | rpm包安装 | yum安装;}”的补充,上篇文章http://www.178linux.com/38812主要介绍了yum安装软件的方法以及归纳了详细的yum命令。本文继续介绍基于本地file、远程…

    Linux干货 2016-08-24
  • 发送mail的几种方法

    mail,顾名思义就是邮件。在这两天上课中,接触了发送mail的一些用法,课下自己在网上又查询补充了一下,总结出来,增加印像。 为了显示清晰,首先我们分别使用两个用户在不同的终端下登录,一个超级管理员root,一个是刚刚创建的用户zhangsan,看下图: 首先先来看一下发送mail用到什么选项:  -s<邮件主题>:指定邮件的主题; …

    2017-07-20
  • 马哥教育网络班20期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 who |cut -d " " -f1 |sort -u 2、取出最后登录到当前系统的用户的相关信息。 [root@localhost ~]# who|tai…

    Linux干货 2016-06-29
  • 8月5日第七节课作业

    一、当天练习 1、找出ifconfig命令结果中本机的所有IPv4地址 2、查出分区空间使用率的最大百分比值 3、查出用户UID最大值的用户名、UID及shell类型 4、查出/tmp的权限,以数字方式显示 5、统计当前连接本机的每个远程主机IP的连接数,并按从大 到小排序 1、显示/proc/meminfo文件中以大小s开头的行;(要求:使 用两种方式) …

    Linux干货 2016-08-08
  • Linux下find命令的使用

    为什么要使用find命令?     Linux系统中有着成千上万的文件,如果你想要找到自己想要的文件,一款查找软件是必不可少的,而locate是根据其生成的数据库进行查找,虽然速度会略快,但非实时查找,有些新的文件或目录是匹配不到的,而且locate是模糊匹配,而find命令为实时查找,且为精确匹配,如果你对目录的权限…

    Linux干货 2016-08-18

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-15 01:09

    改hostname的那个步骤如果文件不存在呢~~~继续加油!