N22-第九周作业

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

#!/bin/bash

#

declare -i count1=0

declare -i count2=0

for i in $(awk -F : '{print $7}' /etc/passwd); do

        if [[ $i == "/sbin/nologin" ]]; then

                let count1++

        else

                let count2++

        fi

done

echo "total nologin:$count1"

echo "total longin: $count2"

脚本执行效果:

[root@localhost ~]# bash file1.sh

total nologin:19

total longin: 5

2、写一个脚本

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

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

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

#!/bin/bash

#

hostname=$(hostname)

if [[ $hostname == "localhost" ]]; then

    hostname www.magedu.com

else

    echo $hostname

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh

[root@localhost ~]# hostname

www.magedu.com

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

if [ -b $1 ]; then

    fdisk -l $1

else

    echo "no block device or no exist"

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh /dev/sda

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x000864ff

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048      196607       97280   83  Linux

/dev/sda2          196608   273661951   136732672   8e  Linux LVM

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

   脚本能够接受一个参数;

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 1

if [[ $1 == "quit" ]]; then

    echo "quit script" && exit 0

elif [[ $1 == "yes" ]]; then

    echo "contine run script"

else

    echo "except quit"

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh quit

quit script

[root@localhost ~]# bash file1.sh yes

contine run script

[root@localhost ~]# bash file1.sh no

except quit

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

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

if [[ $1 == "gzip" ]]; then

    tar -zcf /backups/etc-$(date +"%Y%m%d").tar.gz /etc

elif [[ $1 == "bzip2" ]]; then

    tar -jcf /backups/etc-$(date +"%Y%m%d").tar.bz2 /etc

elif [[ $1 == "xz" ]]; then

    tar -Jcf /backups/etc-$(date +"%Y%m%d").tar.xz /etc

else

    echo "error compress utils" && exit 2

fi

脚本执行效果:

[root@localhost ~]# bash file1.sh gzip

tar: Removing leading `/' from member names

[root@localhost ~]# bash file1.sh bzip2

tar: Removing leading `/' from member names

[root@localhost ~]# bash file1.sh xz

