人志建,则无敌—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)
上一篇 2016-09-05 08:50
下一篇 2016-09-05 08:52

相关推荐

  • N26-第四周

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限。[root@localhost home]# cp -R /etc/skel/ /home/tuser1 && chmod -R g=,o= /home/tuser1 [root@localhost …

    Linux干货 2017-03-07
  • 19道小米网运维工程师笔试真题,你能通关吗?

    第一部分:Linux基础 题目1: 有一百个图片文件,它们的地址都是 http://down.xiaomi.com/img/1.png http://down.xiaomi.com/img/2.png … 一直到http://down.xiaomi.com/img/100.png 批量下载这100个图片文件,并找出其中大于500KB的文件。 题目2: 一个文…

    2017-09-08
  • N26-第八周作业-邢岩

    马哥门徒-N26-邢岩 1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;      在线的主机使用绿色显示;      不在线的主机使用红色显示; #!/bin/bash # Author: jeque# Des…

    2017-03-24
  • 第五周作业

    1、显示当前系统上root、fedora、或user1用户的默认shell;          [root@yangjifeng~]# grep -E “^(root|fedora|user1)\>” /etc/passwd | cut -d: -f…

    Linux干货 2017-09-04
  • 运维必备技能之磁盘管理

    磁盘分区的三个工具 1、fdisk交互式工具         fdisk /dev/sdb进入交互界面         交互式界面的几个常用命令        …

    Linux干货 2017-03-14
  • iptables实战笔记一

    iptables实战 1.开启防火墙 systemctl start firewalld 2.清空所有的默认规则,我们自己定义自己的规则 iptables -F 查看此时的iptables iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination Chain FOR…

    Linux干货 2017-06-13

评论列表(1条)

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

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