第九周作业

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

#!/bin/bash
declare -i sum_login=0
declare -i sum_nologin=0
while read line;do
    shell=$(echo $line|awk -F: '{print $NF}')
    if [ "$shell" == "/sbin/nologin" ];then
        let sum_nologin++
    else
        let sum_login++
    fi
done </etc/passwd
echo "Can login: $sum_login"
echo "Cannot login: $sum_nologin"
执行结果:
[root@centos6 script]# ./chkuser.sh   
Can login: 8
Cannot login: 17

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

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

#!/bin/bash
hostname=$(hostname)
if [ "$hostname" == "localhost" ];then
    hostname www.magedu.com
else
    hostname
fi
[root@centos6 script]# ./chhn.sh 
www.magedu.com

3、写一个脚本,完成如下功能
    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

if [ -b $1 ];then
    fdisk -l $1
else
    echo "Invalid file!"
fi
执行结果:
[root@centos6 script]# ./devinfo.sh /dev/tty0
Invalid file!
[root@centos6 script]# ./devinfo.sh /dev/sda1

Disk /dev/sda1: 524 MB, 524288000 bytes
255 heads, 63 sectors/track, 63 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

4、写一个脚本,完成如下功能
   脚本能够接受一个参数;
   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
   (2) 如果参数1为yes,则显示继续执行脚本;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

while true;do
    case $1 in
        quit)
            echo "Exit in 2 second..."
            sleep 2
            exit 0
            ;;
        yes)
            echo "Continue..."
            break
            ;;
        *)
            echo "Unknown error,exit!"
            exit 1
    esac
done
echo "Execute complete!"
执行结果:
[root@centos6 script]# ./argu.sh 
Usage ./argu.sh arg1
[root@centos6 script]# ./argu.sh quit
Exit in 2 second...
[root@centos6 script]# ./argu.sh yes
Continue...
Execute complete!
[root@centos6 script]# ./argu.sh haha
Unknown error,exit!

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
if [ $# -ne 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

[ -d /backups ] || mkdir -p /backups
case $1 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
        ;;
    *)
        echo "Invalid compress mode!"
        exit 1
esac
执行结果:
[root@centos6 script]# ll /backups
total 22904
-rw-r--r-- 1 root root 8395375 Feb 13 19:34 etc-20170213.tar.bz2
-rw-r--r-- 1 root root 9527374 Feb 13 19:32 etc-20170213.tar.gz
-rw-r--r-- 1 root root 5523792 Feb 13 19:34 etc-20170213.tar.xz

6、写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;

   (4) 其它为无法判断;

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

if [ -L $1 ];then
    echo "$1 is symbolic file."
elif [ -f $1 ];then
    echo "$1 is regular file,you can read and write it."
elif [ -d $1 ];then
    echo "$1 is directory,you can use the command cd to enter it."
else
    echo "Unknown file type!"
fi
执行结果:
[root@www script]# ./chktype.sh /etc/hosts
/etc/hosts is regular file,you can read and write it.
[root@www script]# ./chktype.sh /etc/redhat-release 
/etc/redhat-release is symbolic file.
[root@www script]# ./chktype.sh /etc/
/etc/ is directory,you can use the command cd to enter it.
[root@www script]# ./chktype.sh ddd
Unknown file type!

7、写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为”(none)”,则将其命名为mail.magedu.com;

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

#!/bin/bash
if [ -z "$(hostname)" ] || [ "$(hostname)" == "localhost" ];then
    hostname mail.magedu.com
    echo "set 'mail.magedu.com' as new hostname."
else
    hostname
fi
执行结果:
[root@www script]# ./sethn.sh 
set 'mail.magedu.com' as new hostname.
[root@www script]# hostname
mail.magedu.com

8、写一脚本,接受一个用户名为参数;
   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

if ! id $1 &>/dev/null;then
    echo "$1 not exists!"
    exit 1
fi

uid=$(id -u $1)
if [ $uid -ge 500 ];then
    echo "$1 is regular user."
elif [ $uid -gt 0 ];then
    echo "$1 is system user."
else
    echo "$1 is root."
fi
执行结果:
[root@www script]# ./chkuser2.sh magedu
magedu is regular user.
[root@www script]# ./chkuser2.sh mysql
mysql is system user.
[root@www script]# ./chkuser2.sh root
root is root.
[root@www script]# ./chkuser2.sh hahaha
hahaha not exists!

