马哥教育网络班21期+第9周课程练习

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

[root@localhost test]# ./exercise3.sh 
be eable to login users sum:5
can't login users sum:29
[root@localhost test]# cat exercise3.sh 
#!/bin/bash
declare -i logincount=0
declare -i nologincount=0
for i in $(cut -d: -f7 /etc/passwd);do
if [ "$i" == "/sbin/nologin" ];then
let nologincount++
continue
else
let logincount++
fi
done
echo "be eable to login users sum:$logincount"
echo "can't login users sum:$nologincount"

2、写一个脚本

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

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

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

[root@localhost test]# hostname
localhost.localhostdomain
[root@localhost test]# ./exercise4.sh 
www.magedu.com
[root@localhost test]# hostname
www.magedu.com
[root@localhost test]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=www.magedu.com
[root@localhost test]# cat exercise4.sh 
#!/bin/bash
hostname=$HOSTNAME
if [ "${hostname%.*}" == "localhost" ];then
sed -i  '/^HOSTNAME=/d' /etc/sysconfig/network && 
echo "HOSTNAME=www.magedu.com" >> /etc/sysconfig/network && hostname www.magedu.com && hostname
else
echo "$HOSTNAME"
fi

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

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

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

[root@localhost test]# ./exercise5.sh /dev/
Please input a illegal device path!
[root@localhost test]# ./exercise5.sh /etc/
Please input a illegal device path!
[root@localhost test]# ./exercise5.sh /dev/sda1
/dev/sda1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       477M   29M  424M   7% /boot
[root@localhost test]# ./exercise5.sh /dev/sda
/dev/sda
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a8052
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1959    15215616   8e  Linux LVM
[root@localhost test]# cat exercise5.sh 
#!/bin/bash
[ $# -eq 0 ] && echo "must input a device path!" && exit 12
[ $# -gt 1 ] && echo "too many args,only for one!" && exit 13
[ ! -e $1  ] && echo "not a decive path or doesn't exisits!" && exit 14
if fdisk -l $1 &>/dev/null && echo "$1" | grep "^/dev/[hs]d[a-z]$"; then
fdisk -l $1
elif fdisk -l $1 &>/dev/null && echo "$1" | grep "^/dev/[sh]d[a-z][[:digit:]]$"; then
df  -h $1
else
echo "Please input a illegal device path!"
fi

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

   脚本能够接受一个参数;

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

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

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

[root@www test]# ./exercise6.sh 
quit) exit script
yes) continue script
=======================
Enter a option:quit
see you around!
[root@www test]# echo $?
0
[root@www test]# ./exercise6.sh 
quit) exit script
yes) continue script
=======================
Enter a option:yes
testing!
[root@www test]# ./exercise6.sh 
quit) exit script
yes) continue script
=======================
Enter a option:kg
force exit!
[root@www test]# echo $?
12
[root@www test]# cat exercise6.sh 
#!/bin/bash
cat << EOF
quit) exit script
yes) continue script
=======================
EOF
read  -p "Enter a option:" option
function test {
echo "testing!"
}
case $option in 
     "quit")
  echo "see you around!"
  exit 0
  ;;
      "yes")
   test
   ;;
*)
echo "force exit!"
exit 12
;;
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) 其它任意值,则显示错误压缩工具,并执行非正常退出;

[root@www test]# ls -l  /backups/etc-20160804
-rw-r--r--. 1 root root 6196128 Aug  4 21:26 /backups/etc-20160804
[root@www test]# cat exercise8.sh 
#!/bin/bash
cat << EOF
 gzip) To compress and archive by gzip and tar;
bzip2) To compress and archive by bzip2 and tar;
   xz) To compress and archive by xz and tar;
=======================
EOF
read  -p "Enter a option:" option
case $option in 
     "gzip")
  [ -d /backups/ ] || mkdir /backups/
  tar -zcf /backups/etc-$(date +%Y%m%d) /etc/
  echo "gzip compressed"
  exit 0
  ;;
      "bzip2")
  [ -d /backups/ ] || mkdir /backups/
  tar -jcf /backups/etc-$(date +%Y%m%d) /etc/
  echo "bzip2 compressed"
  exit 0 
   ;;
      "xz")
  [ -d /backups/ ] || mkdir /backups/
  tar -Jcf /backups/etc-$(date +%Y%m%d)  /etc/
  echo "xz compressed"
  exit 0 
    ;;
