马哥教育网络班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

相关推荐

  • 运筹帷幄之中,决胜与千里之外!

           运筹帷幄之中,决胜与千里之外,决定改变一生。        自从2016年年后学校要求产参加毕业实习,但对linux一窍不通的我,阴差阳错的被一家网络公司给录取了。        安排在一家企业做驻场运维工程师,刚去驻场的时候并没有给我分配相应的工作只是给了一些关于数据库,网络,和系统的一些资料,要求进行自学。我想了想,就决定选择了系统进行了解…

    2018-03-26
  • ip ss route nmcli netstart 命令汇总

    route命令 路由管理命令 查看:route -n [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.101.0 0.0.0.0 255.255.255.0 U 1 0 0 e…

    Linux干货 2017-05-07
  • 正则表达式练习

    一: 1、找出ifconfig命令结果中本机的所有IPv4地址         2、查出分区空间使用率的最大百分比值 3、查出用户UID最大值的用户名、UID及shell类型 4、查出/tmp的权限,以数字方式显示 二: 1、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式) 2、显示/etc…

    Linux干货 2016-08-12
  • Linux源程序包管理器之RPM

    一、 RPM功能描述 l  打包 l  安装 l  卸载 l  升级 l  校验 l  数据库管理 二、 RPM包命名格式 1.       命名格式举例 name-version-relase.arch.rpm 例如:bash-4.2.3…

    系统运维 2015-09-25
  • haproxy代理服务

    HAProxy: LB Cluster:         四层:   lvs, nginx(stream),haproxy(mode tcp)         七层:   http: nginx(http, ngx_http_upstrea…

    Linux干货 2017-05-17

评论列表(1条)

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

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