马哥教育网络班22期+第九周课程练习

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

#!/bin/bash
declare -i sum1=0
declare -i sum2=0
shells=$(cat /etc/passwd | cut -d: -f7)
for i in $shells;do
    if [[ $i == "/sbin/nologin" ]];then
        sum1+=1
    fi
    
    if [[ $i != "/sbin/nologin" ]];then
        sum2+=1    
    fi
done
echo "可登录shell用户有$sum1个"
echo "不可登录shell用户有$sum2个"

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

#!/bin/bash
hostName=$(hostname)
if [[ $hostName == "localhost" ]];then
    hostname www.magedu.com
    echo "当前主机名修改为了:www.magedu.com"
else
    hostname
fi

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

#!/bin/bash
read -p "Enter a disk path(/dev/[sh]d[a-z]): " disk_path
if fdisk -l $disk_path &> /dev/null;then
    fdisk -l $disk_path
else
    echo "this disk is not existed!"
fi

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

#!/bin/bash
while true;do
    read -p "Enter an argument(yes or quit): " arg1
    if [ -z "$arg1" -o "$arg1" == "yes" ];then
        continue
    elif [[ $arg1 == "quit" ]];then
        echo "Normal exit!"
        break
    else
        echo "Abnormal exit!"
        exit 1        
    fi
done

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
while true;do
    read -p "Enter an argument(gzip,bzip2,xz): " arg1
    if [ -z $arg1 ];then
        continue
    elif [[ $arg1 == "gzip" ]];then
        tar zcf /backups/etc-20160613.tar.gz /etc &> /dev/null
        echo "Finished"
        break
    elif [[ $arg1 == "bzip2" ]];then
        tar jcf /backups/etc-20160613.tar.bz2 /etc &> /dev/null
        echo "Finished"
        break
    elif [[ $arg1 == "xz" ]];then
        tar Jcf /backups/etc-20160613.tar.xz /etc &> /dev/null
        echo "Finished"
        break
    else
        echo "Error compression tool!"
        exit 1
    fi
done

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

#!/bin/bash
while true;do
    read -p "Enter a filepath: " path_arg
    if [ -z $path_arg ];then
        continue
    elif [ -L $path_arg ];then
        echo "This is a access path!"
    elif [ -d $path_arg ];then
        echo "Can use the 'cd' command!"
    elif [ -f $path_arg ];then
        echo "Can be accessed normally!"
    else
        echo "Error, can't judge!"
        break
    fi
done

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

#!/bin/bash
hostName=$(hostname)
if [ -z $hostName -o $hostName == "localhost" -o $hostName == "(none)" ];then
    hostname mail.magedu.com
    echo "当前主机名修改为了:mail.magedu.com"
else
    hostname
fi

8、写一脚本,接受一个用户名为参数;
   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
   (3) 否则,则显示其为普通用户;

#!/bin/bash
[ -z $1 ] && echo "at least one username!" && exit 1
if id $1 &> /dev/null;then
    id_num=$(id -u $1)
    if [ $id_num -eq 0 ];then
        echo "This is administrator!"
    elif [ $id_num -gt 0 -a $id_num -lt 500 ];then
        echo "This is systemt user!"
    else
        echo "This is common user!"
    fi
else
    echo "user $1 is not existed!"
fi

9、写一个脚本,传递一个用户名参数给脚本;
   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
   (2) 否则,则显示无法登录系统;

#!/bin/bash
[ -z $1 ] && echo "at least one username!" && exit 1
if id $1 &> /dev/null;then
    if [ $(id -u $1) -gt 500 ];then
        if grep "^$1.*sh$" /etc/passwd &> /dev/null;then
            echo "This user can login system!"
        else
            echo "This user can't login system!"
        fi
    fi
else
    echo "user $1 is not existed!"
fi

10、写一个脚本,完成如下任务 :
   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;
   (5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
declare -i n=1
for i in /var/log/*;do
    [ -d /tmp/test$n ] || mkdir /tmp/test$n
    if [ -L $i ];then
        cp -d $i /tmp/test$n
    elif [ -d $i ];then
        cp -r $i /tmp/test$n
    elif [ -f $i ];then
        cp $i /tmp/test$n
    else
        cp -a $i /tmp/test$n
    fi
    let n++
done
echo "Copy finished!"

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

(0)
萝卜萝卜
上一篇 2016-11-28
下一篇 2016-11-28

相关推荐

  • rpm详解

    rpm详解 rpm详解 rpm使用方法 rpm起源 rpm是什么 rpm命名格式 rpm优缺点 rpm获取注意 rpm命令使用 rpm起源  由于在linux中安装应用程序需要源码包编译安装,对于非专业人员而言难度太大,因而出现一种将源码编译好的二进制程序,库文件,配置文件,帮助文件等打包成一个或多个特定格式的程序包,而管理这类包的工具之一,则称为…

    Linux干货 2016-12-23
  • vim编辑器

      Linux文本编辑器vim     Linux下的编辑器最常用的就是vim或者vi文本编辑。vi和vim编辑器的区别是vim是vi的改进版本,在vi编辑器的基础上上扩展了很多实用的功能。 vim的使用 用vim打开文件:     vim [options] [file ..]…

    Linux干货 2016-12-04
  • 第八周作业

    1. 写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;     在线的主机使用绿色显示;     不在线的主机使用红色显示; #!/bin/bash # for i in {1..254};do         pi…

    Linux干货 2017-02-24
  • 马哥教育网络班21期第9周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash # declare -i nologin_user;other_user all_users=`awk -F':'…

    Linux干货 2016-09-19
  • Linux磁盘管理(一)

      磁盘,计算机最常见的存储硬件设备。对Linux而言,其哲学思想之一就是一切皆文件,因此linux之上所有设备的操作都是通过文件接口来实现的,也就是说在访问设备的时候就如同访问一个文件一样,因为他们使用的都是文件系统接口。 向磁盘中写入数据的操作过程:   首先硬件设备在系统上会映射成一个一个文件,我们在对硬件中写入数据就相当…

    Linux干货 2016-08-30

评论列表(1条)

  • luoweiro
    luoweiro 2016-11-29 22:20

    第7题通过hostname修改主机名只是临时的,如果持久生效需要修改配置的,可以完善一下。