脚本

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

    #!/bin/bash
    #
    sum=0
    num=0

    while read line;do
        usershell=$(echo $line | cut -d : -f 7)

        if [ $usershell == "/sbin/nologin" ];then
            let sum++
        else 
            let num++
        fi
    done < /etc/passwd

    echo "Can login user:$num"
    echo "Not can login user:$sum"

 

2、写一个脚本

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

    #!/bin/bash
    #
    while read line;do
            hostname=$(echo $line | cut -d . -f 1)

            if [ $hostname == "localhost" ];then
                    echo "HOSTNAME=www.magedu.com" > /etc/sysconfig/network
            else
                    echo "$hostname"
            fi
    done < /proc/sys/kernel/hostname

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

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

    #!/bin/bash
    #
    read -p "Please enter a disk device path:" path

    while [ -z "$path" ];do
        echo "Please enter a correct disk device path."
        read -p "Please enter a disk device path:" path
    done

    if [ -b "$path" ];then
        fdisk -l $path
    else
        echo "This is not a disk device path."
    fi

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

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

    #!/bin/bash
    #
    cat << EOF
    yes) Continue to execute the shell
    quit) Normal exit
    other) Abnormal exit
    ==================================
    EOF

    read -p "Enter your option:" option

    while [ -z "$option" ];do
        echo "Please enter at least one option"
            read -p "Enter your option:" option
    done

    if [ "$option" == "yes" ];then
        echo "yes,quit or other"
        read -p "Enter your option again:" option
    elif [ "$option" == "quit" ];then
        echo "Normal exit" 
        exit 0
    else
        echo "Abnormal exit"
        exit 1
    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
    gzip) Archive compressed /etc directory using gzip tool 
    bzip2) Archive compressed /etc directory using tool
    xz) Archive compressed /etc directory using xz tool
    quit) quit
    ==================================================
    EOF

    read -p "Enter your option:" option

    while [ -z "$option" ];do
        echo "gzip,bzip2,xz or quit"
        read -p "Enter your option again:" option
    done

    case $option in
    gzip)
        tar -zcf  /backups/etc-$(date +\%Y\%m\%d).tar.gz /etc;;
    bzip2)
        tar -jcf /backups/etc-$(date +\%Y\%m\%d).tar.bz2 /etc;;
    xz)
        tar -Jcf /backups/etc-$(date +\%Y\%m\%d).tar.xz /etc;;
    quit)
        echo "quit"
        exit 0;;
    *)
        echo "Abnormal exit"
        exit 1
    esac

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

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

    #!/bin/bash
    #
    read -p "Please enter a path:" path

    while [ -z "$path" ];do
        read -p "Please enter a path:" path
    done

    if [ -f "$path" ];then
        echo "$path Can be accessed normally."
    elif [ -d "$path" ];then
        echo "$path Is a directory,You can use the CD command to switch to this directory."
    elif [ -h "$path" ];then
        echo "$path Is an access path."
    else
        echo "Can not judge."
        exit 0
    fi

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

(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;

    #!/bin/bash
    #
    while read line;do
        hostname=$(echo $line | cut -d . -f1)

        if [ $hostname == "localhost" -o  $hostname == "(none)" ];then
            echo "HOSTNAME=mail.magedu.com" > /etc/sysconfig/network
        else 
            echo "The current host name is:$line"
        fi
    done < /proc/sys/kernel/hostname

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

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

    #!/bin/bash
    #
    read -p "Please enter a user name:" user

    while [ -z $(grep -o "^$user\>" /etc/passwd) ];do
        echo -e "\033[32m Please enter a correct username. \033[0m"
        read -p "Please enter a user name again:" user
    done

    if [ $(grep "^$user\>" /etc/passwd | cut -d : -f 3) -eq 0 ];then
        echo -e "\033[31m This is an admin user \033[0m"
    elif [ $(grep "^$user\>" /etc/passwd | cut -d : -f 3) -gt 0 -a $(grep "^$user\>" /etc/passwd | cut -d : -f 3) -lt 500 ];then
        echo -e "\033[31m This is an System user \033[0m"
    else
        echo -e "\033[31m This is an Ordinary user \033[0m"
    fi

 

原创文章,作者:641348038@qq.com,如若转载,请注明出处:http://www.178linux.com/64327

(0)
641348038@qq.com641348038@qq.com
上一篇 2016-12-23 09:52
下一篇 2016-12-23 21:24

相关推荐

  • 一键搭建mysql集群系列一

    一键自动安装mysql 5.7 shell脚本自动化安装二进制mysql-5.7 本节主要用到四个shell脚本 和 一台资料存储机器(IP:192.168.42.26) install_mysql.sh 自动化安装mysql脚本 ntpdate.sh 时间同步脚本 system_init.sh 系统初始化脚本 yum.sh yum源配置脚本 执行步骤: 1…

    2017-05-13
  • Redis基于Sentinel哨兵高可用方案

    下载最新redis版本,当前最新版本为  2.8.19  2014-12-30 安装redis 首页地址:http://redis.io/ 最新稳定版下载地址: http://download.redis.io/releases/redis-2.8.19.tar.gz # tar -xvf redis-2.8.19.tar.gz # cd…

    2015-03-03
  • 第一周课程练习

    一、计算机组成及其功能。      计算机系统由硬件系统和软件系统两个部分组成。      硬件系统由运算器、控制器、存储器、输入设备、输出设备组成。           运算器是…

    Linux干货 2016-08-15
  • 文本处理三剑客之一的sed

    处理文本的工具sed Stream EDitor, 行编辑器 sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Se…

    Linux干货 2017-04-27
  • apache编译安装

    apache是什么:     Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。同时Apache音译为阿帕奇,是北美印第安…

    Linux干货 2016-08-24

评论列表(1条)

  • 马哥教育
    马哥教育 2017-01-04 16:21

    脚本写的不错,但是不要使用系统的命令来作为变量,如果可以修改一下就完美了