马哥教育网络班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
下一篇 2016-07-27

相关推荐

  • vim编辑器

      Linux文本编辑器vim     Linux下的编辑器最常用的就是vim或者vi文本编辑。vi和vim编辑器的区别是vim是vi的改进版本,在vi编辑器的基础上上扩展了很多实用的功能。 vim的使用 用vim打开文件:     vim [options] [file ..]…

    Linux干货 2016-12-04
  • DNS简单概念 一

    DNS简单概念 一 DNS简单概念 一 1 §·主机角色 1 §·DNS服务器的类型: 1 §·DNS中简单术语 2 §·DNS中资源记录及其类型 2 §·DNS域与区域 4 §·BIND安全设置 5 §·BIND高级设置BIND VIEW 6   §·主机角色 在网络上不同的主机有不同的角色, 比如: DNS解析角色,邮件角色,网页服务器 §·D…

    Linux干货 2016-09-26
  • Bash Shell中的for循环和运算表达式应用

    Bash Shell中的for循环和运算表达式应用 1、写一个脚本 实现以下功能: 接受一个以上文件路径作为参数, 显示每个文件拥有的行数,总结说明本次共为几个文件统计了其行数。设定此脚本至少需要一个参数并给出提示,$#表示参数的个数。将脚本提供的所有参数组成一个列表放入for语句依次进行循环执行echo "$i has $(wc -l $i | …

    Linux干货 2016-12-13
  • Linux basics–part2

    一、Linux上的文件管理类命令,及常用的使用方法 1.ls:list, 列出指定目录下的内容 常用选项: ls: list, 列出指定目录下的内容 ls [OPTION]… [FILE]… -a: 显示所有文件,包括隐藏文件; -A:显示除.和..之外的所有文件; -l: –long, 长格式列表,即显示文件的详细属性信…

    Linux干货 2017-07-14
  • Linux 的shell脚本编程

    shell脚本编程 程序:指令+数据 程序编辑风格:             过程式:以指令为中心,数据服务于指令             对象式:以数据为中心,指令服务于数据 shell程序:提供了编程能力,解释执行 计算机:运行二进制指…

    Linux干货 2016-08-15
  • 【原创】RHEL7-PPTP-VPN-Server排错

    第一次写博客,明显不知道如何下笔。     昨天6月21日,突然发现往日运行一切正常的pptpvpn服务器怎么也连不上了,错误代码是619。这个错误代码以前并没有见过,于是上google查了一下资料,据说有几种可能: 1,路由器或防火墙干掉了tcp1723; 2,电脑协议栈问题; 3,拨号连接的认证选项有问题; &nb…

    Linux干货 2016-06-23

评论列表(2条)

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

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

    • Snoo
      Snoo 2016-07-27 15:56

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