马哥教育网络班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)
SnooSnoo
上一篇 2016-07-27 09:27
下一篇 2016-07-27 09:31

相关推荐

  • Linux学习总结&day07-正则表达式&文本处理工具

    第一部分、正则表达式     1、什么是正则表达式     正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的辅助,可以让用户轻易的达到查找、删除、替换某特定字符串的处理程序。     正则表…

    Linux干货 2016-08-08
  • N26-博客作业-week5

    1、显示当前系统上root、fedora或user1用户的默认shell; ~]# grep -E “^((root|fedora|user1)\>)” /etc/passwd | cut -d: -f7 2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(); ~]#…

    Linux干货 2017-03-05
  • dns主从

    一、前言 Dns全称domain name system,当我们访问一个网站时,在网站输入一个网址。但是网络是靠ip地址这个逻辑地址来标识地址的。而一个网址是如何转换为ip地址的?下面我们将简单讲解下dns的原理。 二、dns查询过程 在了解dns查询过程时,我们先了解一些有关dns的专业名词     根域:用来管理互…

    Linux干货 2015-10-01
  • grep文本查找和正则表达式

    一、grep grep: (Global search REgular expression and Print outthe line)全局查找正则表达式并且打印结果行。不会对输入的文本进行修改。 Unix的grep家族包括grep、egrep和fgrep,egrep是grep的扩展,支持更多的正则元字符; fgrep就是fixed grep或f…

    Linux干货 2016-08-07
  • shell– 脚本编程

    编程基础 脚本基本格式 变量 运算 条件测试 流程控制 函数 数组 高级字符串操作 高级变量 配置用户环境 编程基础 程序:指令+数据 程序编程风格:     过程式:以指令为中心,数据服务于指令     对象式:以数据为中心,指令服务于数据 shell程序:提供了编程能力,解释…

    Linux干货 2016-08-21
  • puppet学习笔记

      一、Puppet基础原理: Puppet是一款使用GPLV2X协议授权的开源管理配置工具,用ruby语言开发,既可以通过客户端—服务器的方式运行,也可以独立运行。puppet可以为系统管理员提供方便,快捷的系统自动化管理。   二、puppet工作流程 1. 客户端 puppet-client 向 puppet-maste…

    Linux干货 2015-11-04

评论列表(2条)

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

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

    • Snoo
      Snoo 2016-07-27 15:56

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