马哥教育网络班22期+第9周课程练习

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
    
      [root@test ~]# vim userlgin.sh 
      #!/bin/bash

      declare -i i=0
      declare -i j=0 
      while read line;do
              usershell=$(echo $line |cut -d: -f7)
              if [ "$usershell" == "/sbin/nologin" ];then
                      let i++
              else
                      let j++
              fi
      done </etc/passwd

      echo "the nologin user total:$i"
      echo "the not nologin user total:$j"
      root@test ~]# bash userlgin.sh 

2、写一个脚本
    (1) 获取当前主机的主机名,保存于hostname变量中;
    (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
    (3) 否则,则显示当前主机名;

      [root@test tmp]# vim hostname2.sh
      #!/bin/bash

      hostname=`hostname`
      if [ "$hostname" == "localhost" ]; then
              hostname www.magedu.com
              hostname=`hostname`
              echo $hostname
      else
              echo $hostname
      fi
      [root@test tmp]# bash hostname2.sh

3、写一个脚本,完成如下功能
    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
    (2) 如果存在,则显示此设备上的所有分区信息;

      [root@test tmp]# vim disk.sh 
      #!/bin/bash

      fdisk -s $1 &> /dev/null

      if [ $? -eq 0 ];then
              fdisk -l $1
      else
              echo "$1 is not exist"
      fi
      [root@test tmp]# bash disk.sh /dev/sdb1

4、写一个脚本,完成如下功能
   脚本能够接受一个参数;
   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
   (2) 如果参数1为yes,则显示继续执行脚本;
   (3) 否则,参数1为其它任意值,均执行非正常退出;

      [root@www tmp]# vim input2.sh   
      #!/bin/bash

      read -p "Please input yes or quit or other chars:" input
      case $input in
      quit)
              echo "safety exit"
              exit 0
              ;;
      yes)
              echo "continue"
              ;;
      *)
              echo "unsafety exit"
              ;;
      esac
      [root@www tmp]# bash input2.sh


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@www tmp]# vim input2.sh  
      #!/bin/bash

      arg=$1

      case $arg in
      gzip)
              tar -zcf /backups/etc-20160613.tar.gz /etc
              ;;
      bzip2)
              tar -jcf /backups/etc-20160613.tar.bz2  /etc
              ;;
      xz)
              tar -Jcf /backups/etc-20160613.tar.xz   /etc
              ;;
      *)
              echo "compress tool error,please input gzip,bzip2 or xz"
              exit 1
              ;;
      esac
      [root@www tmp]# vim input2.sh
  
