马哥Linux第六周课程作业

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

[[email protected] ~]# cp -a /etc/rc.d/rc.sysinit /tmp  #复制文件到/tmp下
[[email protected] ~]# vim /tmp/rc.sysinit  #使用vim打开文件,提示vim命令找不到
-bash: vim: command not found
[[email protected] ~]# yum -y install vim  #用yum安装vim程序,完成后,再次使用vim打开
[[email protected] ~]# vim /tmp/rc.sysinit
#打开vim编辑器后,使用:进入末行模式,输入以下命令进行查找替换
:%s/\(^[[:space:]]\+\)/#\1/g

另外,命令也可写成
:%s/^[[:space:]]\+/#&/g

拓展思路:

如何用查找替换命令恢复已经保存的加了#号的内容?原理相同,只是查找命令范围更换即可,如下:

:%s/^#\([[:space:]]\+\)/\1/g

#/etc/rc.d/rc.sysinit只能在CentOS6上找到,CentOS7上面是没有的,因为CentOS7采用systemd取代CentOS6是SysV init启动方式,原因在于SysV init服务启动慢。

先了解一下什么是rc.sysinit

  rc.sysinit作用是执行一些重要的系统初始化任务。

在设定了运行等级后,Linux系统执行的第一个用户层文件就是/etc/rc.d/rc.sysinit脚本程序,它做的工作非常多,包括设定PATH、设定网络配置(/etc/sysconfig/network)、启动swap分区、设定/proc等等。

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

[[email protected] ~]# cp -a /boot/grub/grub.conf /tmp  #复制文件
[[email protected] ~]# vim /tmp/grub.conf       #使用vim打开编辑文件
# 打开编辑器后按:进入末行模式,输入以下命令进行查找替换
:%s/^[[:space:]]\+//g

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

[[email protected] ~]# vim /tmp/rc.sysinit  #使用vim打开编辑文件
# 打开编辑器后按:进入末行模式,输入以下命令进行查找替换
:%s/^#[[:space:]]\+//g

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

同上,打开vim编辑进入末行模式后输入以下命令:
:1,3s/*/#&/g
# vim编辑器末行模式,地址定界说明:
        :start_pos,end_pos
           #                 具体第#行,例如2表示第2行
           #,#               从左侧#表示行开始,到右侧#表示行结尾
           #,+#              从左侧#表示的行起始,加上右侧#表示的行数                
           .                 当前行
           $                 最后一行                    
           %                 全文,相当于1,$                
           /pat1/,/pat2/     从第一次被pat1模式匹配到的行开始,一直至第一次被pat2匹配到的行结尾
           #,/pat/           从第#行开始,到被pat模式匹配到的行         
           /pat/,$           从被pat模式匹配到的行开始,到最后一行。

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

进入vim末行编辑模式,分两次修改,先修改enabled=0,再修改gpgchechk=0的值
:%[email protected][email protected][email protected]
:%[email protected][email protected][email protected]

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

[[email protected] ~]# crontab -e
0 */4 * * * /usr/bin/cp -r /etc/ /backup/etc-`date +%Y%m%d%H%M`
时间表示法:
	(1) 特定值;
	        给定时间点有效取值范围内的值;
	(2) *
		给定时间点上有效取值范围内的所有值;表示“每...”;
	(3) 离散取值:,
	        #,#,#
	(4) 连续取值:-
	        #-#
	(5) 在指定时间范围上,定义步长:
	        /#: #即为步长

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

# 创建文件夹:
[[email protected] ~]# mkdir -pv /backup/messages_logs
mkdir: created directory ‘/backup’
mkdir: created directory ‘/backup/messages_logs’

