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

请详细总结vim编辑器的使用并完成以下练习题

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;

[root@centos ~]# cp /etc/rc.d/rc.sysinit /tmp/
[root@centos ~]# vim /tmp/rc.sysinit 
:%s@^[[:space:]]\+@#&@g
[root@centos ~]# cat /tmp/rc.sysinit | less
#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
#    . /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
#    HOSTNAME=localhost
fi
if [ ! -e /proc/mounts ]; then
#       mount -n -t proc /proc /proc
#       mount -n -t sysfs /sys /sys >/dev/null 2>&1

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

[root@centos ~]# cp /boot/grub/grub.conf /tmp/
:%s@^[[:space:]]\+@@g
[root@centos ~]# cat /tmp/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_centos-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-504.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-504.el6.x86_64.img

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符;

:%s@^#[[:space:]]\+@@g
[root@centos ~]# less /tmp/rc.sysinit 
#!/bin/bash
#
/etc/rc.d/rc.sysinit - run once at boot time
#
Taken in part from Miquel van Smoorenburg's bcheckrc.
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
HOSTNAME=localhost
fi
if [ ! -e /proc/mounts ]; then
mount -n -t proc /proc /proc
mount -n -t sysfs /sys /sys >/dev/null 2>&1
fi
if [ ! -d /proc/bus/usb ]; then

4、为/tmp/grub.conf文件中前三行的行首加#号;

:1,3s@.*@#&@g

[root@centos ~]# cat /tmp/grub.conf 
## grub.conf generated by anaconda
##
## Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)

5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;

:%s@=[0-1]@=1@g 
[root@centos ~]# cat /etc/yum.repos.d/CentOS-Media.repo | grep "="
#  yum --enablerepo=c6-media [command]
#  yum --disablerepo=\* --enablerepo=c6-media [command]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202

