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

相关推荐

  • 文本三剑客 grep sed awk

    模式,选项。常用用法。

    2017-12-03
  • 简单的bash脚本查看任意网段的在线主机

    一前言         最近看到许多同志在写ping某个地址段的bash脚本,我也心血来潮来了一发。  当然本人新手,大神勿喷。 二准备工作           linux系统的机…

    2017-03-02
  • 马哥教育网络班22期-第1周博客作业2

    date命令 date – print or set the system date and time 显示或设置系统日期和时间 【SYNOPSIS】 date [OPTION]… [+FORMAT] 【OPTIONS】 -d:–date=STRING,显示字符串所指的日期与时间, 而不是当前时间,字符串前后必须加上双引号…

    Linux干货 2016-08-15
  • 关于tar命令的一些方法

    tar 解压缩文件时只有当文件格式中有tar才能使用,否则使用其他单独的特定解压缩工具,例如uncompress,gzip ,bzip2.xz等等 tar压缩解压缩命令详解 tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命…

    Linux干货 2016-08-29
  • Linux基础知识

    一、描述计算机的组成及其功能 计算机由运算器、控制器、存储器、I/O设备五大部件组成。 运算器和控制器统称为中央处理器,俗称CPU。    1、运算器:实现算术运算和逻辑运算的部件;    2、控制器:计算机的指挥系统,控制计算机的其他部件,使得个部件有条不紊地协调工作;    3、存储器:存储数据…

    2017-07-08
  • 闲玩Xen

    简述理论 虚拟化的实现 模拟技术:模拟计算机的主要硬件硬件并协调工作,但是效率很低下,所以不常用 完全虚拟化:针对cpu的虚拟来分为,基于BT技术(Guset的特权指令在ring1,Host的特权指令在ring0,Guest的特权指令由Host的ring0来参与边翻译边执行)的完全虚拟化,基于HVM(硬件辅助,Guest的特权指令在ring0,Host的特权…

    Linux干货 2016-09-26

评论列表(1条)

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

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