脚本

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

相关推荐

  • Shell脚本编程初步

        shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。shell脚本(shell script),是一种为shell编写的脚本程序。业界所说的shell通常都是指shell脚本,但shell和shell script是两个不同的概念。shell编程跟java、php编程…

    Linux干货 2016-08-24
  • shell编程、yum的使用

    shell编程、yum的使用 一、shell编程 1、while的特殊使用 while 循环的特殊用法(遍历文件的每一行): while read line; do      循环体 done < /PATH/FROM/SOMEFILE   依次读取/PATH/FROM/SOMEFILE 文件…

    Linux干货 2016-08-21
  • linux基础知识1

    Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。文件管理工具:cp,mv,rmcp命令:copy源文件:目标文件单源复制:cp [OPTION]… [-T] SOURCE DEST多源复制:cp [OPTION]… SOURCE… DIRECTORYcp [OPTION]… -t DIRECTORY SOURCE…单源复制:如果DES…

    2018-03-12
  • 第一篇文章

    第一篇文章。

    Linux干货 2016-12-26
  • linux用户和组的配置文件

    1./etc/passwd:用户及其属性信息(用户名,密码,uid,gid,gecos注释性描述信息,家目录,登录shell)在/etc/passwd文件中每个用户都有一个对应的记录行,它记录了这个用户的一些基本属性。系统管理员经常会接触到这个文件的修改以完成对用户的管理工作。这个文件对所有用户都是可读的。      …

    2017-07-22
  • 文本处理工具

    首先介绍一下wc 这个工具,一看到这个词,我不禁捧腹大笑,这不是我们经常要去的地方吗?其实我们都错了,厕所的正确翻译为toilet,为什么叫wc 呢?这中间还有个故事,在这里就大概说一下吧。 很早以前,厕所Toilet里都有水箱watercloset, 人们一提到water closet就想到了toilet,因此watercloset就成了toil…

    2017-07-29

评论列表(1条)

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

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