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

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

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

#!/bin/bash

#

#count nologin

declare -i nologins=0

declare -i logins=0

file="/etc/passwd"

bashs=`cat $file  | cut -f7 -d:`

for i in $bashs ; do

 #echo $i

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

    let nologins++

  else

    let logins++

  fi

done

echo "Total nologin bash shell : $nologins."

echo "Total login bash shell : $logins."

用awk实现,统计每个shell数量:

 awk -F: '{bashs[$NF]++}END{for (i in bashs) {print "bash shell:"i,"\t bash shell count:"bashs[i]}}'  /etc/passwd

 

 cat /etc/passwd | awk -F: '{bashs[$NF]++}END{for (i in bashs) {print i,bashs[i]}}' 

2、写一个脚本

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

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

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

#!/bin/bash

#

#change hostname

hostname=`hostname`

  if [ $hostname == 'localhost' ]; then

    hostname www.magedu.com

    echo "hostname has change to 'www.magedu.com'. "

  else

    echo $hostname

  fi

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

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

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

#!/bin/bash

#

#show the dev info

readdevpath() {

  read -p "Please input the driver path: " drivers

   if [ $drivers = "quit" ]; then

      exit 2

   fi

   if  fdisk -l $drivers &> /dev/null  ; then

     fdisk -l $drivers

   else

     echo "invail driver"

     readdevpath

   fi

}

readdevpath

fdisk -l $drivers

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

   脚本能够接受一个参数;

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

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

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

#!/bin/bash

#

#arg select

if [ $# -lt 1 ]; then

  echo "<1"

  exit 2

fi

case $1 in

quit)

  echo "Exit…"

  exit 0;;

yes)

  echo "continue…";;

*)

  echo "oh no!"

  exit 4;;

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

#

#case tar bzip xz

if [ $# -lt 1 ]; then

  echo "Userage: `basename $0` gzip|bzip2|xz "

  exit 2

fi

case $1 in

gzip)

   mkdir /backups &> /dev/null

   tar czvf /backups/etc-20160613.tar.gz /etc &> /dev/null

   echo "tar gzip Backup /etc succesful!"

;;

bzip2)

   mkdir /backups &> /dev/null

   tar cjvf /backups/etc-20160613.tar.bz2 /etc &> /dev/null

   echo "tar bz2 Backup /etc succesful!"

;;

xz)

   mkdir /backups &> /dev/null

   tar cJvf /backups/etc-20160613.tar.xz /etc &> /dev/null

   echo "tar xz Backup /etc succesful!"

;;

*)

  echo "Tar Err!"

  echo "Userage: `basename $0` gzip|bzip2|xz "

  exit 2

esac

   

   

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

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

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

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

   (4) 其它为无法判断;

#!/bin/bash

#

#file type

if [ $# -lt 1 ]; then

  echo "Usare: `basename $0` arg (as a filename with path)."

  exit 2

fi

if [ -f $1 ]; then

  echo "$1 is a file"

elif

  [ -s $1 ]; then

  echo "$1 is a symbolic link file."

elif

 [ -d $1 ]; then

  echo "$1 is a directory.you can use 'cd $1'  change to it. "

else

  echo "Unknow file type."

fi

   

   

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

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

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

#!/bin/bash

#

#change hostname

ChangeHostName() {

    hostname mail.magedu.com

    echo "hostname has change to 'mail.magedu.com'. "

}

hostname=`hostname`

  if [ $hostname == "localhost" ]; then

    ChangeHostName

  elif

     [ $hostname == "none" ]; then

    ChangeHostName

  else

    echo "HOST Name: "$hostname

  fi

   

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

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

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

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

#!/bin/bash

#

#echo then user id info

declare -i i_uid

declare -i uid_type 

if [ $# -lt 1 ]; then

  echo "\"Userage: `basename $0`  args.\"  args for username."

fi

if id $1 &> /dev/null ; then 

 i_uid=`id -u $1`

 if [ $i_uid -eq 0 ]; then

   uid_type=0

 elif

   [ $i_uid -lt 501 ]; then

   uid_type=1

 else

   uid_type=2 

 fi

 case $uid_type in 

 0)

   echo "User:$1 is root user."

 ;;

 1)

   echo "User:$1 is a system user."

 ;;

 *)

   echo "User:$1 is a comma user."

 esac

