N22-妙手-第九周课程练习

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

#!/bin/bash
#

declare -i numberOfLoginUser=0
declare -i numberOfUnloginUser=0

while read lines; do

    shellType=${lines##*:}
    if [ "$shellType" == "/sbin/nologin" ]; then
        let numberOfUnloginUser++
    else
        let numberOfLoginUser++
    fi
done < /etc/passwd

echo "How many login shell: "$numberOfLoginUser"
echo "How many unlogin shell" $numberOfUnloginUser"

2、写一个脚本

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

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

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

#!/bin/bash
#

hostname=$(hostname)
if [ "$hostname" == "www.magedu.com" ]; then
    hostnamectl set-hostname localhost.localdomain
    echo $(hostname)
else
    echo $hostname
fi

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

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

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

#!/bin/bash
#
if [ -b $1 ]; then
    fdisk -l $1
else
    echo "Please send the device file"
fi

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

脚本能够接受一个参数;

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

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

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

#!/bin/bash

case $1 in
quit) 
    echo "Exit the shell script"
    exit 
    ;;
yes)
    echo "continue excute the shell script"
    ;;
*)
    echo "pleaes input quit or yes"
    exit 2
    ;;
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
#

format=$1

case $format in

gzip)
    tar -cf /backups/etc-20161013.tar.gz /etc/
    ;;
bzip2) 
    tar -jcf /backups/etc-21061013.tar.bz2 /etc/
    ;;
xz)    
    tar -Jcf /backups/etc-20161013.tar.xz /etc/
    ;;
*)
    echo "Please use the right compresss tool"
    exit 2
    ;;
esac

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

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

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

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

(4) 其它为无法判断;

#!/bin/bash
#
para1=$1

if [ -f $para1 ]; then
    echo "This file can be accessed normally."
elif [ -d $para1 ]; then
    echo "This file is directory file and can use cd command to this directory"
elif [ -L $para1 ]; then
    echo "This is a symbol link file"
else 
    echo "The file can not be identified"
fi

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

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

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

#!/bin/bash
#

hostName=$(hostname)

if [ -z "$hostName" ] || [ "$hostName" == "localhost" ] || [ "$hostName" == "(none)" ]; then
    hostnamectl set-hostname mail.magedu.com
    echo $(hostname)
else
    echo $(hostname)
fi

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

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

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

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

#!/bin/bash
#
read -p "Please Enter username: " username

if id $name >& /dev/null; then
    userid=$(id -u $username)
    if [ $userid -eq 0 ]; then
        echo "Root user"
    elif [ $userid -gt 0 ] && [ $userid -le 500 ]; then
        echo "System user"
    else
        echo "Common user"
    fi

else
    echo "please input valid username"
    exit 2
fi

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

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

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

#!/bin/bash
#

if [ $# -lt 1 ]; then
    echo "Please send at lease 1 username"
    exit 1
fi

if id $1 >& /dev/null; then
    userid=$(id -u $1)
    userinfo=$(cat /etc/passwd | grep $1)
    if [ $userid -gt 500 ] && [ "${userinfo: -2}" == "sh" ];then
        echo "a user can log system"
    else    
        echo "this user can not log to system"
    fi
else
    echo "Please input a valid username"
fi

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

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

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

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

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

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

#!/bin/bash
#

dir=/test/test1-testn

if !(( -d $dir )); then
    mkdir -pv /test/test1-testn
fi

for i in /tmp/*; do
    if [ -d $i ]; then
        cp -r $i $dir
    elif [ -f $i ]; then
        cp $i $dir
    elif [ -L $i ]; then
        cp -d $i $dir
    else
        cp -a $i $dir
    fi
done

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

赞 (0)
mxb93mxb93
上一篇 2016-10-19 14:19
下一篇 2016-10-19 14:19

相关推荐

  • Infobright高性能数据仓库

    1.  概述         Infobright是一款基于独特的专利知识网格技术的列式数据库。Infobright简单易用,快速安装部署,使用中无需复杂操作,能大幅度减少管理工作;在应对50TB甚至更多数据量进行多并发复杂查询时,更能够显示出令人惊叹的速度。相比于MySQL,其查询速度提升了数倍甚…

    Linux干货 2015-04-05
  • Linux的发展史

    前言: Linux是什么?    我们知道Linux这玩意儿是在计算机上面运作的,所以说Linux就是一组软件。问题是这个软件是操作系统还是应用程序? 且Linux可以在哪些种类的计算机上面运作?而Linux源自哪里?为什么Linux 还不用钱?这些我们都得来谈一谈先!      计算机系…

    Linux干货 2016-10-13
  • 关于nginx状态监控字段的个人见解(求真相)

    今天看到了马哥视频其中一节对nginx状态监控信息的介绍,对视频ppt上的监控字段解析产生了一些疑问,ppt内容如下: active connections – 活跃的连接数量server accepts handled requests — 总共处理了xxx个连接 , 成功创建xxx次握手, 总共处理了xxx个请求reading — 读取客户端的连接数.w…

    Linux干货 2016-07-12
  • mysql进阶之MySQL查询

    一、MySQL多表查询和子查询 别名:as        表别名        字段别名        查询结果亦可命名别名   联结查询:事先将两张或多张表join,根…

    2016-11-18
  • linux下小练习(2)

    1、编写脚本/bin/per.sh,判断当前用户对指定的参数文件,  是否不可读并且不可写                         2、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充 许…

    Linux干货 2016-08-15
  • 马哥教育网络班21期+第一周课程练习

    1,描述计算机的组成及其功能。 2.按系列罗列linux的发行版,并描述不同发行版之间的练习与区别。 3.描述linux的哲学思想,并按照自己的理解对其进行解释性描述。 1.       一切皆文件,计算机中所有的文件目录,        包括计算机的硬件设备显示为文件格式。 2…

    Linux干货 2016-07-07

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-25 13:28

    注意审题,不要理解反了,好好看看第二个问题的要求