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

相关推荐

  • 正则表达式基础

    一、正则表达式: 元字符是用来阐释字符表达式意义的字符,简言之,就是用来描述字符的字符。 正则表达式RE(Regular Expression)是由一串字符和元字符构成的字符串。 正则表达式的主要功能是文本查询和字符串操作,它可以匹配文本的一个字符或字符集合。实际上正则表达式完成了数据的过滤,将不满足正则表达式定义的数据拒绝掉,剩下与正则表达式匹配的数据。 …

    Linux干货 2017-06-04
  • Linux bash中的命令行展开介绍及应用示例

    Linux bash中的命令行展开介绍及应用示例 在Linux bash中可以使用命令行展开特性一步完成需要分开成多步完成的操作,达到事半功倍的效果。在Linux指令参数位置使用"{}" 将相应的参数括起来,括号中的参数以逗号分隔,然后bash在执行这一指令时会自动将括号中的内容进行展开。 相关示例如下: 同时创建多个目录,在/tmp目录…

    Linux干货 2016-11-07
  • 重定向和管道

     重定向    程序:指令+数据    读入数据:Input    输出数据:Output  linux给程序提供三种I/O设备    标准输入(stdin)-0 默认接受来自键盘的输入    标准输出(stdinout)-1默认输出到终端…

    Linux干货 2016-08-10
  • 【社招】【小米-北京】系统工程师

    【社招】【小米-北京】系统工程师 【工作地点】北京市海淀区安宁庄东路72号科利源大厦 【薪酬福利】15k-30k  期权奖励、六险一金、水果花茶、班车、健身房、食堂 【投递方式】邮件主题“岗位+姓名”发送至lipengcheng3@xiaomi.com   岗位职责: 1.    &nbs…

    Linux职位 2017-07-28
  • 软件安装包的管理

    rpm安装包的管理 rpm的数据库( 公共) :/var/lib/rpm 程序包名称及版本 依赖关系 功能说明 包安装后生成的各文件路径及校验码信息 安装 rpm:{-i} -v:显示详细信息 -h:以#显示程序包管理执行进度 rpm -ivh PACKAGE_FILE … [install-options] –test :测试安装,但不真正执行安装过程 –…

    Linux干货 2017-04-23
  • N21-第五周

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; # grep "^[[:space:]]\+" /etc/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; # grep&nbs…

    Linux干货 2016-08-15

评论列表(1条)

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

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