第九周练习

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

#!/bin/bash
#
nologin=$(awk -F: '$NF=="/sbin/nologin"{print $NF}' /etc/passwd | wc -l)
all=$(cat /etc/passwd | wc -l)
login=$[$all-$nologin]
echo "login user:$login,nologin user:$nologin"

2、写一个脚本

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

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

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

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

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter,please try it again"
    exit 1
fi
for i in $*;do
    if [[ -a $i ]];then
        fdisk -l $i
    else
        echo "the path is not existing or not a block special"
    fi
done

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

脚本能够接受一个参数;

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter"
    exit 1
fi
if [[ "$1" == "quit" || "$1" == "QUIT" ]];then
    echo "This script is quiting"
    exit 0
elif [[ "$1" == "yes" || "$1" == "YES" ]];then
    echo "This script is still running"
else
    exit 2
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 [ $# -eq 0 ];then
    echo "At least one parameter"
    exit 1
fi
mkdir -pv /backups
if [[ "$1" == "gzip" || "$1" == "GZIP" ]];then
    tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc
elif [[ "$1" == "bzip2" || "$1" == "BZIP2" ]];then
    tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc
elif [[ "$1" == "xz" || "$1" == "XZ" ]];then
    tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc
else
    echo "Error"
    exit 0
fi

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

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

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

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

(4) 其它为无法判断;

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter"
    exit 1
fi
for i in $*;do
    if [ -f $i ];then
        echo "the file is normal"
    elif [ -d $i ];then
        echo "you can use cd enter this directory"
    elif [ -L $i ];then
        echo "the link is normal"
    else
        echo "unkown error"
    fi
done

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

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

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

#!/bin/bash
#
hostname=$(hostname)
if [[ -z "$hostname" || "$hostname" == "localhost" || "$hostname" == "(none)" ]];then
    hostname mail.magedu.com
else
    echo $hostname
fi

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

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter"
    exit 1
fi
for i in $*;do
    uid=$(grep -E "^$i" /etc/passwd | cut -d: -f3)
    if [ $uid -eq 0 ];then
        echo "$i is root"
    elif [ $uid -ne 0 && $uid -lt 500 ];then
        echo "$i is a system user"
    else
        echo "$i is a common user"
    fi
done

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
    echo "At least one parameter"
    exit 1
fi
for i in $*;do
    uid=$(grep -E "^$i" /etc/passwd | cut -d: -f3)
    shell=$(grep -E "^$i" /etc/passwd | cut -d: -f7)
    if [[ $uid -ge 500 && "$shell" =~ .*sh ]];then
        echo "$i can login"
    else
        echo "$i can't login"
    fi
done

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

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

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

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

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

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

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

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

(0)
N24_涩味N24_涩味
上一篇 2016-12-21
下一篇 2016-12-21

相关推荐

  • 文件系统权限管理

    文件系统权限管理 文件及目录权限 文件系统上的权限是针对访问者的 访问者:     owner:属主,u     group:属组,g     other:其他,o 针对每个访问者有三种权限 r:readable w:writeable x…

    Linux干货 2016-11-05
  • Linux网络协议命令介绍(3)

      在上一章中,我们谈到了网络协议以及TCP传输层说明,下面我们来介绍一下序列号和确认好有什么区别之外,以及基本的网络命令。 1、序列号和确认号有什么区别? 2、网络功能 3、网络命令 4、配置文件 1、序列号和确认号有什么区别?   发送方与接收方的发送请求,把SYN设置为1,TCP报文出去都有个号码,随机生成称为序…

    Linux干货 2016-11-29
  • 马哥教育网络班N22期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 2、取出最后登录到当前系统的用户的相关信息。     [root@zabbix ~]# id $(who | cut -d' ' -f1 | tail -n 1)    &nbs…

    Linux干货 2016-08-30
  • Bash shell 脚本编程全攻略(上篇)

    Bash shell 脚本编程全攻略(上篇)   什么是shell脚本呢? 当命令不在命令行上执行,而是通过一个文件执行时,该文件就称为shell脚本,脚本以非交互的方式运行。Shell脚本把命令通过一些语法组织起来,便能实现特定的功能。   Shell脚本主要运用在系统运维中,主要功能有: 自动化常用命令; 执行系统管理和故障排除; 创…

    Linux干货 2016-08-29
  • grep,find

    显示当前系统上root、fedora、user用户的默认shell: 2.找出/etc/rc.d/init.d/functions文中单词后跟一组小括号的行 3.使用echo命令输出一个绝对路径,使用grep取出其基名: 4.找出ifconfig命令结果中的1-255之间的数字 5.写一个模式,匹配合理的ip地址 6.写一个模式,匹配出邮箱地址 7.查找/v…

    Linux干货 2016-11-27
  • 第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。 4、复制/etc/sk…

    Linux干货 2017-02-07