else

  echo "$1: No such user!"  

fi

   

   

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

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

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

#!/bin/bash

#

#echo shell can be login into system

declare -i idno

if [ $# -lt 1 ]; then

  echo "Useage: `basename $0` args .(args for username)"

  exit 2

fi

if id $1 &> /dev/null ; then

  idno=`id -u $1`

  if [ $idno -ge 500 ] ; then

    if `grep $1 /etc/passwd | grep sh$ &> /dev/null` ; then

      echo "User:$1 can log system."

    else

      echo "User:$1 can not login to system."

    fi

  else

    echo "User:$1 can not login to system."

  fi

else

  echo "$1: no such user!"

fi

  

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

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

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

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

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

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

#!/bin/bash

#

#copy file to /tmp/test1-testn

sdir=/var/log

ddir=/tmp/test1-testn

files=`ls $sdir`

if [ ! -d $ddir ]; then

  mkdir -pv $ddir

fi

cd $sdir

for i in $files ; do

  if [ -L $i ] ; then

    filetype="s"

  elif

    [ -d $i ] ; then

    filetype="d"

  elif

    [ -f $i ] ; then

    filetype="f"

  else

    filetype="o"

  fi

case $filetype in

  s)

   cp -d $i $ddir

  # echo "cp -d $i"

   ;;

  d)

   cp -r $i $ddir

  # echo "cp -r $i"

   ;;

  f)

   cp $i $ddir

  # echo "cp $i"

   ;;

   

  *)

   cp -a $i $ddir

  # echo "cp -a $i"

   ;;

esac

done

   

   

   

原创文章,作者:马哥Net19_小斌斌,如若转载,请注明出处:http://www.178linux.com/22027

(0)
马哥Net19_小斌斌马哥Net19_小斌斌
上一篇 2016-07-07 10:45
下一篇 2016-07-07 10:45

相关推荐

  • if case语句练习

     1、 写一个脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息 [root@localhost bin]# cat createuser.sh #!/bin/bash # Date…

    Linux干货 2016-08-15
  • Linux进程和计划任务

                                                      &nbsp…

    系统运维 2016-09-21
  • Mairadb

    Mairadb 编译安装Mairadb数据 先下载mariadb的软件包: 将软件包解压缩到/usr/local目录下 ]# tar xf mariadb-5.5.32-linux-x86_64.tar.gz -C /usr/local/ 创建连接 ]# ln -sv maria…

    Linux干货 2016-10-17
  • 网络管理实战(子网划分、单网卡多IP、多网卡单IP、Linux路由实现)

        1、某公司申请到一个C 类IP 地址,但要连接6 个的子公司,最大的一个子 公司有26 台计算机,每个子公司在一个网段中,则子网掩码应设为?          分析过程:C类地址标准的掩码为24位,因为有6个子公…

    Linux干货 2016-09-05
  • TCP 的那些事儿(下)

    这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看看上篇《TCP的那些事儿(上)》 上篇中,我们介绍了TCP的协议头、状态机、数据重传中的东西。但是TCP要解决一个很大的事,那就是要在一个网络根据不同的情况来动态调整自己的发包的速度,小则让自己的连接更稳定,大则让整个网络更稳定。在你阅读下篇之前,你需要做好准备,本篇文章有好些算法和策略,可能会引发你的…

    Linux干货 2016-08-15

评论列表(2条)

  • 马哥教育
    马哥教育 2016-07-07 11:34

    写的很好,排版还可以在漂亮一点,加油

    • 马哥Net19_小斌斌
      马哥Net19_小斌斌 2016-07-25 22:58

      @马哥教育排版一提交就变了,有什么方法可以排得好点呢?