马哥教育网络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 08:51
下一篇 2016-09-05 08:51

相关推荐

  • 推荐-LVS原理详解

    LVS原理详解 LVS原理详解 LVS简介 LVS结构与工作原理 一.LVS的结构 二.LVS内核模型 三.LVS的包转发模型 1.NAT模型: 2.DR模型: 3.TUN模型: 四.LVS的调度算法 LVS的调度算法分为静态与动态两类。 1.静态算法(4种):只根据算法进行调度 而不考虑后端服务器的实际连接情况和负载情况 2.动态算法(6种):前端的调度器…

    Linux干货 2016-03-27
  • 文本处理三剑客之Sed(行编辑器)

    sed(流编辑器,行编辑器)     是一种流编辑器,一次处理一行内容,处理时把当前处理的行存储在临时缓存中,成为“模式空间”,接着用sed命令处理缓冲区的内容,处理完毕后吧缓冲区内容输出到屏幕接着处理下一行。这样不断重复到末尾。文件内容并没有改变。除非使用重定向输出才会改变内容。 sed:行编辑器(全屏编辑器:vi)   &n…

    Linux干货 2016-08-15
  • 马哥教育网络班22期+第2周课程练习

    1.linux上的文件管理类命令有哪些,其常用的使用方法及其相关示例演示    cp  复制文件  :单源;多源(目标必须是目录)        cp  /etc/fstab /tmp/hi.txt     -i 覆盖之前提醒用户确认   …

    Linux干货 2016-08-21
  • GREP EGREP的用法及正则表达式和扩展正则表达式简述

    egrep grep的使用方法 正则表达式及扩展表达式简述 使用事例   grep egrep都是文本搜索工具,可以把符合模式的行或字符显示出来,而这些模式grep一般使用正则表达式进行匹配,而egrep使用扩展正则表达式来进行匹配的。 grep及egrep的使用方法:           &nbs…

    Linux干货 2015-07-27
  • yum更多用法及源码编译安装apache和当天作业

    一、yum的更多用法 上篇文章介绍了yum的基础用法及yum源的基本配置,除此之外yum还有更多的用法,以下将继续介绍yum的使用方法。 1、yum-config-manager工具,该工具可以自动生成yum的repo文件,而不需要手动创建编写,使用方法如下: 用法:yum-config-manager [options] [section] -add-re…

    Linux干货 2016-08-24
  • 马哥教育网络19期+第十八周课程练习

    1、为LNMP架构添加memcached支持,并完成对缓存效果的测试报告; LNMP的安装过程不再赘述. # yum install -y memcached # cat /etc/sysconfig/memcached  PORT="11211" &nbsp…

    Linux干货 2016-09-19

评论列表(1条)

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

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