马哥教育网络班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
下一篇 2016-08-09

相关推荐

  • shell 编程基础

    命令错误 后面的命令继续执行 语法错误 后面的命令不执行 bash -n 检查语法错位 bash -x 查看脚本的执行过程 【排错】 ++文件属性上的 显示是嵌套命令 变量 引用变量要加$符号 echo 是显示字符串的 如果命令能识别变量 可以不加$ 识别不了就压加$ 变量尽量用“ ”引起来 保留里面的格式 name =`whoami` echo &#822…

    Linux笔记 2018-04-15
  • Linux 第四天: (07月28日) Linux文件管理

    Linux 第四天: (07月28日) Linux文件管理         rootfs 根目录文件系统 root filesystemLSB  Linux Standard BaseFHS 文件系统分层结构 Filesystem Hierarchy Standard     蓝色表示 目…

    Linux干货 2016-08-08
  • GRUB加密、修复和破解密码实战指南

    GRUB加密、修复和破解密码实战指南 GRUB(Boot Loader) 从系统启动流程可以得知,在BIOS读取相关信息之后,接下来是去第一个可以启动的设备当中的MBR中读取Boot Loader信息,Boot Loader提供具有菜单功能、直接加载内核信息,以及相关的控制权转交功能。所以说系统启动必须要有Boot Loader,然后才能去加载内核。 Boo…

    Linux干货 2016-09-19
  • corosync+pacemaker+drbd+mysql配置安装详解

    一,  基本环境介绍及基本环境配置 节点1: node1.hulala.com      192.168.1.35     centos6.5_64    添加8G新硬盘 节点2: node1.hulala.com      192.168.1.36…

    Linux干货 2016-05-08
  • 什么是文件系统

    文件系统:层级结构;有索引; /: 原初起点; 倒置树状结构; /dev/pts/2: 最左侧/: 表示根目录 其它的/: 表示路径分隔符 Linux的路径分隔符是/ Windows的是\ 文件的路径表示: 绝对路径:从根开始表示出的路径  相对路径:从当前位置开始表示出的路径 文件名使用法则: 严格区分字符大小写:file1, File1, FI…

    Linux干货 2016-10-29
  • N31第四周作业

    本周(7.9–7.15)第4周 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [a@localhost ~]$ sudo cp -r /etc/skel /home/tuser1 [a@localhost ~]$ sudo chmod -R go= /hom…

    Linux笔记 2018-07-16

评论列表(1条)

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

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