10、写一个脚本,传递一个用户名参数给脚本;
   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

if ! id $1 &>/dev/null;then
    echo "$1 not exists!"
    exit 1
fi

uid=$(id -u $1)
if [ $uid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then
    echo "$1 can login."
else
    echo "$1 cannot login."
fi
执行结果:
[root@www script]# ./chkuser3.sh magedu
magedu can login.
[root@www script]# ./chkuser3.sh nfc
nfc cannot login.

11、写一个脚本,完成如下任务 :
   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;

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

#!/bin/bash
declare -i j=1
for i in /var/log/*;do
    [ ! -d /tmp/test$j ] && mkdir -p /tmp/test$j
    if [ -L $i ];then
        cp -d $i /tmp/test$j
    elif [ -f $i ];then
        cp $i /tmp/test$j
    elif [ -d $i ];then
        cp -r $i /tmp/test$j
    else
        cp -a $i /tmp/test$j
    fi
    let j++
done
执行结果:
[root@www tmp]# tree
.
├── test1
│   └── anaconda.ifcfg.log
├── test10
│   └── btmp
├── test11
│   └── ConsoleKit
│       └── history
├── test12
│   └── cron
├── test13
│   └── dmesg
├── test14
│   └── dmesg.old
├── test15
│   └── dracut.log
├── test16
│   └── lastlog
├── test17
│   └── maillog
├── test18
│   └── messages
├── test19
│   └── mysqld.log
├── test2
│   └── anaconda.log
├── test20
│   └── secure
├── test21
│   └── spooler
├── test22
│   └── tallylog
├── test23
│   └── wtmp
├── test24
│   └── yum.log
├── test3
│   └── anaconda.program.log
├── test4
│   └── anaconda.storage.log
├── test5
│   └── anaconda.syslog
├── test6
│   └── anaconda.xlog
├── test7
│   └── anaconda.yum.log
├── test8
│   └── audit
│       └── audit.log
└── test9
    └── boot.log

原创文章,作者:N26-西安-方老喵,如若转载,请注明出处:http://www.178linux.com/70445

(0)
上一篇 2017-03-05 09:57
下一篇 2017-03-05 11:35

相关推荐

  • sed行编辑器

    Linux文本处理三剑客之一:sed(Stream Editor)流编辑器 sed:     将指定的文本文件内容逐行读取至sed的临时内存空间当中,称为"模式空间",而后逐行查看是否被sed的地址定界所匹配到,如果匹配,则执行相应的编辑操作,否则,默认将模式空间中的内容打印至标准输出  …

    Linux干货 2016-08-15
  • Linux正则表达式及文件查找

    1、显示当前系统上root、fedora或者user1用户的默认shell. #   grep  -E   “^(root|fedora|user1)”   /etc/passwd | cut -d: -f1,7     2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,…

    2017-10-22
  • linux echo命令用法实例

           对于初学者来说,掌握一些基本命令的常用用法是必须的。本文列举了echo命令的几个常用用法并加以实例说明,以作回顾、参考。       echo命令常用作打印输出字符串,根据echo帮助文件可以获得以下信息:        语法 echo…

    2017-05-20
  • 配置NTP服务器

    配置NTP时间服务器 一.安装ntp软件 1.检查是否安装了ntp相关包。 rpm -qa | grep ntp 2.安装ntp软件。 yum -y install ntp 二.参数讲解 ignore  :关闭所有的 NTP 联机服务 nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。 notrust :客户端…

    Linux干货 2016-10-30
  • CentOS 系统自动化安装

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

    Linux干货 2016-09-21
  • Linux 进程及作业管理

    一 进程、线程、程序概念 1、进程(Process) 什么是进程,简单的说,进程是程序的执行实例,即运行中的程序,同时也是程序的一个副本;程序是放置于磁盘的,而运行中的程序是位于内存中的。 2、线程(Thread) 一个进程至少包括一个线程,通常将该线程称为主线程,所以线程是比进程更小的单位,是系统分配处理器时间资源的基本单元。一个进程要想同时在多颗CPU上…

    Linux干货 2016-03-15

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-07 11:52

    完成的很好,脚本的思路清晰,加油!