马哥教育网络班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如何进行分区和目录管理

    第七周作业   1、创建一个10G分区,并格式为ext4文件系统: 1、要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包括acl; ~]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sdb1 2、挂载至/data/mydata目录,要求挂载时禁止程序自动运行,县不更新…

    2017-09-18
  • find命令的基本用法

     linux 中find 常用示例解析 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]    其实[-H] [-L] [-P] [-D debugopts] [-Olevel]这几个选项并不常用(至少在我的日常工作中,没 &…

    Linux干货 2016-05-05
  • 关于shell脚本基础第二篇

                          shell脚本编程基础第二篇 read命令 使用read来把输入的值非配给一个或者多个shell变量,可以提示用户输入一些参数等,此时我们可以使用read命令来完成此功能 re…

    系统运维 2016-08-19
  • Linux用户和组的相关命令(二、组的相关命令)

    用户和组之间息息相关。创建用户时 ,Linux系统会默认生成一个与用户名相同的组,这个组是用户的私有组,也是用户的主组。对用户来说,主组有且只有一个,但是可以有零个或多个附加组。可以在组内设置组管理员来管理组内的用户列表,组管理员可以删除用户列表内的组员。组管理员不唯一,可以有零个或多个。 关于组及其属性和组密码及其属性的有关信息保存在/etc/group和…

    2017-07-22
  • 第二周

    反向单引号 ` `:执行能力强,可以执行命令 [等价于$()]单引号 ‘ ’:只显示字符双引号 “ ”:识别变量,不识别命令花括号{ }: 里面内容互相组合 打印重复字符串的简化形式echo file{1,3,5} 显示为file1.3.5rm -f file{1,3,5} 删除file1.3.5echo file{1..10} 显示为file1&#8230…

    Linux笔记 2018-07-29

评论列表(1条)

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

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