马哥教育网络班21期+第9周课程练习

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

#!/bin/bash
#
declare -i nologuser=0
declare -i loguser=0
while read line;do
        result1=`echo $line | cut -d: -f7`
        result2=`echo $line | cut -d: -f1`
        if [ $result1 == "/sbin/nologin" ];then
                echo "$result2 is nologinuser"
                let nologuser++
        else
                echo "$result2 is loguser"
                let loguser++
        fi
done < /etc/passwd
echo "nologuer total $nologuser"
echo "loguser total $loguser"                                
[root@localhost ~]# bash userlog.sh 
root is loguser
bin is nologinuser
...
user3 is loguser
user4 is loguser
user5 is loguser
nologuer total 31
loguser total 24

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@mylab ~]# bash -x hn.sh 
++ hostname
+ hostname=localhost
+ '[' localhost == localhost ']'
+ hostname www.magedu.com

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

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Usage:bash.sh /dev/sda|..."
	exit 1
else
	diskinfo=`fdisk -l $1 | grep $1`
	result=$?
	if [ $result -eq 0 ];then
		fdisk -l $1 | grep $1
	else
		echo "$1 Unknow dev"
	fi
fi

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

脚本能够接受一个参数;
  (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
  (2) 如果参数1为yes,则显示继续执行脚本;
  (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Usage:bash quit | yes "
	exit 1
fi
case $1 in
quit )
	echo "quiting"
	exit 0
;;
yes ) 
	echo "continue bash $0"
;;
*)
	echo "Unknow command."
;;
esac

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 [ $# -lt 1 ];then
     echo "Usage:bash:gzip|bzip|xz"
     exit 1
fi
if [ -d /backups ];then
case $1 in
gzip )
	tar -p -P -zcf /backups/etc-`date +%Y%m%d`.tar.gz /etc
;;
bzip2 )
	tar -p -P -jcf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc
;;
xz )
	tar -P -p -Jcf /backups/etc-`date +%Y%m%d`.tar.xz /etc
;;
* )
	echo "tar erro"
	exit 2
esac
else
	mkdir -p /backups
case $1 in
gzip )
	tar -p -P -zcf /backups/etc-`date +%Y%m%d`.tar.gz /etc
;;
bzip2 )
	tar -P -p -jcf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc
;;
xz )
	tar -p -P -Jcf /backups/etc-`date +%Y%m%d`.tar.xz /etc
;;
* )
	echo "tar erro"
	exit 2
esac
fi

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

(1) 如果为普通文件,则说明其可被正常访问;
  (2) 如果是目录文件,则说明可对其使用cd命令;
  (3) 如果为符号链接文件,则说明是个访问路径;
  (4) 其它为无法判断;

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Usage:bash /filepath/filename."
	exit 1
fi
if [ -f $1 ];then
	echo "This file can access!"
elif [ -d $1 ];then
	echo "use command cd this file"
else
	echo "Unknow this file"
fi

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

(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
  (2) 否则,显示现有的主机名即可;

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

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

(1) 如果用户的id号为0,则显示其为管理员;
  (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
  (3) 否则,则显示其为普通用户;

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Usage:bash username"
	exit 1
fi
if id $1 &> /dev/null;then
	userid=`grep ^$1 /etc/passwd | cut -d: -f3`
	if [ $userid -eq 0 ];then
		echo "$1 is Administrator."
	elif
	   [ $userid -gt 0 -a $userid -lt 500 ];then
		echo "$1 is System User."
	else
	    echo "$1 is Common User."
	fi
fi

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

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

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Usage:bash user."
	exit 1
fi
if id $1 &>/dev/null;then
	uid=`grep $1 /etc/passwd | cut -d: -f3`
	sh=`grep $1 /etc/passwd | grep "sh$"`	
        result=$?
		if [ $result -eq 0 -a $uid -gt 500 ];then
			echo "$1 a user can log system."
		else
			echo "$1 nologin system."
		fi
fi

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

(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
  (2) 复制目录时,才使用cp -r命令;
  (3) 复制文件时使用cp命令;
  (4) 复制链接文件时使用cp -d命令;
  (5) 余下的所有类型,使用cp -a命令;

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

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

(0)
Net21_LionNet21_Lion
上一篇 2016-09-05 08:51
下一篇 2016-09-05 08:51

相关推荐

  • samba

    FTP,SAMBA,NFS之间的区别 FTP        Windows/linux/unix/macOS等        发布网站,文件共享              Tcp/21 Samba  …

    Linux干货 2016-11-01
  • 磁盘管理—MBR分区与GPT分区总结

    描述: 1,什么是磁盘分区   磁盘分区是使用分区编辑器(partition editor)在磁盘上划分几个逻辑部分,盘片一旦划分成数个分区(Partition),不同类的目录与文件可以存储进不同的分区。 2,硬盘结构及参数   3D参数(Disk Geometry):CHS(Cylinder/Head/Sector)  &nb…

    Linux干货 2016-08-29
  • N26第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。    who | cut -d' ' -f1 | sort -u 2、取出最后登录到当前系统的用户的相关信息。    last | head -1   3、取出当前系统上被用户当作其默认s…

    Linux干货 2017-02-03
  • linux基础学习-第八天

    2016-08-08 授课内容: 处理文本的工具sed vim编辑器 Shell脚本编程基础介绍 sed:sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),     接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。默认不编辑原文件,仅…

    Linux干货 2016-08-10
  • keepalived相关配置示例(二)

    虚拟服务器配置实验: 两台centos6主机为web service系统为CentOS6.8 n1:172.16.253.87 n2:172.16.252.5都安装httpd yum install -y httpd[root@n1 ~]#vim /var/www/html/index.html<h1>Real Server 1</h1&g…

    Linux干货 2017-06-28
  • MariaDB基础

    MariaDB基础 关系型数据库的基础概念: 1)数据类型:作用:存储格式、数据范围、所能参与的运算、排序方式         字符型:         定长字符型:CHAR(#),BIARNY(#)…

    Linux干货 2016-11-07

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-07 18:31

    脚本写得非常的漂亮,思路清晰,还给出了调试的过程,32赞,加油!