马哥教育网络21期+第九周练习博客

马哥教育网络21期+第九周练习博客

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
[root@localhost bin]# cat 1.sh 
#!/bin/bash
#
while read line;do
   if [ `echo $line | cut -d ':' -f7` != '/sbin/nologin' ]; then
      let i++
   elif [ `echo $line | cut -d ':' -f7` = '/sbin/nologin' ];then
      let j++
   fi
done < /etc/passwd
echo "The default shell is '/sbin/nologin' $i."
echo "The default shell is not '/sbin/nologin' $j."

2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
[root@localhost bin]# cat 2.sh 
#!/bin/bash
#
hostname=`hostname`
if [ $hostname = 'localhost.localdomain' ];then
   `echo "www.magedu.com" > /proc/sys/kernel/hostname`
   echo "The host name: `hostname`."
fi

3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
root@localhost bin]# cat  3.sh 
#!/bin/bash
#
read -p "Please enter the disk device file path:" option
#
while true; do
  fdisk -l /dev/$option &> /dev/null 
   if [ $? -eq 0 ]; then
     echo "Disk path right."
    echo "`fdisk -l /dev/$option`"
      break
   fi
   if [ $? -ne 0 ];then 
     read -p "Please input the correct disk path:" option
   fi
done

4、写一个脚本,完成如下功能
 脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
[root@localhost bin]# cat 4.sh 
#!/bin/bash
#
read -p "Please input parameters:" option
while true ;do
  if [ "$option" == 'yes' ];then
    read -p "Please continue to input parameters:" option
  elif [ "$option" == 'quit' ];then
    echo "The normal exit."
  break
    return 0
  elif [ "$option" != 'quit' -a "$option" != 'yes' ];then
    echo "Non-normal exit."
    break
  fi
done

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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
[root@localhost bin]# cat 10.sh 
#!/bin/bash
#
i=`date +%Y%m%d`
if [ $# -lt 1 ]; then
   echo "Enter one option."
else 
case $1 in
gzip)
tar -czf/etc-$i.tar.gz hfz -C /backups
;;
bzip2)
tar -cjf/etc-$i.tar.bz2 hfz -C /backups
;;
xz)
tar -cJf/etc -$i.tar.xzhfz -C /backups
;;
*)
echo "exit."
;;
esac
fi

6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
[root@localhost bin]# cat 8.sh 
#!/bin/bash
#
read -p "Please enter the file path:" option
for file in /$option/* ; do
  if [ -f $file ];then
    echo "($file)  This file can be normal access."
   elif [ -L $file ]; then
    echo "($file) is This file is the access path."
  elif [ -d $file ]; then
    echo "($file) This file can use the CD command."
  else 
    echo "The other could not judge."
  fi
done

7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
[root@localhost bin]# cat 5.sh 
#!/bin/bash
#
i=`hostname`
if [ "$i" == 'localhost' ]; then
   `echo "mail.magedu.com" > /proc/sys/kernel/hostname`
    echo "The host name: `hostname`."
elif [ "$i" == 'none' ];then
    `echo "mail.magedu.com" > /proc/sys/kernel/hostname`
    echo "The host name: `hostname`."
else 
    echo "hostname is not equal to 'localhost' or 'none' so the hostname is $i."
fi

8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
[root@localhost bin]# cat 6.sh 
#!/bin/bash
#
i=`cat /etc/passwd | egrep "^$1" | cut -d':' -f3`
if [ $i -eq 0 ];then
   echo "This user to the administrator."
elif [ $i -gt 0 -a $i -le 500 ];then
   echo "This user for users of the system."
elif [ $i -gt 500 ];then
   echo "This user for ordinary users."
fi

10、写一个脚本,传递一个用户名参数给脚本;
 (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
(2) 否则,则显示无法登录系统;
[root@localhost bin]# cat 7.sh 
#!/bin/bash
#
i=`cat /etc/passwd | egrep "^$1" | cut -d':' -f3`
j=``
#

if [ $i -lt 500 ];then
   echo "user UID less 500"
   echo "Unable to login system."
fi
if [ $i -ge 500 ];then
  cat /etc/passwd | egrep  "^$1.*sh$" &> /dev/null
   if [ $? -eq 0 ];then
     echo "a user can log system."
   fi

fi

11、写一个脚本,完成如下任务 :
(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
 (2) 复制目录时,才使用cp -r命令;
(3) 复制文件时使用cp命令;
(4) 复制链接文件时使用cp -d命令;
(5) 余下的所有类型,使用cp -a命令;
[root@localhost bin]# cat 9.sh 
#!/bin/bash
#
for file in /var/log/* ; do
  if [ -f $file ];then
    cp $file /tmp/test1-testn &> /dev/null
   elif [ -L $file ]; then
    cp -d $file /tmp/test1-testn &> /dev/null
   elif [ -d $file ]; then
    cp -r $file /tmp/test1-testn &> /dev/null
   else
    cp -a $file /tmp/test1-testn &> /dev/null 
  fi
done

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

(0)
wostopwostop
上一篇 2016-09-05
下一篇 2016-09-05

相关推荐

  • lvs——特殊的lvs-dr模型

    lvs-dr模型中:vip与dip/rip不在同一网段的实验环境设计及配置实现 实验拓扑: 提示:在路由器的内网IP接口上配置一个别名IP,此IP同VIP在同一网段 设计要点: VS上的VIP地址可以直接配置在DIP地址所在的网卡上,Linux主机上,一块网卡可以配置多个地址 RS上的VIP地址必须配置在lo接口上,并且还要关闭arp的响应和通告功能 外网接…

    Linux干货 2016-10-26
  • Linux基础之加密通讯过程详解

    加密通讯过程详解 第一阶段 客户端->服务器端 向服务器声明自己的加密通讯协议版本,ssl或者tls 支持的加密算法 支持的压缩算法 第二阶段 服务器端->客户端 向客户端确认使用的加密通讯协议版本 确认的加密方法 确认压缩方法 服务器端证书 第三阶段 客户端->服务器端 客户端验证服务器端证书 发证机构 证书完整性 证书持有者 证书有效期…

    2017-09-16
  • Linux网络管理&脚本编程之执行流程、循环

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥(Bridge):是早期的两端口二层网络设备,用来连接不同网段。网桥的两个端口分别有一条独立的交换信道,不是共享一条背板总线,可隔离冲突域。网桥比集线器(Hub)性能更好,集线器上各端口都是共享同一条背板总线的。后来,网桥被具有更多端口、同时也可隔离冲突域的交换机(Switc…

    Linux干货 2016-11-14
  • 模块化的php编译amp和fpm化的php编译amp;

    编译安装amp 一,编译环境前准备工作 (1) 系统环境:CentOS 6,7     CentOS 6:apr, apr-util的版本为1.3.9,不适用于httpd-2.4;     CentOS 7:apr, a…

    Linux干货 2016-10-17
  • linux 入门基础 (二)

    主要内容包含有 文件查找、压缩和正则表达式,以及包是管理和安装。

    2017-09-10
  • N24_小天 学习宣言

    每周安排时间看视频,每天安排时间学习并跟进课程,认真记笔记,认真写博客。多动手操作,坚持不断。学习思维,提升自己解决问题的思路。多动手,多实践。

    Linux干货 2016-10-27

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-07 18:21

    非常的棒,脚本写得非常的漂亮,思路清晰,5题的排版需要注意一下。加油!