tar: Removing leading `/' from member names

[root@localhost ~]# bash file1.sh cat

error compress utils

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

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

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

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

   (4) 其它为无法判断;

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

if [ -L $1 ]; then

    ls -l $1

elif [ -d $1 ]; then

    cd $1;pwd

elif [ -f $1 ]; then

    cat $1

else

    echo "unknow"

fi

脚本运行效果:

[root@localhost ~]# bash file1.sh /etc/rc.d/rc3.d/K50netconsole 

lrwxrwxrwx. 1 root root 20 Aug  6 06:14 /etc/rc.d/rc3.d/K50netconsole -> ../init.d/netconsole

[root@localhost ~]# bash file1.sh /etc/issue

\S

Kernel \r on an \m

[root@localhost ~]# bash file1.sh /etc/rc.d

/etc/rc.d

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

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

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

#!/bin/bash

#

hostname=$(hostname)

if [ $hostname == "" -o $hostname == "localhost" -o $hostname == "(none)" ]; then

        hostname mail.magedu.com

else

        echo $hostname

fi

脚本运行效果:

[root@localhost ~]# bash file2.sh

[root@localhost ~]# hostname

mail.magedu.com

                                               

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

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

 ! id $1 &>/dev/null && echo "user no exist" && exit 3

if [ $(id -u $1) -eq 0 ]; then

        echo "Is root"

elif [ $(id -u $1) -gt 0 -a $(id -u $1) -lt 500 ]; then

        echo "Is system"

else

        echo "Is user"

fi

脚本执行结果:

[root@localhost ~]# bash file1.sh root

Is root

[root@localhost ~]# bash file1.sh bin

Is system

[root@localhost ~]# bash file1.sh user10

user exist

[root@localhost ~]# bash file1.sh join

Is user

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

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

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

#!/bin/bash

#

[ $# -lt 1 ] && echo "At least a argument" && exit 2

 ! id $1 &>/dev/null && echo "user no exist" && exit 3

if [[ $(id -u $1) -ge 500 && $(grep $1 /etc/passwd |cut -d : -f 7) =~ sh$ ]]; then

        echo "a user can log system."

else

        echo "login faild"

fi

脚本运行效果:

[root@localhost ~]# bash file1.sh join

a user can log system.

[root@localhost ~]# bash file1.sh root

login faild

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

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

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

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

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

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

#!/bin/bash

#

for i in /var/log/*; do

        if [ -d $i ]; then

                cp -r $i /tmp/test1/

        elif [ -f $i ]; then

                cp $i /tmp/test2/

        elif [ -L $i ]; then

                cp -d $i /tmp/test3/

        else

                cp -a $i /tmp/test4/

        fi

done

脚本执行结果:

[root@localhost ~]# ll /tmp/test1

total 4

drwxr-xr-x. 2 root root 4096 Oct 15 00:35 anaconda

drwxr-x—. 2 root root   22 Oct 15 00:35 audit

drwx——. 2 root root    6 Oct 15 00:35 httpd

drwx——. 2 root root    6 Oct 15 00:35 ppp

[root@localhost ~]# ll /tmp/test2

total 3040

-rw-r–r–. 1 root root   8406 Oct 15 00:35 boot.log

-rw——-. 1 root root      0 Oct 15 00:35 btmp

-rw——-. 1 root root    384 Oct 15 00:35 btmp-20161005

-rw——-. 1 root root  34326 Oct 15 00:35 cron

[root@localhost ~]# ll /tmp/test3

total 0

[root@localhost ~]# ll /tmp/test4

total 0

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

(0)
heianyangguoheianyangguo
上一篇 2016-10-17
下一篇 2016-10-17

相关推荐

  • N25第四周总结(lvm)

    lvm 详解 大纲:    1、什么是lvm     2、为什么要使用lvm     3、如何实现lvm     4、lvm各项命令详解   1、什么是lvm:        lmv (Logical Volume Manager…

    Linux干货 2016-12-22
  • Linux基础知识之用户和组管理

    系统环境:    该博文以CentOS6.8_x86_64和CentOS7.2_x86_64系统为基础,Xshell5远程登录CentOS6.8和CentOS7.2系统,分别以root身份和sjsir用户身份登录系统。 学习的重要问题: 一、为什么要学习用户和组?    首先Linux区别于其他的系统的最重要的特性就是…

    Linux干货 2016-08-02
  • 马哥教育网络班21期-第五周课程练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; grep ^[[:space:]] /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; egrep "^#[[:space:]]{1,}[[:alnum:]]…

    Linux干货 2016-08-08
  • Linux DNS服务系列之原理介绍及正反向解析配置

    前言 我们在访问一个网站的时候,只要输入该网站的网址就会跳转到该网站页面,而实现这一过程就需要DNS服务器将域名解析为IP地址,进而实现数据通信。那么DNS服务器是如何工作的呢?本系列分为两部分,本文将详解DNS服务原理及正反向解析配置。 DNS服务原理详解 DNS相关知识 DNS:Domain Name Service,域名解析服务 监听端口:udp/53…

    Linux干货 2015-04-13
  • Linux基础操作-week5

    1、显示当前系统上root、fedora或user1用户的默认shell; 方式一: # whoami root # echo $SHELL /bin/bash 方式二: #grep user1 /etc/passwd|awk -F “:” ‘{print $7}’ /bin/bash 2、找出/etc/rc.…

    Linux干货 2016-11-27
  • 第五周作业

    1、显示当前系统上root,fedora或user1用户的默认shell。 [root@hostname ~]# grep -E ‘^(root|fedora|user1)’ /etc/passwd | cut -d: -f1,7 root:/bin/bash 2、找出/etc/rc.d/init.d/functions文件中某词后面跟一组小括号的行,形如:…

    Linux干货 2017-08-04

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-25 13:33

    每一个脚本都可以看到实现的效果,很好,下次能将排版美化一下