N21-天天-第九周课程练习

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

#!/bin/bash
awk -F: '$NF~"/sbin/nologin" {shell++} $NF!~"/sbin/nologin" {noshell++} END{printf "shell: %d \t noshell: %d \n",shell,noshell}' /etc/passwd

2、写一个脚本

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

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

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

#!/bin/bash
hostname=$HOSTNAME
if [ $hostname == "localhost" ];then
sed -i 's/HOSTNAME=.*/HOSTNAME=www.magedu.com/' /etc/sysconfig/network
else
echo $HOSTNAME
fi

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

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

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

#!/bin/bash
if [ $# -lt 1 ];then
echo "input a device file!"
exit 1
fi
if [ -b $1 ];then
fdisk -l $1
else
echo "The input is not a device file!"
exit 2
fi

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

   脚本能够接受一个参数;

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

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

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

#!/bin/bash
if [ $# -lt 1 ];then
echo "Enter a argument."
exit 0
fi
case $1 in
quit)
echo "scripts is quit!"
exit 0
;;
yes)
echo "scripts is go on!"
;;
*)
echo "scripts break!"
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
day=$(date +%Y%m%d)
dir=/backups
[ -d $dir ] || mkdir $dir
if [ $# -lt 1 ];then
    echo "Enter a argument."
    exit 0
fi
case $1 in
    gzip)
        tar zcf $dir/etc-$day.tar.gz /etc
        ;;
    bzip2)
        tar jcf $dir/etc-$day.tar.bz2 /etc
        ;;
    xz)
        tar Jcf $dir/etc-$day.tar.xz /etc
        ;;
    *)
        echo "choose a wrong tools."
        exit 2
        ;;
esac

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

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

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

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

   (4) 其它为无法判断;

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Enter a argument."
    exit 0
fi
if [ -f $1 ];then
    echo "It is a regular file,can be access!"
elif [ -d $1 ];then
    echo "A directory,can used by cd command!"
elif [ -L $1 ];then
    echo "A symbolic file!"
else
    echo "can not recognize!"
    exit 1
fi

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

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

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

#!/bin/bash
hostname=$HOSTNAME
if [ $hostname == "localhost" ] || [ -z $hostame ] || [ $hostname == "(none)" ];then
#if [ $hostname == "localhost" -o -z $hostame -o $hostname == "(none)" ];then
    sed -i 's/HOSTNAME=.*/HOSTNAME=www.magedu.com/' /etc/sysconfig/network
else
    echo $HOSTNAME
fi

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

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

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

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Enter a user."
    exit 0
fi
id -u $1 &> /dev/null
if [  ! $? -eq 0 ];then
    echo "Wrong user!"
    exit 1
fi
if [ `id -u $1` -eq 0 ];then
    echo "user $1 is administrator"
elif [ `id -u  $1` -lt 500 ];then
    echo "user $1 is system user"
else
    echo "user $1 is common user"
fi

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

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

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Enter a user."
    exit 0
fi
id -u $1 &> /dev/null
if [  ! $? -eq 0 ];then
    echo "Wrong user!"
    exit 1
fi
grep "^$1\>" /etc/passwd | grep sh$  &> /dev/null
if [ `id -u $1` -gt 500 ] && [ $? -eq 0 ];then
    echo "user $1 can login"
else
    echo "user $1 can not login"
fi

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

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

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

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

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

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

#!/bin/bash
dir=/tmp/test1-testn
[ -d $dir ] || mkdir $dir
cd /var/log
for file in `ls`
do
    if [ -d $file ];then
        /bin/cp -r $file $dir
    elif [ -f $file ];then
        /bin/cp $file $dir
    elif [ -L $file ];then
        /bin/cp -d $file $dir
    else
        /bin/cp -a $file $dir
    fi
done

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

(0)
N21-天天N21-天天
上一篇 2016-09-01 08:51
下一篇 2016-09-01 08:53

相关推荐

  • 循环体

    for 变量名in 列表;do 循环体 done 执行机制: 依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束 列表生成方式: (1) 直接给出列表 (2) 整数列表: (a) {start..end} (b) $(seq[start [step]] end) (3) 返回列表的命令  &nbsp…

    Linux干货 2016-08-21
  • CentOS 系统自动化安装

    CentOS 系统自动化安装 概述:     系统安装过程     配置anaconda     自动化安装系统 CentOS 系统安装: 1.启动流程: bootloader–>kernel(initrd)–>rootfs–>anaconda(…

    Linux干货 2016-09-21
  • Linux目录配置整理

    FHS:   为了规范各种Linux发行版的目录配置方法和目录定义而制定了FHS规范。   在FHS规范的Linux中,所有的文件和目录都由根目录开始,即“/”,它是所有文件和目录的起点。然后按层次化的树状划分下来。这种目录结构被称为“目录树”。 目录树的特性:   ·起始点为根目录(/或root)   ·每一个目录不…

    Linux干货 2016-07-07
  • 【Linux基础】文件管理类命令

    总结下linux系统文件管理类的命令 mkdir -> make dirtctories 创建目录 语法:mkdir [OPTION]… DIRECTORY… 常用选项-p: 自动按需创建父目录;-v: verbose,显示详细过程;-m MODE:直接给定权限;注意:路径基名方为命令的作用对象;基名之前的路径必须得存在;示例:~]# mkdir -p…

    Linux干货 2018-03-11
  • ansible部分功能详解

    一. 架构: 组成     ansible core:     modules:     Core Module     Custom Module   Host Inventory: 主机清单       File   &nbs…

    Linux干货 2016-11-11
  • N22-冥界之王-第9周作业

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

    Linux干货 2016-11-01

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-07 18:42

    脚本写得非常的漂亮,思路清晰,1题的排版需要修改一下的,脚本的缩进注意一下哈,加油!