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

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

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

[root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/
[root@localhost ~]# vim /tmp/rc.sysinit
:%s/^[[:space:]]\+/#/

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

[root@localhost ~]# cp /boot/grub/grub.conf /tmp/
[root@localhost ~]# vim /tmp/grub.conf 
:%s/^[[:space:]]//

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

[root@localhost ~]# vim /tmp/rc.sysinit 
:%s/^#[[:space:]]\+//

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

[root@localhost ~]# vim /tmp/grub.conf 
:1,3s/^/#/

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

[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Media.repo
:%s/enabled=0/enabled=1
:%s/gpgcheck=0/gpcheck=1

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

[root@localhost ~]# mkdir /backup
[root@localhost ~]# echo 'tar cf /backup/etc-$(date +%Y%m%d%H%M) /etc/*' > /root/back.sh 
[root@localhost ~]# crontab -e
0 */4  * * * bash /root/back.sh &>/dev/null

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

[root@localhost ~]# mkdir /backup/messages_logs
[root@localhost ~]# echo 'tar cf /backup/messages_logs/messages-$(date +%Y%m%d)' > /root/back.sh 
[root@localhost ~]# crontab -e
* *  * * 2,4,6 bash /root/back.sh &>/dev/null

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

[root@localhost ~]# echo 'grep ^S /proc/meminfo >> /stats/memory.txt ' > /root/back.sh 
[root@localhost ~]# crontab -e
0 */2 * * * bash /root/back.sh &>/dev/null

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

[root@localhost ~]# crontab -e
0 11,13,15,17 * * 1-5 echo "howdy"

脚本编程练习

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

[root@localhost ~]# cat test10.sh 
#!/bin/bash
mkdir  /tmp/testdir-$(date +%Y%m%d-%H%M%S)

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

[root@localhost testdir-20160721-201246]# cat test11.sh 
#!/bin/bash
for i in {1..100};
do
        touch file$i
done
[root@localhost testdir-20160721-201246]# ls
file1    file2   file30  file41  file52  file63  file74  file85  file96
file10   file20  file31  file42  file53  file64  file75  file86  file97
file100  file21  file32  file43  file54  file65  file76  file87  file98
file11   file22  file33  file44  file55  file66  file77  file88  file99
file12   file23  file34  file45  file56  file67  file78  file89  test11.sh
file13   file24  file35  file46  file57  file68  file79  file9
file14   file25  file36  file47  file58  file69  file8   file90
file15   file26  file37  file48  file59  file7   file80  file91
file16   file27  file38  file49  file6   file70  file81  file92
file17   file28  file39  file5   file60  file71  file82  file93
file18   file29  file4   file50  file61  file72  file83  file94
file19   file3   file40  file51  file62  file73  file84  file95

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

[root@localhost ~]# cat test12.sh 
#!/bin/bash
sed -n 'n;p' /etc/passwd | cut -d: -f1
[root@localhost ~]# bash test12.sh 
bin
adm
sync
halt
uucp
games
ftp
dbus
vcsa
rtkit
abrt
nfsnobody
gdm
apache
postfix
sshd
named

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

#!/bin/bash

for i in {10..19}
do
        id user$i &> /dev/null
        if [ $? -ne 0 ];then
                useradd user$i
                echo user$i:user$i | chpasswd
        else
                echo "user$i existed!"
        fi
done

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

[root@localhost tmp]# cat test14.sh 

#!/bin/bash
for i in {10..19}
do
        if [ -f file$i ];then
                echo "file $i existed!"
        else
                touch file$i
        fi
done

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

[root@localhost tmp]# cat test15.sh 
#!/bin/bash
for i in {10..19}
do
        chown user$i:user$i file$i
done

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

(0)
N21-天天N21-天天
上一篇 2016-08-02 10:58
下一篇 2016-08-02 10:58

相关推荐

  • centos7上实现corosync V2 + pacemaker + pcs | crmsh备忘录

    1、集群配置的前提: 一、时间同步(ntpdate做crontab定时任务) 二、主机名和IP相互解析 三、基于ssh密钥主机互信 #ssh-keygen -t rsa #ssh-copy-id -i   集群的全生命周期管理工具: pcs: agent(pcsd) crmsh: agentless (pssh)     2、安…

    2017-11-15
  • N25_第一周

    计算机组成 计算机通常由硬件和软件组成1.硬件 CPU CPU通常由控制器和运算器组成。控制器:是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算机各部分工作及内存与外设的访问等。运算器:是对数据进行各种算术运算和逻辑运算,即对数据进行加工处理。 存储 存储器的功能是存储程序、…

    Linux干货 2016-12-03
  • CentOS安装本地 YUM源 产生的问题和解决过程

    本人刚刚踏入Linux的神秘殿堂就被如何安装软件这样的问题难倒了,经过Google大神的指点,终于找到了如何配置YUM源的方法,其中也不是一帆风顺,也产生了一些小问题,这些都是基础知识,希望能够帮到刚刚学习Linux的朋友。     由于VMware的终端不好用,所以想换成XShell,过程中又需要配置网络属性,桥接到…

    Linux干货 2016-01-05
  • Liunx获取信息帮助与man文档章节的划分

    首先要判断命令的类型,可用 type COMMAND 来判断命令 如果显示结果为(….是 shell内嵌)即为shell内嵌命令,如需获取帮助,使用 help COMMAND 即可; 下图的例子是pwd是shell内嵌命令 内部命令属于Shell的一部分,所以并没有单独对应的系统文件,只要Shell解释器被运行,内部指…

    2017-07-02
  • linux基础学习第十天(文件查找与压缩)

    2016-08-12 授课内容: shell的流程控制 文件查找和压缩 shell的流程控制: 过程式编程语言: 顺序执行 选择执行 循环执行(未讲) 顺序执行: if语句: 单分支 if 判断条件; then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件为真的分支代码 else 条件为假的分支代码 fi 多分支 if CONDITIO…

    Linux干货 2016-08-15
  • 修改文件的权限

        linux中一切皆文件,文件有权限,所有者,所属组,大小等属性。文件所有者是指创建文件的用户,所属组是指创建文件的用户属于哪一个主要的组(用户的主组只能有一个)。     用户对文件进行各种操作的前提是有相应的权限,所以有些文件我们只能读,不能写,而有些文件既可以读写,还可以更改内容,下面就…

    2017-07-30

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-02 11:29

    写的很好,排版也很棒,加油,工作时间是从9点开始吧,在仔细看看第7个对吗?