首页 > 代码库 > 第六周作业
第六周作业
本周作业内容:
1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
答:
[root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@localhost ~]# sed -i ‘s/\(^[[:space:]]\)/#\1/g‘ /tmp/rc.sysinit
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
答:
[root@localhost ~]# cp /boot/grub/grub.conf /tmp/ [root@localhost ~]# sed -i ‘s/^[[:space:]]\+//g‘ /tmp/grub.conf [root@localhost ~]# 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/VolGroup-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-642.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img [root@localhost ~]#
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
答:
[root@localhost ~]# sed -i ‘s/^#[[:space:]]\+//g‘ /tmp/rc.sysinit
4、为/tmp/grub.conf文件中前三行的行首加#号;
答:
[root@localhost ~]# sed -i ‘1,3s/\(^.\)/#\1/g‘ /tmp/grub.conf [root@localhost ~]# 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/VolGroup-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-642.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img [root@localhost ~]#
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
答:
[root@localhost ~]# sed ‘s/enabled=0/enabled=1/g;s/gpgcheck=0/gpgcheck=1/g‘ /etc/yum.repos.d/CentOS-Media.repo # CentOS-Media.repo # # This repo can be used with mounted DVD media, verify the mount point for # CentOS-6. You can use this repo and yum to install items directly off the # DVD ISO that we release. # # To use this repo, put in your DVD and use it with the other repos too: # yum --enablerepo=c6-media [command] # # or for ONLY the media repo, do this: # # yum --disablerepo=\* --enablerepo=c6-media [command] [c6-media] name=CentOS-$releasever - Media baseurl=file:///media/CentOS/ file:///media/cdrom/ file:///media/cdrecorder/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [root@localhost ~]#
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
答:
[root@localhost ~]# mkdir /backup [root@localhost ~]# crontab -e 添加 */4 * * * * cp -rf /etc /backup/etc\-$(date +"%Y%m%d%H%M") 保存退出。
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
答:
[root@localhost ~]# mkdir -p /backup/messages_logs [root@localhost ~]# crontab -e 添加 * * * * 2,4,6 cp -rf /var/log/messages /backup/messages_logs/messages\-$(date +"%Y%m%d") 保存退出。
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
答:
[root@localhost ~]# mkdir /stats [root@localhost ~]# crontab -e 添加 1 */2 * * * grep "^[s,S]" /proc/meminfo >> /stats/memory.txt 保存退出。
9、工作日的工作时间内,每两小时执行一次echo "howdy"
答:
[root@localhost ~]# crontab -e 添加 * */2 * * 1-5 echo "howdy" 保存退出。
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
答:
#!/bin/bash date=`date +"%Y%m%d%H%M"` mkdir /tmp/testdir-$date
11、在此目录创建100个空文件:file1-file100
答:
#!/bin/bash for i in `seq 100` do touch /tmp/$date/file$i done
12、显示/etc/passw d文件中位于第偶数行的用户的用户名;
答:
sed -n "n;p" /etc/passwd
13、创建10用户user10-user19;密码同用户名;
答:
#!/bin/bash for i in {10..19};do useradd "user$i" echo "user$i" |passwd --stdin user$i done
14、在/tmp/创建10个空文件file10-file19;
答:
#!/bin/bash for i in {10..19};do mkdir /tmp/file$i done
15、把file10的属主和属组改为user10,依次类推。
答:
#!/bin/bash for i in {10..19};do chown "user$i" /tmp/file$i done
第六周作业