*)
echo "force exit!"
exit 12
;;
esac

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

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

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

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

   (4) 其它为无法判断;

[root@www test]# ./exercise9.sh /etc/issue
/etc/issue is a common file!
[root@www test]# ./exercise9.sh /etc/rc.d/rc3.d/S55sshd 
/etc/rc.d/rc3.d/S55sshd is a symbol link file use ls -l for more info!
[root@www test]# ./exercise9.sh /dev/sda
/dev/sda doesn't exisits or maybe other type file!
[root@www test]# cat exercise9.sh 
#!/bin/bash
[ $# -eq 0  -o $# -gt 1 ] && echo "only for one arg or too many args!" && exit 12
if [ -f $1 -a ! -h $1 ];then
echo "$1 is a common file!"
elif [ -d $1 ];then
echo "$1 is a directory,you can use command cd to access!"
elif [ -h $1 ];then
echo "$1 is a symbol link file use ls -l for more info!"
else
echo "$1 doesn't exisits or maybe other type file!"
fi

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

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

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

[root@www test]# bash -x exercise10.sh
+ '[' -z www.magedu.com -o www.magedu.com == localhost -o www.magedu.com == none ']'
+ hostname
www.magedu.com
[root@www test]# cat exercise10.sh 
#!/bin/bash
if [ -z $HOSTNAME -o "$HOSTNAME" == "localhost" -o "$HOSTNAME" == "none" ];then
 hostname mail.magedu.com && sed -i '/^HOSTNAME/d' /etc/sysconfig/network &&
 echo "HOSTNAME=mail.magedu.com" >> /etc/sysconfig/network
else
hostname
fi

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

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

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

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

[root@www test]# ./exercise11.sh hehe
id: hehe: No such user
[root@www test]# ./exercise11.sh root
root is super user
[root@www test]# ./exercise11.sh ntp
ntp is System user
[root@www test]# ./exercise11.sh derulo
derulo is Common user
[root@www test]# cat exercise11.sh 
#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ]&& echo "only for one user account or too many user account!"&& exit 12
userid=$(id $1 | cut -d " " -f1 | tr "()" " " | cut -d "=" -f2 | cut -d " " -f1)
echo "$userid $1" >/tmp/id.txt
id $1 &>/dev/null && awk '{if($1==0){print $2 " is super user";}else if($1>0 && $1<500)
{print $2 " is System user";}else{print $2 " is Common user";}}' /tmp/id.txt

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

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

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


[root@www test]# ./exercise12.sh root
you cannot login!
[root@www test]# ./exercise12.sh derulo
a user can login!
[root@www test]# ./exercise12.sh hehe
user doesn't exisits!
[root@www test]# cat exercise12.sh 
#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ]&& echo "only for one user account or too many user account!"&& exit 12
if id $1 &>/dev/null;then
userid=$(id $1 | cut -d " " -f1 | tr "()" " " | cut -d "=" -f2 | cut -d " " -f1)
usershell=$(finger $1 | grep "Shell" | cut -d ":" -f3 | cut -d " " -f2)
userend=${usershell:0-2}
echo "$userid $1 $usershell $userend" >/tmp/idtmp.txt
else
echo "user doesn't exisits!"
exit 13
fi
awk '{if($1>=500 && $4=="sh"){print "a user can login!";}else{print "you cannot login!";}}' /tmp/idtmp.txt

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

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

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

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

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

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

