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

相关推荐

  • LINUX的初探

    一般而言,计算机的组成遵循着“冯诺依曼体系结构”。这种结构的体现实质上是一种存储程序的原理,把程序本身当作数据来对待,程序和该程序处理的数据用同样的方式来存储。这种结构的要点是:计算机的数制采用二进制;计算机应该按照程序顺序执行。

    2018-03-04
  • httpd协议配置进阶

    目录 虚拟主机配置 status状态页面 curl命令 mod_deflate模块 https配置 httpd自带应用程序 虚拟主机配置 有三种实现方案:    基于IP:        为每个虚拟主机准备至少一个IP地址    基于端口:     &nbsp…

    Linux干货 2016-10-31
  • linux进程管理

    linux系统进程查看及管理工具 对于服务器来说重要的IO 设备磁盘网络 pstree,ps,pidof,pgrep,top,htop,glances,pmap,vmstat,dstat,kill,pkill,job,bg,fg,nohup,nice,renice,killall….. pstree命令: pstree-display …

    Linux干货 2016-09-09
  • 一键获取MySQL账户及相关的权限

    有时候,运维同学可能在做grant授权时候,给的用户权限太高了,有风险。因此,我们接手服务器后有必要检查一遍用户权限。 #!/bin/bash# 一键获取MySQL账户及相关的权限 user=’root’ password=’123456′ MYSQL_CMD=’/usr/local/mysql/b…

    Linux干货 2017-05-07
  • Linux文件查找及压缩

    Linux文件查找(locate & find) locate     查询系统上预建的文件索引数据库(速度快,但更新不实时)     /var/lib/mlocate/mlocate.db     依赖于事先构建的索引 &nbsp…

    Linux干货 2016-08-19
  • shell编程之数组及环境变量

    数组 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的 集合 索引:编号从0 声明数组: declare -a ARRAY_NAME declare -A ARRAY_NAME:关联数组 1.declare  -a menu  menu[0]=beef  menu[1]=chicken menu…

    Linux干货 2016-08-29

评论列表(1条)

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

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