第九周脚本练习

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

awk -F: '{if($NF!="/sbin/nologin") print $1}' /etc/passwd | wc -l

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

   #!/bin/bash

    a=$(hostname)

    [ “$a” == “localhost” ] && hostname www.magedu.com

    hostname

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

#!/bin/bash
[ -b $1 ] && fdisk -l




[root@localhost dev]# bash  devic.sh /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a1d43

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/mapper/cl-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cl-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost dev]# bash  devic.sh /dev/sdf
[root@localhost dev]# echo $?
1

~

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

#!/bin/bash
[ "$1" == "quit" ] && echo " exit the processing " && exit
[ "$1" == "yes" ] && echo " continue processing the scripts "
[ "$1" != "quit" -a "$1" != "yes" ] && echo " error " && exit



[root@localhost tmp]# bash  test3.sh quit
 exit the processing 
[root@localhost tmp]# bash  test3.sh yes
 continue processing the scripts 
[root@localhost tmp]# bash  test3.sh 1334
 error 
[root@localhost tmp]#

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
    [ "$1" == "gzip" ]  && tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc
    [ "$1" == "bzip2" ] && tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc
    [ "$1" == "xz" ] && tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc
    [ "$1" != "gzip"  -a "$1" != "bzip2" -a "$1" != "xz" ] && echo "error" && exit

    [root@localhost backups]# bash tartool.sh xz
    tar: Removing leading `/' from member names
    [root@localhost backups]# ls
    etc                   etc-20170322.tar.gz  etc-20172203  tartool.sh
    etc-20170322.tar.bz2  etc-20170322.tar.xz  etc-.tar.xz

6、写一个脚本,接受一个路径参数: (1) 如果为普通文件,则说明其可被正常访问; (2) 如果是目录文件,则说明可对其使用cd命令; (3) 如果为符号链接文件,则说明是个访问路径; (4) 其它为无法判断;

#!/bin/bash
    if [ -f $1 ]; then
       echo " this file can be visited "
    elif [ -d $1 ]; then
       echo " it is directory and  and can be excuted by command cd "
    elif [ -L $1 ]; then
        echo " it is a path to visit "
    elif [ -e $1 ]; then
         echo " file exit but can not identify filetype"
    else
         echo " can not be identified"
    fi

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

#!/bin/bash
a=$(hostname)
[ "$a" == "localhost" -o "$a" == "none" ] && hostname www.magedu.com
hostname

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

#!/bin/bash
    declare -i userid
    userid=$(id -u $1)
    if [ $userid -eq 0 ]; then
         echo "$1 is administer "
     elif [ $userid -gt 0 -a $userid -lt 500 ]; then
         echo "$1 is system user"
     else
         echo "$1 is a common user"
    fi

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

#!/bin/bash
if ! id $1 &> /dev/null;then
    echo " no such user,will exit " 
    exit 2
else
      userid=$(id -u $1)
      if [ $userid -ge 500 ]  && grep "^$1.*sh$" /etc/passwd &> /dev/null;then
      echo "$1  is a user can log system"
      else
      echo "can not log in system"
      fi
fi

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

#!/bin/bash
    declare -i di=1
    for j in  /var/log/*
          do
          [  -d /tmp/test$di ] ||  mkdir -p /tmp/test$di
           if [ -d $j ]; then
                cp -r $j /tmp/test$di
           elif [ -f $j ]; then
                cp $j /tmp/test$di
           elif [ -L $J ] ; then
                cp -d $j /tmp/test$di
           else
                cp -a $j /tmp/test$di
            fi
          let di++
    done

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

(0)
diglinuxdiglinux
上一篇 2017-03-30
下一篇 2017-03-30

相关推荐

  • 在VMware workstations中安装CentOS-7-x86_64-Everything-1611.ISO

      对于最近在Linux学习的过程中,许多同学对CentOS系统的详细配置还不熟悉,于是我编写此详细步骤供大家参考。 工具: VMware workstations; CentOS-7-x86_64-Everything-1611.iso; 准备工作:  1、先要下载一个系统镜像centOS-7-x86_64-Everythin…

    2017-07-11
  • 第三周作业

    博客具体内容请移步博客园: http://www.cnblogs.com/ITOps/p/6204549.html

    Linux干货 2016-12-20
  • 实验删除分区表

    首先我们先对分区表做个备份 dd if=/dev/sda of=/app/mbr bs=1 count=512 把备份考到另一台设备上,不拷贝的话这台设备开不了机无法使用备份的文件 清除分区表 接下来需要在客户端操作 使用光盘救援模式启动 启用网络 选网卡 自动获取ip后默认下一步 接下来就是把刚刚拷到另一台设备上的文件拷贝回来   注:这是在光盘的根目录下…

    2017-12-05
  • awk的详细用法

    awk简介 awk是一种编程语言,也可以说是一个脚本语言解释器,主要是用来实现文本的格式化输出。awk是取了其三位作者名字的首字母,即Aho,Weinberger和Kernighan。awk在Linux上是基于GNU协议实现的,即GNU awk,也就是gawk。 awk的工作模式 遍历整个文件,从第一行到最后一行。每次仅读取文件的一行,依照用户指定的输入分隔…

    Linux干货 2016-11-14
  • LVS-几种负载方式的区别

    LVS的原理很重要,很重要。每一个知识点都要做到熟记与脑,谨记于心,张口就来。 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。 使用集群技术和Linux操作系统实现一个高性能、高可用的服务器. 很好的可伸缩性(Scal…

    Linux干货 2017-05-11
  • Shell脚本自动部署(编译)LAMP平台

    Shell脚本自动部署(编译)LAMP平台 Shell脚本自动部署(编译)LAMP平台 为什么要用脚本进行部署? 脚本功能介绍 笔者环境 准备工作 声明 使用测试 脚本代码 Shell脚本自动部署(编译)LAMP平台 LAMP是当下非常流行的一套Web架构,我们可以在GNU/Linux下通过其他人打包的程序包来进行安装; 但是在生产环境中,很多时候都需要我们…

    Linux干货 2016-03-26