[root@www test]# ./exercise13.sh 
/var/log/anaconda.ifcfg.log copy to /tmp/tes1-testn/ finished!
/var/log/btmp copy to /tmp/tes1-testn/ finished!
/var/log/spooler-20160804 copy to /tmp/tes1-testn/ finished!
/var/log/tallylog copy to /tmp/tes1-testn/ finished!
.......
[root@www test]# cat exercise13.sh 
#!/bin/bash
sdir="/var/log"
ddir="/tmp/tes1-testn/"
[ ! -e $ddir ] && mkdir $ddir
for i in $(ls -l $sdir | grep "^-" | awk '{print $NF}');do
cp $sdir/$i  $ddir
echo "$sdir/$i copy to $ddir finished!"
done
for m in $(ls -l $sdir | grep "^d" | awk '{print $NF}');do
cp -r $sdir/$m  $ddir
echo  "$sdir/$m copy to $ddir finished!"
done
for n  in $(ls -l $sdir | grep "^l" | awk '{print $NF}');do
cp -d  $sdir/$i  $ddir
echo "$sdir/$n copy to $ddir finished!"
done
for k in $(ls -l $sdir | grep -v "^[-dl]" | awk '{print $NF}');do
cp -a $sdir/$i  $ddir
echo "$sdir/$k copy to $ddir finished!"
done
[root@www test]# cd /tmp/tes1-testn/
[root@www tes1-testn]# ls -a
.                     btmp-20160804  maillog-20160804   spooler
..                    ConsoleKit     messages           spooler-20160804
anaconda.ifcfg.log    cron           messages-20160804  sssd
anaconda.log          cron-20160804  ntpstats           tallylog
anaconda.program.log  cups           pm-powersave.log   wpa_supplicant.log
anaconda.storage.log  dmesg          ppp                wtmp
anaconda.syslog       dmesg.old      prelink            Xorg.0.log
anaconda.xlog         dracut.log     sa                 Xorg.0.log.old
anaconda.yum.log      gdm            samba              Xorg.1.log
audit                 httpd          secure             Xorg.9.log
boot.log              lastlog        secure-20160804    yum.log
btmp                  maillog        spice-vdagent.log

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

(0)
上一篇 2016-08-05 16:09
下一篇 2016-08-05 16:09

相关推荐

  • CentOS 6 系统启动流程

    一、CentOS 6 启动流程 1.加载BIOS的硬件信息,获取第一个启动设备。 2.读取第一个启动设备MBR的引导加载程序(grub)的启动信息 3.加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备。 4.核型执行init程序并获取运行信息。 5.Init执行/etc/rc.d/rc.sysinit文件。 6.启动核心的外挂模块(/et…

    Linux干货 2016-09-12
  • liunx性能监控工具总结

    1.uptime 1)显示的信息:显示当前时间,系统已启动的时间,当前在线人数,系统平均负载(1分钟、5分钟、10分钟的平均负载,一般不会超过1) 2)系统平均负载:指在特定时间间隔内运行队列中的平均进程数。 3)如果每个CPU内核的当前活动进程数不大于3的话,那么系统的性能良好。如果每个CPU内核的任务数大于5,那么这台及其的性能有严重问题 4)ruguo…

    Linux干货 2016-09-09
  • 马哥教育网络班21期第七周作业

    1、创建一个10G分区,并格式为ext4文件系统;   (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;   (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@localhost ~]#…

    Linux干货 2016-10-09
  • centos 7 修改sshd服务默认端口号

    由于最近sshd服务默认端口号22被匿名进行试探性的进行登入,为防止匿名用户再次进行试探性的登入,将sshd服务的默认端口号进行修改。 环境:centos 7.3  xshell 思路:先将sshd的被指文件进行修改,把sshd服务的默认端口号修改为所需要的端口号,然后重启sshd服务,发现重启报错。找其原因是应为selinux不允许自定义sshd…

    Linux干货 2017-04-06
  • linux文件管理类命令汇总及几个通配的查找事例

    linux文件管理类命令汇总 1.文本文件查看类命令:cat,tac,more,less,tail,head cat(tac): 正向(反向)显示 cat [OPTION]… [FILE]…                -E:显示行结束符$ -n:对显示出的每一行进行编…

    Linux干货 2016-09-24
  • 通用二进制安装MySQL(MariaDB)

    一、前言     MySQL是一个关系型数据库管理系统,是最流行的关系型数据库管理系统,由于其体积小、速度快、总体拥有成本低,并且之前是完全开源,所以大受欢迎。但由于后面MySQL卖给了SUN,随后SUN被Oracle收购,虽然也有开源免费版本,但是很多功能都需要另外购买商业版本,导致现在MySQL使用份额逐渐减少。所…

    Linux干货 2015-10-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-05 16:54

    写的很好,排版也很棒,加油