马哥教育网络班20期 第9周课程练习

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

#!/bin/bash
#the purpose of program :
#
#08/11/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
awk -F":" 'BEGIN{num1=0;num2=0;}{if($NF != "/sbin/nologin") {printf "%s is login user\n",$1 ;num1+=1;} else {printf "%s is nologin user \n",$1;num2+=1;}}END{printf "loguser number :%d \n nologin user number:%d \n",$num1,$numn2; }' /etc/passwd

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
hostname=$(hostname )
if [ $hostname == "localhost" ];then
    hostname www.magedu.com
else
    echo "$hostname"   
fi

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input a dev directory: " devdir
if [ -b $devdir ];then
    fdisk -l |grep "${devdir}" 
fi

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
case $1 in 
    "quit")
        echo "scripts quit"
        exit 0
        ;;
    "yes")
        echo "continue the scripts"
        ;;
    *)
        echo "Abnormal exit "
        exit 1 
        ;;
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
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cd /backups ||mkdir /backups 
case $1 in 
    "gzip")
        tar -z -c -f /backups/etc-$(date +%Y%m%d).tar.gz /etc
        ;;
    "bzip2")
        tar -j -c -f /backups/etc-$(date +%Y%m%d).tar.bz2 /etc
        ;;
    "xz")
        tar -J -c -f /backups/etc-$(date +%Y%m%d).tar.xz /etc
        ;;
    *)
        echo "Wrong compression tool "
        exit 1 
        ;;
esac

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input one path " pathname
if [ -f ${pathname} ];then
    echo "this is a general file,use it as usual"
elif [ -d ${pathname} ];then
    echo -e "this is a directory,we can use \"cd\" command  for it"
elif [ -L ${pathname} ];then
    echo -e "this is a link file"
else
    echo "we can't decide the type of file "
fi

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
hostname=$(hostname )
case $hostname in
    "localhost")
        hostname mail.magedu.com
        ;;
    "none")
        hostname mail.magedu.com
        ;;
    "")
        hostname mail.magedu.com
        ;;
    *)
        echo "$hostname"
        ;;
esac

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "input one user name: " username
id ${username} &> /dev/null && result=0||result=1
while [ ${result} -eq 1 ];do
    read -p "input one user name ,your input is not a user name :" username
    id ${username} &> /dev/null && result=0||result=1
done
userid=$(id -u ${username})
if [ ${userid} -eq 0 ];then
    echo "user is root "
elif [ ${userid} -gt 0 -a ${userid} -lt 500 ];then
    echo "user is system user"
else
    echo "user is general user"
fi

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "input one user name: " username
id ${username} &> /dev/null && result=0||result=1
while [ ${result} -eq 1 ];do
    read -p "input one user name ,your input is not a user name :" username
    id ${username} &> /dev/null && result=0||result=1
done
userid=$(id -u ${username})
usershell=$(grep -E "^${username}" /etc/passwd|cut -d: -f7|grep ".*sh$")
if [ ${userid} -ge 500 -a ${usershell} != "" ];then
    echo "a user can log system."
else
    echo "a user can't log system"
fi

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

#!/bin/bash
#the purpose of program :
#
#08/12/2016 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cd /var/log
mkdir -p /tmp/test1-testn
for i in $(ls );do
    if [ -d $i ];then
        cp -r $i /tmp/test1-testn
        continue
    elif [ -f $i ];then
        cp $i /tmp/test1-testn
        continue
    elif [ -L $i ];then
        cp -d $i /tmp/test1-testn
        continue
    else
        cp -a $i /tmp/test1-testn
        continue
    fi
done

原创文章,作者:N20-重庆-雪寒,如若转载,请注明出处:http://www.178linux.com/18707

(0)
N20-重庆-雪寒N20-重庆-雪寒
上一篇 2016-08-15 12:06
下一篇 2016-08-15 12:06

相关推荐

  • linux中的vim

    什么是vim:     Linux 在命令行下的文本编辑器,很多个别软件的编辑接口都会主动调用vim,所以在 Linux 的系统中使用文字编辑器来编辑你的Linux参数配置文件,在 Linux 的世界中,绝大部分的配置文件都是以 ASCII 的纯文本形态存在,因此利用简单的文字编辑软件就能够修改设置.所有的Unix L…

    Linux干货 2016-08-11
  • 8月20日软件包管理

    软件包管理 本章内容 软件运行环境 软件包基础 Rpm包管理 yum管理 定制yum仓库 编译安装 软件运行环境 API:Application Programming Interface     POSIX:Portable OS 程序源代码–>预处理–>编译–>…

    Linux干货 2016-08-21
  • N22-网路班-第2次作业-冥界之王

    1.linux上文件管理类命令都有哪些,其常用的使用方法极其示例演示。(1).创建目录格式:mkdir [选项]  目录功能:创建目录常用选项说明: -m  创建目录的同时设置访问权限 -p  一次性创建多级目录 -v  显示过程[root@Cent…

    Linux干货 2016-08-22
  • N22-第一周博客作业

    1、计算机的组成和及其功能? 计算机由硬件,操作系统,软件三大部分组成。 硬件: 计算机系统中所使用的电子线路和物理设备,主要由中央处理器( CPU )、存储器、外部设备(输入输出设备、I/O设备)及总线等组成。 a、中央处理器(CPU)的主要功能是根据存储器内的程序 ,逐条地执行程序所指定的操作。中央处理器的主要组成部分是:数据寄存器、指令寄存器、指令译码…

    Linux干货 2016-08-15
  • 马哥教育络班22期-第1周博客作业

    第一周博客作业 1 描述计算机的组成及其功能 计算机由硬件和软件两大部分组成: 1.1 计算机硬件组成与功能 计算机硬件由运算器、控制器、存储器、输入设备、输出设备五大部分组成。 1.1.1 运算器 算术逻辑部件ALU,执行算术运算和逻辑运算。例如:加减乘除,移位,取模。运算器的核心是加法器。 1.1.2 控制器 控制器是计算机的指挥中心,它的主要作用是按照…

    Linux干货 2016-08-14
  • 作业:0803日

    1:三种权限rwx对文件和目录的不同意义:       对文件: r:能够查看文件内容 w:修改文件内容 x:执行文件,通常文件的执行权限能够发起一个进程       对目录: r: 能够cd进目录,使用ls查看目录内的文件列表 w: 能够…

    Linux干货 2016-08-05

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-17 14:01

    写的很好,排版也很棒,加油 case的是不是可以用if做那?尝试一下