6、写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;
   (4) 其它为无法判断;

      [root@www tmp]# vim filetype2.sh
      #!/bin/bash

      if [ $# -lt 1 ]; then
              echo "At least one word."
              exit 1
      fi

      if [ -f $1 ];then
              echo "You can access it."
      elif [ -L $1 ];then
              echo "it's a file path"
      elif [ -d $1 ];then
              echo "You can use command cd into the file."
      else
              echo "it's an unknow file"
      fi
      [root@www tmp]# bash filetype2.sh /etc

7、写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
   (2) 否则,显示现有的主机名即可;

      [root@www tmp]# vim hostname3.sh
      #!/bin/bash

      hostname=`hostname`
      if [ -z "$hostname" -o "$hostname" == "localhost" -o "$hostname" == "(none)" ];
       then        hostname mail.magedu.com
              hostname=`hostname`
              echo $hostname
      else
              echo $hostname
      fi
      [root@www tmp]# bash -x hostname3.sh


8、写一脚本,接受一个用户名为参数;
   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
   (3) 否则,则显示其为普通用户;

      [root@www tmp]# vim userid.sh
      #!/bin/bash
      if [ $# -lt 1 ];then
              echo "Please input an username"
              exit 1
      fi
      id $1 &> /dev/null
      if [ $? -eq 0 ] ;then
              uid=`id -u $1`
              if [ $uid -eq 0 ];then
                      echo "This is an administrator user"
              elif [ $uid -gt 0 -a $uid -lt 500 ];then
                      echo "This is a system user"
              else
                      echo "This is a common user"
              fi
      else
              echo "$1 is not exist"
      fi
      [root@www tmp]# bash userid.sh


10、写一个脚本,传递一个用户名参数给脚本;
   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
   (2) 否则,则显示无法登录系统;

      [root@mail tmp]# vim userlg.sh
      #!/bin/bash

      if [ $# -lt 1 ];then
              echo "Please input an username"
              exit 1
      fi
      id $1 &> /dev/null

      if [ $? -eq 0 ];then
              uid=`grep "^$1\>" /etc/passwd |cut -d: -f3`
              shell=`grep "^$1\>" /etc/passwd |cut -d: -f7|grep -o "sh$"`
              if [ $uid -eq 0 ];then
                      echo "the user is an administrator user"
              elif [  $uid -ge 500 -a "$shell" == "sh" ];then
                      echo "a user can log system"
              else
                      echo "the user can't log system"
              fi
      else
              echo "$1 is not exist"
      fi
      [root@mail tmp]# bash -x userlg.sh user1


11、写一个脚本,完成如下任务 :
   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;
   (5) 余下的所有类型,使用cp -a命令;

      [root@mail tmp]# vim  cpfile.sh 
      #!/bin/bash

      file=/var/log/*
      for i in $file;do
              if [ -f $i ];then
                      echo "$i is common file"
                      cp $i /tmp/test1-testn/
              elif [ -L $i ];then
                      echo "$i is link file"
                      cp -d $i /tmp/test1-testn/
              elif [ -d $i ];then
                      echo "$i is directory"
                      cp -r $i /tmp/test1-testn
              else
                      echo "$i is other file"
                      cp -a $i /tmp/test1-testn
              fi
      done
      [root@mail tmp]# bash cpfile.sh

原创文章,作者:N22_上海_长清,如若转载,请注明出处:http://www.178linux.com/53353

(0)
N22_上海_长清N22_上海_长清
上一篇 2016-10-24 09:11
下一篇 2016-10-24 09:11

相关推荐

  • 8.1_Linux管道的使用和用户管理

    什么是管道? 管道就是把命令1的标准输出发送给命令2的标准输入,把命令2发的标准输出发送给命令3的标准输入。。。 最后一个命令会在当前shell进程的子shell进程中执行用来 管道实现了使用目的单一的小程序,组合小程序完成复杂的任务 管道需要配合其他命令的使用 tr命令的使用,常常用于配合管道     -c或–co…

    Linux干货 2016-08-10
  • 整型、浮点型在内存中的存储方式

      在学习C语言的过程中,有时候会想,数据到底是以什么样的方式在内存中存储的呢?经过一段时间的查阅资料,小编终于整理了一些…… 以VC6.0编译器为测试环境(int型为4Byte) 先简介一下大小端模式,具体资料可参考 http://blog.csdn.net/ce123_zhouwei/article/details/6971544  …

    Linux干货 2016-01-14
  • ansible

    运维工具介绍 Ansible Ansible优点:  Agentless       (去中心化)  Stupied Simple (上手简单,学习曲线平滑)  SSH by default (安全,无需安装客户端)  YAML no code,定制剧本 (语法简单,维护方便) 自动工具衍…

    Linux干货 2017-05-23
  • 计算机基础入门及Pyhton基础

    编程基础 计算机基础 程序(program识别并执行的指令 计算机是执行程序的机器 现代计算机 艾伦·麦席森·图灵奠定了现在计算机的逻辑工作方式的基础 冯·诺依曼提出计算机应该有五大部件组成:输入/输出设备、运算器、控制器、输出设备。这是现代计算机基本框架。 语言基础 计算机语言:与计算机之间交互的语言 机器语言:一定位数的二进制代码成为机器指令,指令集合则…

    2017-09-16
  • class15磁盘管理(二) 高级磁盘管理(一)

    挂载点和/etc/fstab 配置文件系统体系 被mount、fsck和其它程序使用 系统重启时保留文件系统体系 可以在设备栏使用文件系统卷标 使用mount -a命令挂载  /etc/fstab 中的所有文件系统 文件挂载配置文件 /etc/fstab每行定义一个要挂载的文件系统;   &nbsp…

    Linux干货 2016-09-05
  • 磁盘阵列RAID

    磁盘阵列RAID 什么是RAID RAID的全称是Redundant Array of Inexpensive Disks 可以释义为 廉价磁盘冗余阵列,后来的RAID里的字母I被认为是Independent,即独立的磁盘冗余阵列。 RAID的作用 由于服务器中磁盘读写率太大,所以磁盘经常损坏,raid可以让坏了一块磁盘时,服务器不至于岩机。当然这只是其中的…

    Linux干货 2017-06-18

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-02 14:41

    完成的非常好,给出了详细操作步骤,脚本思路清晰,能把排版在完善一下就更完美了,加油!