N26-博客作业-week9

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

#!/bin/bash 
#
declare -i nologin_num=0,login_num=0
for i in $(cut -d: -f7 /etc/passwd); do
    if [[ $i == "/sbin/nologin" ]]; then
        let nologin_num++
    else
        let login_num++
    fi
done

echo "Nologin shell num: $nologin_num"
echo "Login shell num: $login_num"

2、写一个脚本
    (1) 获取当前主机的主机名,保存于hostname变量中;
    (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
    (3) 否则,则显示当前主机名;

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

3、写一个脚本,完成如下功能
    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
    (2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
#
if [ $# -eq 0 ]; then
    echo "Please input a disk directory!"
    exit 1
fi

if df $1 &> /dev/null; then
    fdisk -l $1
else
    echo "No such disk!"
    exit 2
fi

4、写一个脚本,完成如下功能
   脚本能够接受一个参数;
   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
   (2) 如果参数1为yes,则显示继续执行脚本;
   (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
#
read -p "Please input your choice: " option

if [[ $option == "quit" ]]; then
    echo "Script is normally quited!"
    exit 0
elif [[ $option == "yes" ]]; then
    echo "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
#
cat << EOF
Please chose a tool:
gzip
bzip2
xz
======================
EOF

read -p "Your choice: " option

if [[ $option == "gzip" ]]; then
    tar -zcf /backups/etc-20160613.tar.gz /etc &> /dev/null
elif [[ $option == "bzip2" ]]; then
    tar -jcf /backups/etc-20160613.tar.bz2 /etc &> /dev/null
elif [[ $option == "xz" ]]; then
    tar -Jcf /backups/etc-20160613.tar.xz /etc &> /dev/null
else
    echo "Wrong tool!"
    exit 1
fi

6、写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;
   (4) 其它为无法判断;

#!/bin/bash
#
if [ $# -lt 1 ];then
    echo "At least one directory!"
    exit 1
fi

if ! [ -e $1 ];then
    echo "File is not exist!"
    exit 2
fi

if [ -f $1 ];then
    echo "This file can be access normally"
elif [ -d $1 ];then
    echo "This file can use command cd"
elif [ -L $1 ];then
    echo "This is an access path!"
else
    echo "Can not judge!"
fi

7、写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为””(none)””,则将其命名为mail.magedu.com;
   (2) 否则,显示现有的主机名即可;

#!/bin/bash
#
hostname=$(hostname)
if [ -z "$hostname" -o "$hostName" == "localhost.localdomain" -o "$hostname" == "localhost" ]; then   
    hostname mail.void.com
    echo "hostname has change to $(hostname)"
else
    hostname
fi

原创文章,作者:浙江-咲,如若转载,请注明出处:http://www.178linux.com/72271

(0)
浙江-咲浙江-咲
上一篇 2017-04-01
下一篇 2017-04-01

相关推荐

  • rpm命令详解及和yum之间的关系

        RPM 全名是“ RedHat Package Manager ”简称则为 RPM 顾名思义,当初这个软件管理的机制是由 Red Hat 这家公司发展出来的。 RPM 是以一种数据库记录的方式来将你所需要的软件安装到你的 Linux 系统的一套管理机制。    &nbsp…

    Linux干货 2016-08-21
  • proxy_pass转发路径

    一、引言 在nginx中配置proxy_pass时,proxy_pass后面的路径最后面加“/”和不加“/”会有所区别。加“/”时,nginx不会代理location部分,不加“/”时,nginx会同时代理location部分。下面通过实验来证明。 二、实验 实验环境简要说明:     node1为httpd服务器(1…

    Linux干货 2017-01-12
  • linux时间

    linux有两个时间:     系统时间和硬件时间 系统时间: 最简单的使用date命令显示当前系统的时间,命令行输入date。 # date Thu Jun  2 17:11:48 CST 2016 date的具体使用方式:     1. date格式显示:date …

    Linux干货 2016-06-03
  • 以洪荒之力打开linux德·摩根定律

    德·摩根定律让学习liunx的小伙伴绕得头疼,现在我就用洪荒之力为大家理顺一下这个定律。 由图片可以看出, A=4+3 B=3+2 非A=2+1 非B=4+1 A且B就是代表既是A又是B的地方,A和B都有色块3,所以色块3代表了A且B 同时,我们再看A=4+3 ,B=3+2,我们可以推断出,且运算的结果就是两个算数式中重复的数字。 A且B=3 A或B就是代表…

    Linux干货 2016-08-15
  • Linux 用户, 组和权限

    用户, 组和权限 Linux登陆需要用户名、密码。/etc/passwd 文件保存用户名。登录linux时,Linux 先查找 /etc/passwd 文件中是否有这个用户名,没有则跳出,有则读取用户名的user ID 、 group ID 、用户名对应的根目录路径以及所使用的 shell ,最后在 /etc/shadow 中核对该 UI…

    2017-07-22
  • 文本

    一,复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没 有任何访问权限; cp -r /etc/skel/ /home/tuser1 chmod -R go= /home/tuser1 二,编辑/etc/group文件,添加组hadoop; vim /etc/group hadoop:x:100…

    Linux干货 2016-11-20

评论列表(1条)

  • 马哥教育
    马哥教育 2017-04-10 16:22

    总结的很好,脚本部分可以给出运行结果,加油~~~