往死里苦练脚本啊啊啊啊啊啊啊~~~~~~~~~~~~~~~~

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

#脚本内容
[root@centos script]# cat week9_title1.sh 
#!/bin/bash
#Author Jerry
#

#Display shell is nologin user's shell count 
echo "The shell is  nologin user count is $(grep -o "nologin" /etc/passwd |wc -l)" 

#Display shell not's nologin user's shell count 
echo "The shell not's nologin user count is $(grep -v "nologin" /etc/passwd |wc -l)" 

#测试脚本
[root@centos script]# bash week9_title1.sh 
The shell is  nologin user count is 25
The shell not's nologin user count is 7
[root@centos script]#

2、写一个脚本

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

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

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

[root@centos script]# cat week9_title2.sh 
#!/bin/bash
#Author Jerry
#

hostname=$(hostname)

if [ $hostname == "localhost" ];then
    hostname www.magedu.com
    echo "current hostname is:$hostname"
else
    echo "current hostname is:$hostname"

fi
[root@centos script]#

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

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

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

[root@centos script]# cat week9_title3.sh 
#!/bin/bash
#Author Jerry
#


[ $# -lt 1 ] && echo "please atleast input on"

if [ -b $1 ];then
    fdisk -l $1
else
    echo "nothing"
fi

4、写一个脚本,完成如下功能, 脚本能够接受一个参数;

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

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

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

[root@centos script]# bash week9_title4.sh 
please input one atleast
error!!!!!
[root@centos script]# bash week9_title4.sh quit
exit....
[root@centos script]# bash week9_title4.sh yes
go!go! go! 
[root@centos script]# echo $?
0
[root@centos script]# 
[root@centos script]# cat week9_title4.sh 
#!/bin/bash
#Author Jerry
#

[ $# -eq 0 ] && echo "please input one atleast"

case $1 in

yes)
    echo "go!go! go! "
;;

quit)
    echo "exit...."
    exit 0
;;

*)
    echo "error!!!!!"
    exit 2
;;

esac
[root@centos script]#

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) 其它任意值,则显示错误压缩工具,并执行非正常退出;

[root@www /]# cat /script/week9_title5.sh 
#!/bin/bash

    if [ $# -eq 0 ];then
       echo "give me a argument,the argument is gzip bzip2 xz "
       exit 19
    fi


      mkdir /backups &> /dev/null

       case $1 in
       gzip)
        tar -zcvf  /backups/etc-`date +%Y%m%d`.tar.gz /etc
        ;;
       bzip2)
        tar -jvf  /backups/etc-`date +%Y%m%d`.tar.bz2 /etc
        ;;
       xz)
        tar -Jvf  /backups/etc-`date +%Y%m%d`.tar.xz /etc
        ;;
       *)
         echo "error!!!"
         ;;
       esac
[root@www /]#

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

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

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

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

(4) 其它为无法判断;

[root@www script]# cat week9_title6.sh 
#!/bin/bash
#Author Jerry
#

#Check whether the user is to script a path parameter
[ $# -lt 1 ] && echo "Please give a path to the script parameters"

if [ -f $1 ]; then

    echo "这是一个普通文件,可以被正常访问"

elif [ -d $1 ];then

    echo "这是一个目录文件,可以使用cd命令"

elif [ -h $1 ];then

    echo "这是一个符号链接文件,它是一个访问路径"

elif [ ! -h $1 ] && [ ! -d $1 ] && [ ! -f $1 ];then

    echo "这是什么鬼,不想跟你说话并向你扔了一只狗!!!"
    exit

fi

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

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

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

#!/bin/bash
hostname=$(hostname)
if [ -z "$hostname" -o "$hostname" == "localhost" -o  "$hostname" == "none" ];then
    hostname mail.magedu.com
else
    echo "$hostname"
fi

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

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

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

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

#!/bin/bash
if [ $# -eq 0 ];then
    echo "input argu"
    exit 1
fi
    userid=$(id -u)
if [ $userid -eq 0 ];then
    echo "admin"
elif [ $userid -gt 0 -a $userid -lt 500 ];then
    echo "system"
else
    echo "user"
fi

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

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

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

#!/bin/bash
if [ $# -eq 0 ];then
    echo "input argu"
    exit 1
fi
    userid=grep ^$1 /etc/passwd | grep sh$ | cut -d: -f3
if [ $userid -ge 500 ];then
    echo " a user can log system"
else
    echo "nologin"
fi

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

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

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

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

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

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

#!/bin/bash
for i in 'ls /var/log';do
    if [ -d $i ];then
        cp -r /var/log/$i /tmp/test/test1-testn/$i
    elif [ -f $i ];then
        cp /var/log/$i /tmp/test/test1-testn/$i
    elif [ -L $i ];then
        cp -d /var/log/$i /tmp/test/test1-testn/$i
    else
        cp -a /var/log/$i /tpm/test/test1-testn/$i
    fi
done

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

(0)
N24_JerryN24_Jerry
上一篇 2017-02-16 15:02
下一篇 2017-02-16 17:01

相关推荐

  • 一起学DNS系列(十四)DNS查询工具之DIG的使用(1)

      这几节主要讲2个常用的DNS的命令行诊断工具,分别是linux下的DIG和windows下的NSLOOKUP,我们首先来说DIG工具。 DIG,全称Domain Information Groper。原本是Linux平台上BIND服务器诊断的工具(已上传至附件),现在已经有了windows的版本,这节就是利用此工具在windows环境下来做一些…

    2015-03-17
  • 第四周作业

    Linux系统文件管理使用案例 1、 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 首先,利用cp命令将/etc/skel目录复制为/home/tuser1: [root@localhost ~]# cp -r /etc/skel /home/tuser1 随后通过chmo…

    Linux干货 2018-03-24
  • Linux文本处理三剑客之awk详解

    前言 awk是一款强大的报告生成器,不同于sed和grep,它的侧重点是如何把文本信息更好的展示出来,常用与统计和格式化输出。awk相当于微型的shell,有着自己一套语法结构,例如:循环结构,数组,条件判断,函数,内置变量等功能。处理对象一般纯文本文件或纯文本信息。 用法详解 基本语法 awk [options] 'progra…

    Linux干货 2015-04-29
  • Linux文件管理

    1、Linux上的文件管理命令都有哪些,其常用的使用方法及其相关示例演示。 Linux上的文件管理命令   目录管理类命令:mkdir,rmdir    mkdir [OPTION]… DIRECTORY…     -p: 自…

    Linux干货 2016-09-26
  • systemd管理

    centos7:systemdSystemd :系统启动和服务器守护进程管理器,负责在系统启动或运行时,          激活系统资源,服务器进程和其它进程Systemd 新特性:        系统引导时实现服务并行启动      &n…

    Linux干货 2017-04-10
  • vim shell 练习作业题

    vim练习题 1. 复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符   :%s/^[[:blank:]]\+//g 【注意要把/etc/profile复制到/tmp下,cp  /etc/profile  /tmp(尽量不要修改原文件)】 2. 复制/etc/rc.d…

    2017-08-07

评论列表(1条)

  • luoweiro
    luoweiro 2017-02-23 07:32

    题目很新颖,脚本就是多写多练,加油。