马哥教育网络班19期+第九周课程练习

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin),分别这两类用户的个数,通过字符串比较来实现。

#!/bin/bash

for user_shell in `awk -F: '{print $7}' /etc/passwd`;do

     if [ "$user_shell" = "/sbin/nologin" ];then

        echo "`awk -F: '{print $7}' /etc/passwd | grep -c /sbin/nologin` users is nologin"
        break

     elif [ "$user_shell" != "/sbin/nologin" ];then
        echo "`awk -F: '{print $7}' /etc/passwd | grep -cv /sbin/nologin` users is login"

     fi
done

2、写一个脚本
 (1) 获取当前主机的主机名,保存于hostname变量中;
 (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
 (3) 否则,则显示当前主机名;

#!/bin/bash

hostname=`hostname`

if [ "$hostname" == "localhost" ];then
        hostname www.magedu.com
else
      hostname
fi

3、写一个脚本,完成如下功能
 (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
 (2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
read -p "please input a block device: " device
case $device in
/dev/[hs]d*)
        echo "`fdisk -l $device`"
;;
*)
        echo "please input a exist block device"
;;
esac

4、写一个脚本,完成如下功能
脚本能够接受一个参数;
 (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
 (2) 如果参数1为yes,则显示继续执行脚本;
 (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash

read -p "please input a word: " i
if  [ $i == "quit" ];then
        echo "ok"
    elif [ $i == "yes" ];then
         echo "please retry"
         read -p "please input a word: " i
      if [ $i == "quit" ];then
         echo "ok"
      else
         echo "error exist"
         exit 1
      fi
    else
         echo "error exist"
         exit 1
    fi

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

if  [ ! -e /backups ];then
mkdir /backups
fi

read -p "please choose the Compression tool(gzip|bzip2|xz) " i

case $i 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 "please choose the correct tools"
        exit 1
;;

esac

6、写一个脚本,接受一个路径参数:
   (1) 如果为普通文件,则说明其可被正常访问;
   (2) 如果是目录文件,则说明可对其使用cd命令;
   (3) 如果为符号链接文件,则说明是个访问路径;
   (4) 其它为无法判断;

#!/bin/bash
read -p "please input something " n

if [ -f $n ];then
   echo "$n can be access"
elif [ -d $n ];then
   echo "$n can be use "cd" command"
elif [ -L $n ];then
   echo "this is a access path"
else
   echo "$n can't be judge" 
fi

7、写一个脚本,取得当前主机的主机名,判断
   (1) 如果主机名为空或为localhost,或为""(none)"",则将其命名为mail.magedu.com;
   (2) 否则,显示现有的主机名即可;

#!/bin/bash
hostname=`hostname`
if [ -z "$hostname" ]||[ $hostname == "localhost" ]||[ $hostname == \"\"\(none\)\"\" ];then
    hostname mail.magedu.com
else
    echo $hostname
fi

8、写一脚本,接受一个用户名为参数;
   (1) 如果用户的id号为0,则显示其为管理员;
   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
   (3) 否则,则显示其为普通用户;

#!/bin/bash

if [ $# -eq 1 ] && id $1>/dev/null;then
uid=`grep "$1" /etc/passwd | cut -d: -f3`
 if [ $uid -eq 0 ];then
  echo "this is root"
 elif [ $uid -gt 0 ]&&[ $uid -lt 500 ];then
  echo "this is system user"
 elif [ $uid -gt 500 ];then
  echo "this is normal user"
 fi
 else
  echo "please input the exist username"
fi

9、写一个脚本,传递一个用户名参数给脚本;
   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
   (2) 否则,则显示无法登录系统;

#!/bin/bash

if [ $# -eq 1 ]&&id $1>/dev/null;then
uid=`grep "$1" /etc/passwd | cut -d: -f3`
 if [ $uid -ge 500 ]&& grep "sh$" /etc/passwd>/dev/null;then
        echo "a user can log system"
 else
        echo "the user can't login"
 fi
else
 echo "please input a exist user"
fi

10、写一个脚本,完成如下任务 :
   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
   (2) 复制目录时,才使用cp -r命令;
   (3) 复制文件时使用cp命令;
   (4) 复制链接文件时使用cp -d命令;
   (5) 余下的所有类型,使用cp -a命令;

#!/bin/bash

if ! [ -d /tmp/test1-testn ];then
    mkdir -p /tmp/test1-testn
fi
for file in `ls /var/log`;do
    if [ -d $file ];then
        cp -r /var/log/$file /tmp/test1-testn
    elif [ -f $file ];then
        cp  /var/log/$file /tmp/test1-testn
    elif [ -L $file ];then
        cp -d /var/log/$file /tmp/test1-testn
    else
        cp -a /var/log/$file /tmp/test1-testn
    fi
done

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

(0)
N19_leoN19_leo
上一篇 2016-06-30 09:35
下一篇 2016-07-02 10:47

相关推荐

  • 马哥教育网络20期+第五周练习博客

    1、  显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@localhost ~]# grep "^[[:space:]]\+.*" /boot/grub/grub.conf          root (hd0…

    Linux干货 2016-08-02
  • N21沉舟15周作业

    1、总结sed和awk的详细用法; 2、删除/boot/grub/grub.conf文件中所有行的行首的空白字符; #  sed 's@^[[:space:]]@@' /boot/grub/grub.conf 3、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字…

    Linux干货 2016-11-14
  • Linux基础知识之用户和组的配置文件解析

    实验环境:  Linux系统的版本为CentOS6.8_x86_64版本,以root用户远程用xshell连接,进行实验。 1.创建用户设置的配置文件:/etc/default/useradd        useradd 的配置文件如下图所示:        &nbs…

    Linux干货 2016-08-02
  • 第二章 操作系统发展史

    Linux主流版本:   Debian(英语发音:/ˈdɛbiən/)是由GPL和其他自由软件许可协议授权的自由软件组成的操作系统,由Debian项目(Debian Project)组织维护。   历史: Debian于1993年8月16日由一名美国普渡大学学生伊恩·默多克(Ian Murdock)首次发表。伊恩·默多克最初把他的系统称为…

    Linux干货 2016-06-03
  • linux 磁盘管理与文件挂载

        磁盘对于linux来说是至关重要的而且与它 的中心思想相关联,一切皆文件,因此就把磁盘分为了好多类型, 如:块设备,字符设备。对于每类磁盘的使用,我们对它进行了分区与规划,这就用到我们的分区与查看工具 fdisk,理论上磁盘容量足够时,可以建立任意数量的分区。  一.磁盘的管理与工具 1.  fdisk 强大的…

    2017-06-17
  • VMware12 Centos系统安装VMware-tools过程

    1、点击VMware菜单虚拟机选中安装VMware-tools:           注:虚拟机桌面会生成VMwaretools DVD安装光盘 2、查看虚拟机设置ISO映像文件是否是VMwaretoolsDVD带Linux.iso映像的文件: 3、在centos系统中 open term…

    Linux干货 2016-07-26

评论列表(1条)

  • 马哥教育
    马哥教育 2016-07-04 14:01

    写的很好,排版也很棒,但是有的题目还是有点小瑕疵,比如第4个,如果用while会不会更好 加油