马哥教育网络班21期-第九周课程练习

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

#!/bin/bash
#
declare -i login_user=0
declare -i nologin_user=0
while read line ;do
  login_shell=$(echo $line | awk -F: '{print $7}')
  if [ $login_shell == '/sbin/nologin' ];then
    let nologin_user++
  else
    let login_user++
  fi
done < /etc/passwd
echo "nologin user have $nologin_user"
echo "login user have $login_user"

2、写一个脚本

    (1) 获取当前主机的主机名,保存于hostname变量中;

    (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;

    (3) 否则,则显示当前主机名;

#!/bin/bash
#
hostname=$(hostname)
if [ $hostname == 'localhost' ];then
   `hostname www.magedu.com`
else
  echo $hostname
fi

3、写一个脚本,完成如下功能

    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

    (2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
#
if [ $# -ne 1 ];then
  echo "Usage: $0 dev_path"
  exit
fi
for list in $(ls /dev);do
  if [[ $1 = /dev/$list ]];then
    fdisk -l $1
  fi
done

4、写一个脚本,完成如下功能

   脚本能够接受一个参数;

   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;

   (2) 如果参数1为yes,则显示继续执行脚本;

   (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
#
if [ $# -ne 1 ];then
  echo "Usage: $0 argument"
  exit
fi
case $1 in
  quit)
    exit ;;
  yes)
    echo "script will continue" ;;
  *)
    echo "execute abnormal"
    exit 11 ;;
esac

   

   

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
#
while [ $# -eq 1 ];do
  case $1 in
    gzip)
     tar cfvz /backups/etc-20160613.tar.gz /etc
     exit
    ;;
    bzip2)
     tar cfjv /backups/etc-20160613.tar.bz2 /etc
     exit
    ;;
    xz)
     tar cfJv /backups/etc-20160613.tar.xz /etc
     exit
    ;;
    *)
     echo "the compression tool is wrong"
     exit 11
    ;;
  esac
done

   

6、写一个脚本,接受一个路径参数:

   (1) 如果为普通文件,则说明其可被正常访问;

   (2) 如果是目录文件,则说明可对其使用cd命令;

   (3) 如果为符号链接文件,则说明是个访问路径;

   (4) 其它为无法判断;

#!/bin/bash
#
read -p "pls input a path:"  var
if [ -z "$var" ];then
  echo "Usage: $0 file_path"
  exit
fi
if [ -f $var ];then
  echo "$var is a normal file,you can access"
elif [ -d $var ];then
  echo "$var is a Directory,you can use cd command"
elif [ -L $var ];then
  echo "$var is a softlink to a access path"
else
  echo "unknown file"
fi

   

7、写一个脚本,取得当前主机的主机名,判断

   (1) 如果主机名为空或为localhost,或为""(none)"",则将其命名为mail.magedu.com;

   (2) 否则,显示现有的主机名即可;

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

   

8、写一脚本,接受一个用户名为参数;

   (1) 如果用户的id号为0,则显示其为管理员;

   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

   (3) 否则,则显示其为普通用户;

#!/bin/bash
#
read -p "pls input a username: "  user
if ! id $user &> /dev/null ;then
  echo "$user is not exist"
  exit 11
fi
uid=$(id -u $user)
if  [ $uid -eq 0 ];then
  echo "$user is a administrator"
elif [ $uid -gt 0 -a $uid -lt 500 ];then
  echo "$user is system user"
else
  echo "$user is a common user"
fi

10、写一个脚本,传递一个用户名参数给脚本;

   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

   (2) 否则,则显示无法登录系统;

#!/bin/bash
#
if [ $# -ne 1 ];then
  echo "Usage: $0 username"
  exit 11
fi
if ! id $1 &> /dev/null;then
  echo "$1 is not exist"
  exit 12
fi
if [ $(id -u $1) -ge 500 ] && awk -F: '/sh$/{print $7}' /etc/passwd &> /dev/null;then
  echo " user $1 can login system "
else
  echo "user $1 can't login system"
fi

11、写一个脚本,完成如下任务 :

   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;

   (2) 复制目录时,才使用cp -r命令;

   (3) 复制文件时使用cp命令;

   (4) 复制链接文件时使用cp -d命令;

   (5) 余下的所有类型,使用cp -a命令;"

#!/bin/bash
#
dir_path="/var/log"
bak_path="/tmp/test"
declare -i num=1
for list in $(ls $dir_path);do
  cd $dir_path
  mkdir $bak_path$num
  if [ -d $list ];then
    cp -r $list $bak_path$num/$list
  elif [ -f $list ];then
    cp $list $bak_path$num/$list
  elif [ -L $list ];then
    cp -d $list $bak_path$num/$list
  else
    cp -a $list $bak_path$num/$list
  fi
  let num++
done

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

赞 (0)
Net21_木头Net21_木头
上一篇 2016-09-15 22:21
下一篇 2016-09-15 22:21

相关推荐

  • 2016/10/14作业

    操作类: 1、设置自己的终端提示符,要求终端登陆时:     a> 需要带颜色      b> 需要显示当前执行到了第几条命令      c> 显示当前登录终端,主机名和当前时间 首先我们要了解一个环境…

    Linux干货 2016-10-19
  • 马哥教育网络21期+第二周练习博客

    马哥教育网络21期+第二周练习博客 复习第一节课内容: 1,计算机的组成及其功能:     运算器,控制器,存储器,输入设备,输出设备 2,Linux的发行版:     shackware,debian,redhat 3,Linux的哲学思想 一切介文件把几乎所有资源,包括硬件设…

    Linux干货 2016-07-12
  • 作业——2016-10-17

    1 生产环境发现一台服务器系统时间产生偏差,造成服务异常,请帮忙校正。     将系统时间同步到硬件时间的命令是hwclock -s 2 生产有一个数据同步脚本需要执行很长时间,怎样做到无人值守,在管理工具退出的情况下,脚本依然能正常运行。     screen -S work  进去后开始做工作,暂时离开时…

    Linux干货 2016-10-18
  • linux中用户、组和权限认识

    linux中用户、组和权限认识 Linux 用户和组的主要配置文件:/etc/passwd :用户及其属性信息( 名称、UID 、主组ID 等)                  …

    Linux干货 2017-02-23
  • 手动添加用户

        通常使用useradd命令可以轻松添加一个用户,然后使用passwd命令设置一个密码后就可以登录系统了,其实这一过程完成可以自己手动完成,下面就让我们来通过修改配置文件来添加一个用户。 一、修改/etc/passwd文件     在etc/passwd文件中手动添加一行内…

    Linux干货 2015-04-27
  • old notes

    yum update: update software guest edition ***: how to install guest edition: 1) need to root: su – 2) yum install kernel-devel 3)yum install gcc* ( when don't remember n…

    Linux干货 2016-09-19

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-19 18:12

    写脚本的时候不要使用系统的函数名作为变量名,切记