马哥linux第六周作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了马哥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目录中,删除/tmp/grub.conf文件中的行首的空白字符;
[[email protected] tmp]# cp /boot/grub/grub.conf . [[email protected] tmp]# vim grub.conf :% s#^[[:space:]]## #按Esc进入vim的末行模式,并输入
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
[[email protected] tmp]# vim rc.sysinit :% s/^[[:space:]]\+// #按Esc进入vim的末行模式,并输入
4、为/tmp/grub.conf文件中前三行的行首加#号;
[[email protected] tmp]# vim grub.conf :1,3 s/^#/#&/ #按Esc进入vim的末行模式,并输入 1 ## grub.conf generated by anaconda 2 ## 3 ## Note that you do not have to rerun grub after making changes to this file
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
[[email protected] tmp]# vim /etc/yum.repos.d/CentOS-Media.repo :/enabled=0/s/0/1/g :/gpgcheck=0/s/0/1/g #按Esc进入vim的末行模式,并输入
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
[[email protected] tmp]# mkdir /backup [[email protected] tmp]# crontab -e * */4 * * * cp -r /etc/ /backup/etc-`date +%Y%m%d%H%M` [[email protected] tmp]# /etc/init.d/crond start
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
[[email protected] tmp]# mkdir /backup/messages_logs [[email protected] tmp]# crontab -e * * * * */2,4,6 cp /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d` [[email protected] tmp]# /etc/init.d/crond restart
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
[[email protected] tmp]# mkdir -pv /stats/memory.txt [[email protected] tmp]# grep "^S" /proc/meminfo SwapCached: 0 kB SwapTotal: 2097148 kB SwapFree: 2097148 kB Shmem: 240 kB Slab: 73812 kB SReclaimable: 14712 kB SUnreclaim: 59100 kB [[email protected] tmp]# crontab -e * */2 */1 * * grep "^S" /proc/meminfo >> /stats/memory.txt [[email protected] tmp]# /etc/init.d/crond restart
9、工作日的工作时间内,每两小时执行一次echo "howdy"
[[email protected] tmp]# crontab -e * 9-18/2 * * */1-5 echo "howdy" [[email protected] tmp]# /etc/init.d/crond restart
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
[[email protected] tmp]# vim testdir.sh #!/bin/bash mkdir -pv /tmp/testdir-$(date +%Y%m%d%H%M) [[email protected] tmp]# bash -n testdir.sh [[email protected] tmp]# bash testdir.sh mkdir: created directory `/tmp/testdir-201608181859‘ [email protected] tmp]# cd /tmp/ [[email protected] tmp]# ls a.sh grub.conf testdir-201608181859 a.txt rc.sysinit testdir.sh
11、在此目录创建100个空文件:file1-file100
[[email protected] tmp]# vim file.sh #!/bin/bash for i in {1..100}; do mkdir -pv /tmp/testdir-201608181859/file$i done [[email protected] tmp]# bash -n file.sh [[email protected] tmp]# bash file.sh mkdir: created directory `/tmp/testdir-201608181859/file1‘ mkdir: created directory `/tmp/testdir-201608181859/file2‘ mkdir: created directory `/tmp/testdir-201608181859/file3‘ ....... mkdir: created directory `/tmp/testdir-201608181859/file98‘ mkdir: created directory `/tmp/testdir-201608181859/file99‘ mkdir: created directory `/tmp/testdir-201608181859/file100‘ [[email protected] tmp]# cd testdir-201608181859/ [[email protected] testdir-201608181859]# ls 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/passw d文件中位于第偶数行的用户的用户名;
[[email protected] tmp]# vim passwd.sh #!/bin/bash sum=$(wc -l /etc/passwd | cut -d‘ ‘ -f1) for i in $(seq 2 2 $sum); do head -n$i /etc/passwd | tail -n 1 | cut -d: -f1 done [[email protected] tmp]# bash -n passwd.sh [[email protected] tmp]# bash passwd.sh bin adm sync halt uucp games ftp vcsa postfix zengzy basher nologin slackware mysql hadoop1 fedora
13、创建10用户user10-user19;密码同用户名;
[[email protected] tmp]# vim user.sh #!/bin/bash if [ ! $UID -eq 0 ]; then echo "welcome root crate user" exit 1 fi for i in {10..19}; do if id user$i &> /dev/null ; then echo "user exists" else useradd user$i if [ $? -eq 0 ]; then echo "user$i" | passwd --stdin user$i &> /dev/null echo "New user:user$i add success" fi fi done [[email protected] tmp]# bash -n user.sh [[email protected] tmp]# su zengzy [[email protected] tmp]$ bash user.sh welcome root crate user [[email protected] tmp]$ exit [[email protected] tmp]# bash user.sh New user:user10 add success New user:user11 add success New user:user12 add success New user:user13 add success New user:user14 add success New user:user15 add success New user:user16 add success New user:user17 add success New user:user18 add success New user:user19 add success
14、在/tmp/创建10个空文件file10-file19;
[[email protected] tmp]# vim newfile.sh #!/bin/bash for i in {10..19}; do touch /tmp/file$i done [[email protected] tmp]# bash -n newfile.sh [[email protected] tmp]# bash newfile.sh [[email protected] tmp]# ls a.sh file12 file16 file.sh rc.sysinit testdir.sh a.txt file13 file17 grub.conf testdir-201608181852 user.sh file10 file14 file18 newfile.sh testdir-201608181857 file11 file15 file19 passwd.sh testdir-201608181859
15、把file10的属主和属组改为user10,依次类推。
[[email protected] tmp]# vim prmfile.sh #!/bin/bash for i in {10..19}; do chown user$i:user$i /tmp/file$i donemageedu tmp]# ll total 68 -rw-r--r--. 1 root root 0 Aug 18 19:49 file10 -rw-r--r--. 1 root root 0 Aug 18 19:49 file11 -rw-r--r--. 1 root root 0 Aug 18 19:49 file12 -rw-r--r--. 1 root root 0 Aug 18 19:49 file13 -rw-r--r--. 1 root root 0 Aug 18 19:49 file14 -rw-r--r--. 1 root root 0 Aug 18 19:49 file15 -rw-r--r--. 1 root root 0 Aug 18 19:49 file16 -rw-r--r--. 1 root root 0 Aug 18 19:49 file17 -rw-r--r--. 1 root root 0 Aug 18 19:49 file18 -rw-r--r--. 1 root root 0 Aug 18 19:49 file19 [[email protected] tmp]# bash -n prmfile.sh [[email protected] tmp]# bash prmfile.sh [[email protected] tmp]# ll total 68 -rw-r--r--. 1 user10 user10 0 Aug 18 19:49 file10 -rw-r--r--. 1 user11 user11 0 Aug 18 19:49 file11 -rw-r--r--. 1 user12 user12 0 Aug 18 19:49 file12 -rw-r--r--. 1 user13 user13 0 Aug 18 19:49 file13 -rw-r--r--. 1 user14 user14 0 Aug 18 19:49 file14 -rw-r--r--. 1 user15 user15 0 Aug 18 19:49 file15 -rw-r--r--. 1 user16 user16 0 Aug 18 19:49 file16 -rw-r--r--. 1 user17 user17 0 Aug 18 19:49 file17 -rw-r--r--. 1 user18 user18 0 Aug 18 19:49 file18 -rw-r--r--. 1 user19 user19 0 Aug 18 19:49 file19
本文出自 “给自己充电” 博客,请务必保留此出处http://zengzeyang.blog.51cto.com/6129531/1851682
以上是关于马哥linux第六周作业的主要内容,如果未能解决你的问题,请参考以下文章
马哥2016全新Linux+Python高端运维班第六周作业
马哥2016全新Linux+Python高端运维班第六周作业