人志建,则无敌—if、case练习

马哥21期网络班-9周博客作业

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

#!/bin/bash
for i in `cut -d: -f7 /etc/passwd`; do
        if [ $i == /sbin/nologin ]; then
                let nologin++       
        else
                let sh++     
        fi  
 done
 echo "login user:$sh"echo "nologin user:$nologin"

2、写一个脚本

(1) 获取当前主机的主机名,保存于hostname变量中;

(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com

(3) 否则,则显示当前主机名;

#!/bin/bash
name=`hostname`
if [ $name == localhost ];then
        hostname www.magedu.com
else
        echo "$name"
fi

3、写一个脚本,完成如下功能

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

(2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
df -l $1 &> /dev/null
if [ `echo $?` = 0 ];then
        fdisk -l | grep "$1"
else
        echo "Enter dir no zai."
fi

4、写一个脚本,完成如下功能

脚本能够接受一个参数;

(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;

(2) 如果参数1为yes,则显示继续执行脚本;

(3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
[ $# -eq 0 ] && echo "Enter something!" && exit 1
case $1 in  
    "quit")        
        echo "scripts quit"
        exit 0
        ;;  
    "yes")        
        echo "continue the scripts"
        ;;  
    *)  
        echo "Abnormal exit "
        exit 1 
        ;;  
esac

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
[ $# -eq 0 ] && echo "Enter compression tool!" && exit 1
case $i in
        "gzip")
                tar -cfz /etc/ /backups/etc-20160613.tar.gz
                ;;  
        "bzip2")
                tar -cfj /etc/ /backups/etc-20160613.tar.bz2
                ;;  
        "xz")
                tar -cfJ /etc/ //backups/etc-20160613.tar.xz
                ;;  
                    
        *)  
                echo "Error tool."
                exit 0
                ;;  
esac

6、写一个脚本,接受一个路径参数:

(1) 如果为普通文件,则说明其可被正常访问;

(2) 如果是目录文件,则说明可对其使用cd命令;

(3) 如果为符号链接文件,则说明是个访问路径;

(4) 其它为无法判断;

#!/bin/bash
if [ -f $1 ]; then
        echo "This is file normal access."
elif [ -d $1 ]; then
        echo "This id folder use of cd."
elif [ -L $1 ]; then
        echo "This is access path."
else 
        echo "unknown."
fi

7、写一个脚本,取得当前主机的主机名,判断

(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com

(2) 否则,显示现有的主机名即可;

#!/bin/bash
if [ `hostname` == localhost ] && [ `hostname` == none ]; then
        hostname mail.magedu.comelse
        hostnamefi

8、写一脚本,接受一个用户名为参数;

(1) 如果用户的id号为0,则显示其为管理员;

(2) 如果用户的id号大于0且小于500,则显示其为系统用户;

(3) 否则,则显示其为普通用户;

#!/bin/bash
id $1 &> /dev/null
if [ $? -eq 0 ]; then
        id=`grep "^$1" /etc/passwd | cut -d: -f3`        
        if [ $id -eq 0 ]; then
                echo "This root"
        elif [ $id -le 500 ]; then
                echo "This is system"
        else 
                echo "general user"
        fi
fi

10、写一个脚本,传递一个用户名参数给脚本;

(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

(2) 否则,则显示无法登录系统;

#!/bin/bash 

if [ $# -eq 0 ]; then
        echo "Enter user."
        exit 1fiid $1 &> /dev/null
        if [ $? -eq 0 ]; then
        id=`grep "^$1.*sh$" /etc/passwd | cut -d: -f3`        
        if [ -n $id ] && [ $id -ge 500 ]; then
                echo "a user can log system."
        else 
                echo "Unable to log in."
        fi  
 else
      echo "Enter user."
      exit 2
 fi

11、写一个脚本,完成如下任务 :

(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;

(2) 复制目录时,才使用cp -r命令;

(3) 复制文件时使用cp命令;

(4) 复制链接文件时使用cp -d命令;

(5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
dir="/var/log/"
des="/tmp/test1-testn"
mkdir $des
for i in `ls $dir`; do
    if [ -d $i ]; then 
        cp -r $dir$i $des/    
        elif [ -f $i ]; then
        cp $dir$i $des/    
        elif [ -s $i ]; then
        cp -d $dir$i $des/    
        else
        cp -a $dir$i $des/    
        fi  
    done

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

(0)
N21_志建N21_志建
上一篇 2016-09-05 08:51
下一篇 2016-09-05 08:51

相关推荐

  • ​Linux 硬连接与软连接的区别

        Linux 硬连接与软连接的区别                        &…

    Linux干货 2016-10-25
  • mariadb之再次演练

    架构图如下: 1.按照架构图所示,准备机器,做好时间同步,主机名解析 192.168.42.150 node1 [proxySQL keepalived]192.168.42.151 node2 [proxySQL keepalived]192.168.42.152 node3 [mysql-master wha]192.168.42.153 node4 […

    Linux干货 2016-03-06
  • 小工具(维基百科)

     维基百科  方法:复制C:\Windows\System32\drivers\etc到我的电脑地址栏               打开hosts,选择文本文档方式打开              19…

    Linux干货 2017-07-10
  • linux 常用命令

    命令1 rm 作用: 删除指定的文件  选项 作用 -f 强制删除 -i 删除前提示询问 -r  或  -R 递归处理,删除指定目录下的所有文件包括子目录 -v 显示指令执行过程 -d 直接把删除目录的硬连接数据删成零,删除该目录 命令2 终端(tty): 终端类型 开启方式 物理终端 console 控制台console 虚拟…

    Linux干货 2017-07-13
  • HTTP详解(2)-请求、响应、缓存

    1. HTTP请求格式              做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干。HTTP协议传输的消息也是这样规定的…

    Linux干货 2015-04-04
  • Linux文本处理三剑客之GNU awk的使用

    awk: Aho, Weinberger, Kernighan,报告生成器,格式化文本输出 有多种版本:New awk(nawk),GNU awk(gawk) gawk –模式扫描和处理语言 基本用法:     awk[options] ‘program’ var=value file…   &nb…

    Linux干货 2016-10-06

评论列表(1条)

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

    完成的非常的好,排版也非常的漂亮,在变量与字符串之间比较时,字符串应加上”” 或 ”,加油