马哥教育网络班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)
SnooSnoo
上一篇 2016-08-05
下一篇 2016-08-05

相关推荐

  • 马哥教育网络班22期+第一周课程练习

    1、描述计算机的组成及其功能。 计算机由硬件系统、操作系统和应用软件所组成,没有安装任何软件的计算机称为裸机。 1.1、硬件系统由运算器、控制器、存储器、输入设备和输出设备五大部分组成,遵循冯、诺依曼原理。 控制器:读取分析指令,向其它部分发出控制信号,保证计算机按照预先规定的目标和步骤有条不紊地进行操作及处理。  运算器:对数据进行各种运算,例如…

    Linux干货 2016-08-22
  • select和case用法

    一、作业 1、斐波那契数列又称黄金分割数列,因数学家列昂纳多·斐波那契以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2) 写一个函数,求n阶斐波那契数列 2、汉诺塔(又称河内塔)问…

    Linux干货 2016-08-21
  • Linux的磁盘管理:

    Linux的磁盘管理: Linux的核心:一切介文件:     open,read,write,close 块设备:block,存取单位‘块’,磁盘 字符设备:char 存取单位‘字符’,键盘 设备文件:关联至一个设备驱动程序,进而能够跟与之对应硬件设备进行通信: 设备号码:   &nbs…

    Linux干货 2016-07-22
  • 任务计划使用方法

    概述     任务计划其实就是针对未来的某一刻或者是某一周期内设置要执行的工作;     任务计划分为两种:         1、一次性任务:在指定的未来的某个时间点仅执行一次任务;  &n…

    Linux干货 2015-03-26
  • HAProxy

    LB Cluster: 四层:lvs, nginx(stream),haproxy(mode tcp) 七层:http: nginx(http, ngx_http_upstream_module), haproxy(mode http), httpd, ats, perlbal, pound… HAProxy 程序环境: 主程序:/usr/sbi…

    Linux干货 2017-07-03
  • 逻辑卷

    一、作业 1、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项 2、写一个脚本,完成如下功能: (1) 列出当前系统识别到的所有磁盘设备 (2) 如磁盘数量为1,则显示其空间使用信息 否则,则显示最后一个磁盘上的空间使用信息 3、创建一个可用…

    Linux干货 2016-08-30

评论列表(1条)

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

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