马哥教育网络21期+第九周练习博客

马哥教育网络21期+第九周练习博客

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
[root@localhost bin]# cat 1.sh 
#!/bin/bash
#
while read line;do
   if [ `echo $line | cut -d ':' -f7` != '/sbin/nologin' ]; then
      let i++
   elif [ `echo $line | cut -d ':' -f7` = '/sbin/nologin' ];then
      let j++
   fi
done < /etc/passwd
echo "The default shell is '/sbin/nologin' $i."
echo "The default shell is not '/sbin/nologin' $j."

2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
[root@localhost bin]# cat 2.sh 
#!/bin/bash
#
hostname=`hostname`
if [ $hostname = 'localhost.localdomain' ];then
   `echo "www.magedu.com" > /proc/sys/kernel/hostname`
   echo "The host name: `hostname`."
fi

3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
root@localhost bin]# cat  3.sh 
#!/bin/bash
#
read -p "Please enter the disk device file path:" option
#
while true; do
  fdisk -l /dev/$option &> /dev/null 
   if [ $? -eq 0 ]; then
     echo "Disk path right."
    echo "`fdisk -l /dev/$option`"
      break
   fi
   if [ $? -ne 0 ];then 
     read -p "Please input the correct disk path:" option
   fi
done

4、写一个脚本,完成如下功能
 脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
[root@localhost bin]# cat 4.sh 
#!/bin/bash
#
read -p "Please input parameters:" option
while true ;do
  if [ "$option" == 'yes' ];then
    read -p "Please continue to input parameters:" option
  elif [ "$option" == 'quit' ];then
    echo "The normal exit."
  break
    return 0
  elif [ "$option" != 'quit' -a "$option" != 'yes' ];then
    echo "Non-normal exit."
    break
  fi
done

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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
[root@localhost bin]# cat 10.sh 
#!/bin/bash
#
i=`date +%Y%m%d`
if [ $# -lt 1 ]; then
   echo "Enter one option."
else 
case $1 in
gzip)
tar -czf/etc-$i.tar.gz hfz -C /backups
;;
bzip2)
tar -cjf/etc-$i.tar.bz2 hfz -C /backups
;;
xz)
tar -cJf/etc -$i.tar.xzhfz -C /backups
;;
*)
echo "exit."
;;
esac
fi

6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
[root@localhost bin]# cat 8.sh 
#!/bin/bash
#
read -p "Please enter the file path:" option
for file in /$option/* ; do
  if [ -f $file ];then
    echo "($file)  This file can be normal access."
   elif [ -L $file ]; then
    echo "($file) is This file is the access path."
  elif [ -d $file ]; then
    echo "($file) This file can use the CD command."
  else 
    echo "The other could not judge."
  fi
done

7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
[root@localhost bin]# cat 5.sh 
#!/bin/bash
#
i=`hostname`
if [ "$i" == 'localhost' ]; then
   `echo "mail.magedu.com" > /proc/sys/kernel/hostname`
    echo "The host name: `hostname`."
elif [ "$i" == 'none' ];then
    `echo "mail.magedu.com" > /proc/sys/kernel/hostname`
    echo "The host name: `hostname`."
else 
    echo "hostname is not equal to 'localhost' or 'none' so the hostname is $i."
fi

8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
[root@localhost bin]# cat 6.sh 
#!/bin/bash
#
i=`cat /etc/passwd | egrep "^$1" | cut -d':' -f3`
if [ $i -eq 0 ];then
   echo "This user to the administrator."
elif [ $i -gt 0 -a $i -le 500 ];then
   echo "This user for users of the system."
elif [ $i -gt 500 ];then
   echo "This user for ordinary users."
fi

10、写一个脚本,传递一个用户名参数给脚本;
 (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
(2) 否则,则显示无法登录系统;
[root@localhost bin]# cat 7.sh 
#!/bin/bash
#
i=`cat /etc/passwd | egrep "^$1" | cut -d':' -f3`
j=``
#

if [ $i -lt 500 ];then
   echo "user UID less 500"
   echo "Unable to login system."
fi
if [ $i -ge 500 ];then
  cat /etc/passwd | egrep  "^$1.*sh$" &> /dev/null
   if [ $? -eq 0 ];then
     echo "a user can log system."
   fi

fi

11、写一个脚本,完成如下任务 :
(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
 (2) 复制目录时,才使用cp -r命令;
(3) 复制文件时使用cp命令;
(4) 复制链接文件时使用cp -d命令;
(5) 余下的所有类型,使用cp -a命令;
[root@localhost bin]# cat 9.sh 
#!/bin/bash
#
for file in /var/log/* ; do
  if [ -f $file ];then
    cp $file /tmp/test1-testn &> /dev/null
   elif [ -L $file ]; then
    cp -d $file /tmp/test1-testn &> /dev/null
   elif [ -d $file ]; then
    cp -r $file /tmp/test1-testn &> /dev/null
   else
    cp -a $file /tmp/test1-testn &> /dev/null 
  fi
done

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

(0)
上一篇 2016-09-05 08:50
下一篇 2016-09-05 08:52

相关推荐

  • shell脚本3——循环语句

    流程控制:      顺序执行      选择执行      循环执行 循环执行:      将某代码段重复运行多次      重复运行多少次:  &…

    Linux干货 2016-08-18
  • N25_第九周作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; 2、写一个脚本     (1) 获取当前主机的主机名,保存于hostname变量中;     (2) 判断此变量的值是否为localhost,如果是…

    Linux干货 2017-02-02
  • Linux 基础(5)

    /etc/passwd           /etc/shadow         /etc/gpasswd (chage)            /etc/gshadow    usera…

    2017-07-22
  • vim编辑器整理(无演示)

    vim编辑器 vim编辑器是linux中最强大的全屏幕纯文本编辑器,他是vi编辑器的增强版。 文本编辑器: 文本:纯文本,在ASCII角度讲是纯粹的不加任何修饰的文本信息     支持Unicode编码方式 文本编辑器种类: 行编辑器:sed 全屏编辑器:nano,vi vi : (VIsual&n…

    Linux干货 2016-08-10
  • Corosync+Pacemaker+DRBD实现高可用 Mariadb

    一、DRBD简介 DRBD全称Distributed Replicated Block (分布式的复制块设备),开源项目。它是一款基于块设备的文件复制解决方案,速度比文件级别的软件如NFS,samba快很多,而且不会出现单点故障,是很多中小企业的共享存储首选解决方案。 二、DRBD的工作模式从上图中我们可以清楚的看到DRBD是工作在内核中,将协议建立在buf…

    2017-11-05
  • Linux的终端类型

    Linux的终端类型 Linux的终端类型: TTY(虚拟终端,图形终端) 早期的电子计算机的输入设备是穿孔卡片【又称霍列瑞斯式卡(Herman Hollerith)或IBM卡】,用有洞和无洞表示计算机二进制里面的0和1,以此来记录相关信息,但是这种方式很慢。 1986年股票代码被发明,人们通过电报和电传打印机发送实时股票价格,此时teletypewrite…

    Linux干货 2016-10-14

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-07 18:21

    非常的棒,脚本写得非常的漂亮,思路清晰,5题的排版需要注意一下。加油!