马哥教育网络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)
wostopwostop
上一篇 2016-09-05 08:51
下一篇 2016-09-05 08:51

相关推荐

  • Find命令简介与使用

    用途:         find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。 语法: find [OPTION]… [查找…

    Linux干货 2016-08-30
  • 0805作业

    课堂练习 1.找出ifconfig 命令结果中本机的所有IPv4 地址   ifconfig|tr -cs '[0-9].' '\n'|sort -ut '.' -k3 2.查出分区空间使用率的最大百分比值   df|tr -s ' '|cut -d&quot…

    Linux干货 2016-08-07
  • 文件查找命令(find、locate)

    在文件系统上查找符合条件的文件: 实现工具:locate, find locate: 依赖于事先构建好的索引库; 系统自动实现(周期性任务); 手动更新数据库(updatedb); 工作特性: 查找速度快; 模糊查找; 非实时查找; locate [OPTION]… PATTERN… -b:只匹配路径中的基名; -c:统计出共有多少个符合条件的文件;…

    Linux干货 2016-11-06
  • python Django分页

    自定义html_helper.py  —>  Page_helper类 #coding:utf-8 from django.utils.safestring import mark_safe class Page_Helper(object):    …

    Linux干货 2016-08-22
  • shell脚本执行

    shell脚本执行 对于我们这些小白而讲,shell脚本无疑是最令人头疼的事了,我再这里讲一下shell脚本运行要注意的哪些。 shell脚本就好像c++,java代码一样,按照从上到下然后从左到右的顺序执行。就像我们日常读书一样。如果shell命令遇见了子进程(嵌套代码),程序就会跟着顺序进入子进程。当子进程完成后,回到父进程,并且把父进程所需要的参数提交…

    2017-06-05
  • Linux发行版之间的联系与区别及其哲学思想。

    一、Linux发行版之间的联系与区别。 随着Linux不断发展,Linux所支持的文件系统类型也在迅速扩充。很多的数据中心服务器上都运行着Linux,可以节省大量的许可证费用及维护费用。但伴随着Linux新版本的发行,其中每一个不同版本的Linux所支持的文件系统类型都有所不同。Linux的发行版有数百种之多。 1、Debian:是指一个致力于创建自由操作系…

    Linux干货 2016-10-30

评论列表(1条)

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

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