马哥教育网络班20期+第九周博客作业

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

#!/bin/bash
#
login=0
nologin=0
cat /etc/passwd|while read myline
do

if [ ${myline##*/}  = "nologin" ];then
        let nologin+=1
else
        let login+=1
fi

echo "login:$login,nologin:$nologin" >tmp1

done
cat tmp1
[root@llww3317 tmp]# bash  1.sh
login:15,nologin:30

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@llww3317 tmp]# bash 2.sh
llww3317
[root@llww3317 tmp]# hostname localhost
[root@llww3317 tmp]# hostname
localhost
[root@llww3317 tmp]# bash 2.sh
[root@llww3317 tmp]# hostname
www.magedu.com

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

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

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

#!/bin/bash
#

if ls /dev/$1 &>/dev/null ;then
fdisk -l /dev/$1
fi

[root@llww3317 tmp]# bash 3.sh sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0004d60b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  8e  Linux LVM

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

    脚本能够接受一个参数

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

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

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

#!/bin/bash
#
case $1 in
quit)
exit 0
;;
yes)
echo "continue"
;;
*)
exit 1
;;
esac



[root@llww3317 tmp]# bash 4.sh 1
[root@llww3317 tmp]# echo $?
1
[root@llww3317 tmp]# bash 4.sh quit
[root@llww3317 tmp]# echo $?
0
[root@llww3317 tmp]# bash 4.sh yes
continue

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
#
case $1 in
gzip)
 tar -czvf /backups/etc-20160613.tar.gz  /etc
;;
bzip2)
tar -cjvf /backups/etc-20160613.tar.bz2 /etc
;;
xz)
tar -cjvf /backups/etc-20160613.tar.xz /etc
;;
*)
echo "error tar tools"
exit 1
;;
esac
[root@llww3317 tmp]# bash 5.sh gzip >/dev/null
[root@llww3317 tmp]# bash 5.sh bzip2 >/dev/null
[root@llww3317 tmp]# bash 5.sh xz >/dev/null
[root@llww3317 tmp]# ls /backups
etc-20160613.tar.bz2  etc-20160613.tar.gz  etc-20160613.tar.xz

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

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

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

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

    (4)其它为无法判断

#!/bin/bash
#

if [ -f $1 ];then
echo "Can be accessed normally"
elif [ -L $1 ];then
echo "access route"
elif [ -d $1 ];then
echo "Can be cd command"
else
echo "unable"
fi

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

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

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

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

#!/bin/bash
#
user=`cat /etc/passwd|grep "^$1\>"|cut -d: -f3`
if [ $user -eq 0 ];then
echo "$1:admin"
elif [ $user -gt 0 -a $user -lt 500 ];then
echo "$1:system"
else
echo "$1:normal"
fi
[root@llww3317 tmp]# bash 8.sh root
root:admin
[root@llww3317 tmp]# bash 8.sh named
named:system
[root@llww3317 tmp]# bash 8.sh magedu
magedu:normal

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

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

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

#!/bin/bash
#
passwd=`cat /etc/passwd|grep "^$1\>"`
userid=`echo $passwd|cut -d: -f3`
shell=${passwd: -2}
if [ $userid -ge 500 -a $shell = "sh" ];then
echo "a user can log system"
else
echo "can't log system"
fi
[root@llww3317 tmp]# bash  10.sh root
can't log system
[root@llww3317 tmp]# bash  10.sh magedu
a user can log system

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

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

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

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

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

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

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

echo $i
done
[root@llww3317 tmp]# ls /tmp/test1-testn/
anaconda.ifcfg.log    ConsoleKit  messages           sssd
anaconda.log          cron        ntpstats           tallylog
anaconda.program.log  cups        pm-powersave.log   wpa_supplicant.log
anaconda.storage.log  dmesg       ppp                wtmp
anaconda.syslog       dmesg.old   prelink            Xorg.0.log
anaconda.xlog         dracut.log  sa                 Xorg.0.log.old
anaconda.yum.log      gdm         samba              Xorg.9.log
audit                 httpd       secure             yum.log
boot.log              lastlog     spice-vdagent.log
btmp                  maillog     spooler

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

(0)
laiwen2007laiwen2007
上一篇 2016-08-09 15:20
下一篇 2016-08-09 18:44

相关推荐

  • bash编程初体验(三)

    bash编程初体验之for for while until 概述 本文将介绍以for为代表的循环语句在shell 脚本中的应用,常见的循环语句有for, while,until,作为循环语句,顾名思义,它就是重复地做一件事,直到满足某一条件而退出;另外,还有两个循环控制语句continue与break来配合循环语句,以实现临时中断或跳出循环的功能;以下为fo…

    Linux干货 2016-08-24
  • linux常用命令

    系统信息 arch 显示机器的处理器架构(1)uname -m 显示机器的处理器架构(2)uname -r 显示正在使用的内核版本dmidecode -q 显示硬件系统部件 – (SMBIOS / DMI)hdparm -i /dev/hda 罗列一个磁盘的架构特性hdparm -tT /dev/sda 在磁盘上执行测试性读取操作cat /pro…

    2018-03-17
  • 第二周作业

    第二周

    Linux笔记 2018-05-20
  • 马哥教育网络版25期+第一周作业

    1、描述计算机的组成及其功能 计算机是由CPU,控制器,RAM,输入设备,输出设备组成的 2、按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别 现如今主流的LINUX发行版系列主要有: Debian,Slackware,Redhat,这些发行版都是基于GUNLinux开发的,不过是由不同的组织或团体开发并发行的。 3、描述Linux的哲学思想,…

    Linux干货 2016-12-05
  • Ansible使用介绍(二)YAML语法及Playbook

    playbook是由一个或多个”play”组成的列表,play的主要功能在于将事先归并为一组的主机装扮成事先通过ansibe中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的module。将多个play组织在一个playbook中,即可让它们联同起来按事先编排的机制运行。

    2018-05-30
  • 博客启动计划&我个人理解的Python优缺点

    很久没有写博客了,因为最近一直在使用Python。Python实在不是一门好的工作用语言(我的观点是所有的动态语言都不是好的工作用语言,不仅是Python),但是自己玩还是可以的。但,生活所迫,还是要继续用的呀(笑)。 所以呢,博客还是要写的,java还是最喜欢的,Python也是要学习的,当然其实更多是总结了。既然博客要启动了,这篇文字就权当测试吧,使用公…

    Linux干货 2015-03-13

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-12 14:12

    写的很好,排版也很棒,加油