[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
[root@centos ~]# cat backup.sh 
#!/bin/bash
if [ -e /backup ]; then 
cp -r /etc /backup/etc-%Y%m%d%H%M
else
mkdir /backup && cp -r /etc /backup/etc-%Y%m%%d%H%M
fi
exit 0

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20150402;

[root@centos ~]# chmod +x backup1.sh 
[root@centos ~]# crontab -u root -e
crontab: installing new crontab
[root@centos ~]# bash -n backup1.sh 
[root@centos ~]# bash -x backup1.sh 
+ '[' -e /backup/messages_logs ']'
+ mkdir -p /backup/messages_logs
+ cp /var/log/messages /backup/messages_logs/messages-%Y%m%d
+ exit 0
[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
0 0 * * 2,4,6 /root/backup1.sh
[root@centos ~]# cat backup1.sh 
#!/bin/bash
if [ -e /backup/messages_logs ]; then 
cp /var/log/messages  /backup/messages_logs/messages-%Y%m%d
else
mkdir -p /backup/messages_logs && cp /var/log/messages  /backup/messages_logs/messages-%Y%m%d
fi
exit 0

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中;

[root@centos ~]# bash -n meminfo.sh 
[root@centos ~]# bash -x meminfo.sh 
+ '[-e' /status ']'
meminfo.sh: line 2: [-e: command not found
+ mkdir /status
+ grep '^S' /proc/meminfo
+ exit 0
[root@centos ~]# cat meminfo.sh 
#!/bin/bash
if [-e /status ];then
grep "^S" /proc/meminfo >>/status/memory.txt
else
mkdir /status && grep "^S" /proc/meminfo >>/status/memory.txt
fi
exit 0
[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
0 0 * * 2,4,6 /root/backup1.sh
0 */2 * * * /root/meminfo.sh

9、工作日的工作时间内,每两小时执行一次echo "howdy";

[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
0 0 * * 2,4,6 /root/backup1.sh
0 */2 * * * /root/meminfo.sh
0 9-18/2 * * 1,2,3,4,5 /root/echo.sh

脚本编程练习

10、创建目录/tmp/testdir-当前日期时间;

[root@centos test]# bash -x exercise1.sh 
++ date +%F-%T
+ mkdir /tmp/testdir-2016-07-23-20:31:14
++ date +%F-%T
+ echo '/tmp/testdir-2016-07-23-20:31:14 finished! '
/tmp/testdir-2016-07-23-20:31:14 finished! 
[root@centos test]# cat excercise1.sh 
#!/bin/bash
mkdir /tmp/testdir-$(date +%F-%T) && echo "/tmp/testdir-$(date +%F-%T) finished! "

11、在此目录创建100个空文件:file1-file100;

[root@centos test]# bash -n exercise2.sh 
[root@centos test]# bash -x exercise2.sh 
+ for i in '{1..100}'
+ touch file1
+ for i in '{1..100}'
+ touch file2
+ for i in '{1..100}'
+ touch file3
+ for i in '{1..100}'
[root@centos test]# cat excersice2.sh 
#!/bin/bash
for i in {1..100};do
touch file$i
done
exit 0

12、显示/etc/passwd文件中位于第偶数行的用户的用户名;

[root@centos test]# bash -x  exercise3.sh
+ cut -d: -f2
+ awk '{if($1%2!=0) next ;printf"%s:%s\n", $1, $2}'
+ cat -n /etc/passwd
bin
adm
sync
halt
uucp
games
ftp
dbus
rpc
rtkit
avahi-autoipd
postfix
nfsnobody
gdm
apache
sshd
derulo
testbash
nologin
user1
[root@centos test]# cat excercise3.sh 
#!/bin/bash
cat -n /etc/passwd | awk  '{if($1%2!=0) next ;printf"%s:%s\n", $1, $2}' | cut -d: -f2

13、创建10用户user10-user19;密码同用户名;

[root@centos test]# bash -n exercise4.sh 
[root@centos test]# bash -x exercise4.sh 
+ for i in '{10..19}'
+ useradd user10
+ passwd --stdin user10
+ echo user9
Changing password for user user10.
passwd: all authentication tokens updated successfully.
+ echo 'user10 added!'
user9 added!
+ for i in '{10..19}'
+ useradd user11
+ passwd --stdin user11
+ echo user11
Changing password for user user11.
[root@centos test]# cat exercise4.sh 
#!/bin/bash
for i in {10..19}; do
useradd user$i && echo "user$i" | passwd --stdin user$i
echo "user$i added!"
done

14、在/tmp/创建10个空文件file10-file19;

[root@centos test]# bash -n  exercise5.sh 
[root@centos test]# bash -x  exercise5.sh 
+ for i in '{10..19}'
+ touch /tmp/file10
+ echo 'file /tmp/file10 added!'
file /tmp/file10 added!
+ for i in '{10..19}'
+ touch /tmp/file11
+ echo 'file /tmp/file11 added!'
[root@centos test]# cat exercise5.sh 
#!/bin/bash
for i in {10..19}; do
touch /tmp/file$i
echo "file /tmp/file$i added!"
done

15、把file10的属主和属组改为user10,依次类推。

[root@centos test]# bash -n  exercise6.sh
[root@centos test]# bash -x  exercise6.sh
+ for i in '{10..19}'
+ chown user10:user10 /tmp/file10
+ echo 'file /tmp/file10  user and group have changed!!'
file /tmp/file10  user and group have changed!!
+ for i in '{10..19}'
+ chown user11:user11 /tmp/file11
+ echo 'file /tmp/file11  user and group have changed!!'
file /tmp/file11  user and group have changed!!
[root@centos test]# cat exercise6.sh 
#!/bin/bash
for i in {10..19}; do
chown user$i:user$i /tmp/file$i
echo "file /tmp/file$i  user and group have changed!!"
done
[root@centos test]# ls -lh /tmp/file*
-rw-r--r--. 1 user10 user10 0 Jul 24 11:03 /tmp/file10
-rw-r--r--. 1 user11 user11 0 Jul 24 11:03 /tmp/file11
-rw-r--r--. 1 user12 user12 0 Jul 24 11:03 /tmp/file12
-rw-r--r--. 1 user13 user13 0 Jul 24 11:03 /tmp/file13
-rw-r--r--. 1 user14 user14 0 Jul 24 11:03 /tmp/file14
-rw-r--r--. 1 user15 user15 0 Jul 24 11:03 /tmp/file15
-rw-r--r--. 1 user16 user16 0 Jul 24 11:03 /tmp/file16
-rw-r--r--. 1 user17 user17 0 Jul 24 11:03 /tmp/file17
-rw-r--r--. 1 user18 user18 0 Jul 24 11:03 /tmp/file18
-rw-r--r--. 1 user19 user19 0 Jul 24 11:03 /tmp/file19

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

(0)
上一篇 2016-07-27 09:27
下一篇 2016-07-27 09:31

相关推荐

  • Linux常用命令使用方法

    下面以一些常用的Linux命令为例讲解一下Linux命令的使用方法。 1.  ifconfig – 配置网络接口 | 对象 | 参数 | 含义 || —— | —— | —— |网卡名 | / | 查看指定网卡IP的地址,不指定默认查看所有网卡地址网卡名 | up …

    Linux干货 2017-09-01
  • 今天把/boot/目录下的文件删除了只好重新安装一下内核和grub

    一前言         今天手贱把/boot/目录下的文件删了  系统重启之后无法登陆系统 无奈只好重装一下内核和grub 二准备工作           Centos7 系统光盘一张 当然我这是虚拟…

    2017-03-06
  • Linux-Centos7编译内核

    编译内核     前提:         (1)准备好开发环境         (2) 获取目标主机上硬件设备的相关信息         (3) 获取目标主机系统功能的相关信息       …

    2017-07-16
  • 在Centos系统上安装EPEL扩展源以及安装htop工具

        Htop是一个强大的进程管理前端工具,但这是一个扩展工具,一般在Centos系统源中并没有,所有我们需要到fedora-epel源中下载。         EPEL即Extra Packages for Enterprise Lin…

    Linux干货 2016-02-14
  • httpd功能配置之https

       HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。HTTPS使用443端口进行通信。    …

    Linux干货 2016-03-13
  • 路由配置

      RA和RB为linux系统,此处虚拟成为路由器,在每一台路由器的接口上设置好ip之后开始配置路由表 ip地址配置如下: RA:eth1:192.168.100.1/24    eth0:10.1.0.1/8       主机A:10.1.0.25/8 RB: eth1:192.168.100.2…

    Linux干货 2016-09-07

评论列表(2条)

  • 马哥教育
    马哥教育 2016-07-27 11:12

    写的很好,排版也很棒,第8题的脚本是不是错了?加油

    • Snoo
      Snoo 2016-07-27 15:56

      @马哥教育目测少了个空格,bash -n 不严谨啊,,