# 使用crontab命令创建任务,cp命令进行备份
[[email protected] ~]# crontab -e
0 0 * * 2,4,6 cp /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`

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

# 创建所需的文件夹及文件
[[email protected] ~]# mkdir  /stats/
[[email protected] ~]# touch  /stats/memory.txt
[[email protected] ~]# crontab -e
0 */2 * * * /usr/bin/egrep ‘^S‘ /proc/meminfo >> /stats/memory.txt

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

[[email protected] ~]# crontab -e
0 */2 * * 1-5 /usr/bin/echo "howdy"

脚本编程练习

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

# 编辑脚本,并保存
[[email protected] tmp]# vim datedir.sh

#!/bin/bash
mkdir -pv /tmp/testdir-$(date +%Y%m%d%H%M)
# 修改脚本权限为可执行
[[email protected] tmp]# chmod o+x datedir.sh
# 执行脚本创建目录
[[email protected] tmp]# ./datedir.sh
mkdir: created directory ‘/tmp/testdir-201609270041’
验证脚本结果:
[[email protected] tmp]# ls -l
drwxr-xr-x. 2 root root    6 Sep 27 00:41 testdir-201609270041

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

# 编辑脚本保存
[[email protected] tmp]# vim create_empty_file.sh
#!/bin/bash
#create_empty_file
for i in {1..100}; do
        touch /tmp/testdir-201609270041/file$i
 done
 
# 授予执行权限:
[[email protected] tmp]# chmod o+x create_empty_file.sh
# 运行脚本
[[email protected] tmp]# ./create_empty_file.sh 

# 查看脚本运行结果 
[[email protected] tmp]# ls ./testdir-201609270041/    
file1    file18  file27  file36  file45  file54  file63  file72  file81  file90
file10   file19  file28  file37  file46  file55  file64  file73  file82  file91
file100  file2   file29  file38  file47  file56  file65  file74  file83  file92
file11   file20  file3   file39  file48  file57  file66  file75  file84  file93
file12   file21  file30  file4   file49  file58  file67  file76  file85  file94
file13   file22  file31  file40  file5   file59  file68  file77  file86  file95
file14   file23  file32  file41  file50  file6   file69  file78  file87  file96
file15   file24  file33  file42  file51  file60  file7   file79  file88  file97
file16   file25  file34  file43  file52  file61  file70  file8   file89  file98
file17   file26  file35  file44  file53  file62  file71  file80  file9   file99

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

# 创建脚本并保存
[[email protected] tmp]# vim display_username.sh
#!/bin/bash
# 显示/etc/passwd文件中位于偶数行的用户的用户名
sed -n ‘n;p‘ /etc/passwd |cut -d: -f1
# 对脚本授予执行权限x并运行
[[email protected] tmp]# chmod o+x display_username.sh 
[[email protected] tmp]# ./display_username.sh 
bin
adm
sync
halt
operator
ftp
avahi-autoipd
systemd-network
polkitd
tss
sshd
centos
acc
archlinux

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

# 创建脚本
[[email protected] tmp]# vim adduser.sh
#!/bin/bash
# create user10-user19,these passwd the same as username
for i in {10..19};do
        useradd user$i
        echo "user$i" | passwd--stdin user$i
done
# 授予脚本执行权限
[[email protected] tmp]# chmod o+x adduser.sh

# 运行脚本
[[email protected] tmp]# ./adduser.sh
Changing password for user user10.
passwd: all authentication tokens updated successfully.
Changing password for user user11.
passwd: all authentication tokens updated successfully.
Changing password for user user12.
passwd: all authentication tokens updated successfully.
Changing password for user user13.
passwd: all authentication tokens updated successfully.
Changing password for user user14.
passwd: all authentication tokens updated successfully.
Changing password for user user15.
passwd: all authentication tokens updated successfully.
Changing password for user user16.
passwd: all authentication tokens updated successfully.
Changing password for user user17.
passwd: all authentication tokens updated successfully.
Changing password for user user18.
passwd: all authentication tokens updated successfully.
Changing password for user user19.
passwd: all authentication tokens updated successfully.

# 验证结果方式,使用新创建的user10-19进行登录,测试密码是否与用户名一致

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

# 创建脚本
[[email protected] tmp]# vim createfile.sh
#!/bin/bash
#create empty file10-file19

for i in {10..19};do
        touch /tmp/file$i
done

#对脚本授执行权限x
[[email protected] tmp]# chmod o+x createfile.sh

#执行并显示结果
[[email protected] tmp]# ./createfile.sh
[[email protected] tmp]# ls
adduser.sh            display_username.sh  file13  file17  fstab.2
create_empty_file.sh  file10               file14  file18  meminfo.txt
createfile.sh         file11               file15  file19  profile
datedir.sh            file12               file16  fstab   testdir-201609270041

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

# 创建脚本
[[email protected] tmp]# vim chown.sh
#!/bin/bash
for i in {10..19};do
        chown user$i:user$i /tmp/file$i
done

# 执行脚本命令
[[email protected] tmp]# chmod o+x chown.sh
[[email protected] tmp]# ./chown.sh
[[email protected] tmp]# ll
...(中间省略)...
-rw-r--r--. 1 user10 user10    0 Sep 28 00:22 file10
-rw-r--r--. 1 user11 user11    0 Sep 28 00:22 file11
-rw-r--r--. 1 user12 user12    0 Sep 28 00:22 file12
-rw-r--r--. 1 user13 user13    0 Sep 28 00:22 file13
-rw-r--r--. 1 user14 user14    0 Sep 28 00:22 file14
-rw-r--r--. 1 user15 user15    0 Sep 28 00:22 file15
-rw-r--r--. 1 user16 user16    0 Sep 28 00:22 file16
-rw-r--r--. 1 user17 user17    0 Sep 28 00:22 file17
-rw-r--r--. 1 user18 user18    0 Sep 28 00:22 file18
-rw-r--r--. 1 user19 user19    0 Sep 28 00:22 file19
drwxr-xr-x. 2 root   root   4096 Sep 27 23:35 testdir-201609270041
时间: 2024-07-28 19:17:10

马哥Linux第六周课程作业的相关文章

马哥Linux第七周课程作业

1.创建一个10G分区,并格式为ext4文件系统: (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl: # 创建10G分区 [[email protected] ~]# fdisk /dev/sdb 命令(输入 m 获取帮助):n   Partition type:    p   primary (0 primary, 0 extended, 4 free)    e   extended Select (default p): p 分

马哥Linux培训第二周课程作业

1.   Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. #    文件及目录列表查看:ls #    文件内容查看:cat, tac #    修改文件时间戳或创建新文件:touch #    文件编辑: vi, nano 文件管理:cp, mv, rm, (mkdir, rmdir:创建.删除目录) (1)     .复制命令:cp 格式: cp[OPTION]... [-T] SOURCE DEST cp[OPTION]... SOURCE... DIRECTOR

马哥linux第六周作业

1.复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#: [[email protected] tmp]# cp /etc/rc.d/rc.sysinit . [[email protected] tmp]# vim rc.sysinit   :% s/^[[:space:]]/#&/             #按Esc进入vim的末行模式,并输入 2.复制/boot/grub/grub.conf至/tmp

马哥linux 培训第二周作业

注意:第二周作业,请将以下题目整理在51cto博客当中,完成后请将对应的博文链接地址提交在答案栏中,提交格式如下:学号+姓名+博文链接地址eg:1+张三+http://mageedu.blog.51cto.com/4265610/1794420 本周作业内容:1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. 文件管理的命令有cp.mv.rm 复制命令:cp 用法: cp [-adfilprsu] 来源文件(source) 目标文件(destination) cp [o

马哥linux第三周作业---基本班

1.列出当前系统上所有已经登陆的用户的用户名,注意:同一个用户登陆多次,则显示一次即可. who |cut -d' ' -f1 | sort | uniq 2.取出最后登陆到当前系统的用户的相关信息 w|tail-n1或者who|tail-n1 3.取出当前系统上被用户当做其默认shell的最多的那个shell cat /etc/passwd | awk -F':' '{print $7}'|uniq –c 结果为:/sbin/nologin awk -F':' '{a[$7]++}END{fo

马哥Linux第五周作业

1.显示当前系统上root.fedora或user1用户的默认shell: PS:第一眼看到问题可能会有点头疼,那就把问题拆分完成,组合多个简单命令完成复杂工作 第一步,查找到这些用户并显示: 使用|或衔接多个过滤条件: [[email protected] ~]# grep -E "^root\>|^fedora\>|^user1\>" /etc/passwd   #grep -E也可使用egrep root:x:0:0:root:/root:/bin/bash u

马哥linux第七周作业

1.创建一个10G分区,并格式为ext4文件系统: (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl: (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳: [[email protected] tmp]# fdisk /dev/sda          WARNING: DOS-compatible mode is deprecated. It's strongly recommended

马哥linux第八周作业

1.请描述网桥.集线器.二层交换机.三层交换机.路由器的功能.使用场景与区别. 集线器:物理层设备,多端口,无法隔离冲突域,用于连接主机. 将多个网络连接起来,使之能够互访的设备叫网桥. 多端口网桥称为交换机 二层交换机:数据链路层设备,多端口,与网桥相比,交换机可识别MAC地址,根据MAC地址转发数据,可隔离冲突域,用于在路由设备与主机之间架设,接入层与汇聚层皆可使用. 三层交换机:相当于是带路由功能的二层交换机,工作在网络层,有更高的带宽,可做核心层使用,用于大中型网络的路由交换. 路由器:

马哥linux第8周作业

1.请描述网桥.集线器.二层交换机.三层交换机.路由器的功能.使用场景与区别. 网桥:   连接不同子网,使其透明通信,它们工作在链路层.它们处理的是链路层数据,一般来说就是以太   网帧格式的  数据,缺点是无法避免"广播风暴",交换机就是网桥的集合. 集线器:     集线器的基本功能是信息分发,它把一个端口接收的所有信号向所有端口分发出去.一些集线器 在分发之前将弱信号重新生成,一些集线器整理信号的时序以提供所有端口间的同步数据通信, 工作在物理层,集线器只是把各个终端互相连接起