马哥教育21期网络班—第9周课程+练习—-成长进行时–不退步

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

#!/bin/bash 
declare -i other=0
declare -i nologin=0
    for i in $(cut -d: -f7 /etc/passwd);do
        if [ $i == /sbin/nologin ];then
            let nologin++
                else
            let other++
       fi
    done
echo "other user total:$other"echo "nologin user total:$nologin"
  • 2、写一个脚本

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

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

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

#!/bin/bash 
hostname=$(hostname)
if [ $hostname=='localhost' ];then
    hostname www.magedu.com   
    echo "hostnamme change : 'www.magedu.com'"
else
    echo 'hostname is: $hostname'
fi
  • 3、写一个脚本,完成如下功能

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

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

#!/bin/bash 
read -p "Enter dev and file:" dev
if [ -e $dev ];then
    fdisk -l $dev
else
    echo "This devfile is not exist"
fi
  • 4、写一个脚本,完成如下功能

  • 脚本能够接受一个参数;

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

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

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

#!/bin/bash
if [ $# -eq 0 ];then
    echo “Enter one option”
else
case $1
inquit)echo “exit nomal”
;;
yes)echo “go on”
;;
*)echo “exit unomal”
;;
esac
fi
  • 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
if  [ ! -e /backups ];then
mkdir /backups
fi
    read -p "please choose the Compression tool(gzip|bzip2|xz) " i
case $i in  
gzip)
        tar czvf /backups/etc-20160613.tar.gz /etc
;; 
bzip2)
        tar cjvf /backups/etc-20160613.tar.bz2 /etc
;; 
xz)
        tar cJvf /backups/etc-20160613.tar.xz /etc
;;
*)
        echo "please choose the correct tools"
        exit 1
;;
esac
  • 6、写一个脚本,接受一个路径参数:

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

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

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

  • (4) 其它为无法判断;

#!/bin/bash
read -p "please input something " n
if [ -f $n ];then
   echo "$n can be access"
elif [ -d $n ];then
   echo "$n can be use "cd" command"
elif [ -L $n ];then
   echo "this is a access path"
else
   echo "$n can't be judge" 
fi
  • 7、写一个脚本,取得当前主机的主机名,判断

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

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

#!/bin/bash
host=`hostname`
if [ “$host”=”localhost” -o -z “$host” –o “$host”=”none” ];then
hostnamemail.magedu.com
else
echo “this machine hostname exists, it is $host”
fi
  • 8、写一脚本,接受一个用户名为参数;

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

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

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

#!/bin/bash        
        read -p "input a user name:" username
        uid=$(grep "^$username" /etc/passwd |cut -d: -f3)
        if [ $uid -eq 0 ];then
        echo "this is root."
        elif [ $uid -gt 0 -a $uid -le 500 ];then
        echo "this is a system user."
        else 
        echo "this is a common user."
        fi
  • 10、写一个脚本,传递一个用户名参数给脚本;

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

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

 #!/bin/bash
if [ $# -eq 1 ] && id $1>/dev/null;then
uid=`grep "$1" /etc/passwd | cut -d: -f3`
 if [ $uid -eq 0 ];then
  echo "this is root"
 elif [ $uid -gt 0 ]&&[ $uid -lt 500 ];then
  echo "this is system user"
 elif [ $uid -gt 500 ];then
  echo "this is normal user"
 fi
 else
  echo "please input the exist username"
fi
  • 11、写一个脚本,完成如下任务 :

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

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

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

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

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

#!/bin/bash
if ! [ -d /tmp/test1-testn ];then
    mkdir -p /tmp/test1-testn
fi
for file in `ls /var/log`;do
    if [ -d $file ];then
        cp -r /var/log/$file /tmp/test1-testn
    elif [ -f $file ];then
        cp  /var/log/$file /tmp/test1-testn
    elif [ -L $file ];then
        cp -d /var/log/$file /tmp/test1-testn
    else
        cp -a /var/log/$file /tmp/test1-testn
    fi
done

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

(0)
N21_ DominicN21_ Dominic
上一篇 2016-09-05 08:51
下一篇 2016-09-05 08:51

相关推荐

  • 基于LNMP架构添加Memcached支持,并验证其缓存结果

    一 Memcached简介 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。它是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。 Memcached简洁而强大。它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存的很多问题。它的API兼容大部分流…

    Linux干货 2016-12-12
  • 简单的shell脚本结合awk实现防止对web服务的dos攻击

    一 实验环境         鄙人使用的是centos 6.8操作系统   需要安装iptables(常见的linux系统貌似都会自动安装iptables)这条可以忽略   awk也需要安装没有的话就用yum装下吧       &…

    2017-04-24
  • 第一周-2:Linux部分常用命令使用示例、查看命令行工具使用手册以及LinuxFHS部分目录简介

    一、部分命令使用示例: 1、ifconfg 在命令行界面下或系统配置脚本中用于配置、控制及查询TCP/IP网络接口的系统管理工具 [netartisan@localhost 桌面]$ ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 &n…

    Linux干货 2016-11-07
  • 常用命令之ls 详解

    ls详解 罗列目录内容权限,列出文件的信息列表(默认为当前目录)。 这个命令是Linux操作系统使用频率最高的命令,没有之一。熟练掌握这个命令的常用参数,可以提高学习、工作效率,因此需要认真对待。 1. 常用选项 – -a,–all:显示当前目录下全部文件(常用) [root@jingz ~]# ls -…

    Linux干货 2016-08-02
  • 有趣的bash脚本

    1、编写脚本/root/bin/createuser.sh,实现如下功能:使 用一个用户名做为参数,如果指定参数的用户存在,就显示 其存在,否则添加之;显示添加的用户的id号等信息 #!/bin/bash read -p “Please input username: ” n if id $n &> /dev/null;then echo “T…

    Linux干货 2017-08-25
  • 马哥教育网络班20期+第6周课程练习

    vim编辑器使用总结 一、文件的打开与关闭             打开文件:                 # vim [OPTION]… FILE…      …

    Linux干货 2016-07-17

评论列表(1条)

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

    脚本写得非常的漂亮,思路清晰,脚本的缩进注意一下哈,7题的if 判断 字符串之间的相等判断使用的是”==“