脚本

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

相关推荐

  • N22-第六周博客作业

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; :%s@^[[:space:]]\+[^[:space:]]@#\1@g 或 :%s@^[[:space:]]\+[^[:space:]]@#&a…

    Linux干货 2016-09-26
  • 权限管理

    权限管理包括普通权限、特殊权限、默认权限、ACL权限。 这些权限都是针对u(owner)、g(group)、o(other)三类定义的。 设置权限时文件和目录不同(此处把文件和目录分开,一般说文件就包含目录(linux思想:一切皆文件))如下图所示, 分别用两个用户分别创建了一个目录一个文件,可以看到同是目录的liutest和test的权限不同,它俩同为目录…

    Linux干货 2017-07-30
  • Linux基础知识之软件包管理(二)

    (1)CentOS7 yum dnf  yum repository: yum repo 存储了众多rpm包,以及包的相关的元数据文件(放置于特定目录下,repodata) 文件服务器: ftp:// http:// nfs:// file:/// (2)yum客户端: 配置文件: /etc/yum…

    Linux干货 2016-08-24
  • linux的管道命令和用户管理命令

    1、列出当前系统上的所有已经登录的用户的用户名 [root@localhost home]# who | cut -d” ” -f1 | uniq root test root 2、取出最后登录到当前系统的用户相关信息 [root@localhost home]# who | tail -1 root pts/3 2017-07-16 19:38 (192.…

    Linux干货 2017-07-17
  • 来两道百度的shell开胃菜

    1、写脚本实现,可以用shell、perl等。在目录/tmp下找到100个以abc开头的文件,然后把这些文件的第一行保存到文件new中。 方法1: #!/bin/sh for files in `find /tmp -type f -name "abc*"|h…

    Linux干货 2016-09-19
  • > 输出重定向

    重定向输出 > >> 追加 目 录 1、 >正确定向等同于1> 2、 2> 错误定向 3. &> 把错误导成正确的{洗钱} 4. 正确的变成错误的 5. > 覆盖 6. 举一反三 最近学的命令中学到了一些很有意思的命令,分享一下 > 重定向输出 最…

    Linux干货 2017-08-13

评论列表(1条)

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

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