马哥教育网络班21期第六周作业

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

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

cp /etc/rc.d/rc.sysinit /tmp
vim /tmp/rc.sysinit
:%s/^[[:space:]]\+/#/g

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

cp /boot/grub/grub.conf /tmp
vim /tmp/grub.conf
:%s@^[[:space:]]\+@@g

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

:%s@^#[[:space:]]\+@@g

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

:1,3s/^/#/g

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

:%s/\<enabled=0\>/enabled=1/g
:%s/\<gpgcheck=0\>/gpgcheck=1/g

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

# mkdir /backup
# crontab -l
0 */4 * * * /bin/cp -a /etc/ /backup/etc-$(date +%Y%m%d%H%M) &>/dev/null

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

0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-$(date +%Y%m%d) &>/dev/null


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

0 */2 * * * /bin/egrep '^(S|s)' /proc/meminfo >> /stats/memory.txt &>/dev/null

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

0 8-18/2 * * 1-5 /bin/echo "howdy" &>/dev/null

脚本练习题

10、创建目录/tmp/testdir-当前日期时间;在此目录创建100个空文件:file1-file100

[root@rhel-6 test]# cat test1.sh
#!/bin/bash
TestDir=/tmp/testdir-$(date +%Y%m%d%H%M)
TestLog=${TestDir}/failure.log
mkdir ${TestDir} &>/dev/null
[[ $? -eq 0 ]] && echo "Create ${TestDir} success."
for (( i = 1; i < 101; i++ )); do
        touch ${TestDir}/file$i || echo "Failure:file$i" >> ${TestLog}
done
if [[ -s "${TestLog}" ]]; then
        cat ${TestLog}
else
        echo 'Create file1-file100 success.'
fi
[root@rhel-6 test]# sh test1.sh
Create /tmp/testdir-201608132309 success.
Create file1-file100 success.

12、显示/etc/passwd文件中谓语第偶数行的用户的用户名

[root@rhel-6 test]# cat test2.sh
#!/bin/bash
NO_passwd=$(cat /etc/passwd | wc -l)
echo 'The UserName list is:'
#for (( i = 0; i <= ${NO_passwd}; i+=2 )); do
for i in $(seq 2 2 ${NO_passwd});do
        head -n $i /etc/passwd | tail -n 1 | cut -d':' -f1
done
[root@rhel-6 test]# sh test2.sh
The UserName list is:
bin
adm
...
...

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

[root@rhel-6 test]# cat test3.sh
#!/bin/bash
for i in {10..19}; do
        id user$i &>/dev/null
        if [[ $? -eq 0 ]]; then
                echo "user$i exist."
                echo user$i | passwd --stdin user$i &>/dev/null
                [[ $? -eq 0 ]] && echo "user$i password change success." || echo "user$i password change failed."
        else
                useradd user$i
                echo "user$i" | passwd --stdin user$i &>/dev/null
                [[ $? -eq 0 ]] && echo "Create user$i success." || echo "Creat user$i failure."
        fi
done
[root@rhel-6 test]# useradd user13
[root@rhel-6 test]# sh test3.sh
Create user10 success.
...
user13 exist.
user13 password change success.
Create user14 success.
...

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

[root@rhel-6 test]# cat test4.sh#!/bin/bashTmpFile=/tmp/filefor i in {10..19}; do        if [[ -f "${TmpFile}$i" ]]; then                echo "${TmpFile}$i exist."                chown user$i:user$i ${TmpFile}$i && echo "${TmpFile}$i: owner and group change to user$i:user$i"        else                touch "${TmpFile}$i"                 if [[ $? -eq 0 ]]; then                        echo "${TmpFile}$i create success."                        chown "user$i:user$i" ${TmpFile}$i && echo "${TmpFile}$i: owner and group change to user$i:user$i"                else                        echo "${TmpFile}$i create failure."                fi        fi     done[root@rhel-6 test]# touch /tmp/file12[root@rhel-6 test]# sh test4.sh/tmp/file10 create success./tmp/file10: owner and group change to user10:user10/tmp/file11 create success./tmp/file11: owner and group change to user11:user11/tmp/file12 exist./tmp/file12: owner and group change to user12:user12......

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

(0)
N21-chenggbN21-chenggb
上一篇 2016-08-22
下一篇 2016-08-22

相关推荐

  • 简单shell脚本习题

    习题 作业 简单shell脚本习题 习题1 答案 习题2 答案 习题3 答案 习题4 答案 习题5 答案 习题6 答案 习题1 编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本, CPU型号,内存大小,硬盘大小。 答案 #!/bin/bash IPADDR=$(…

    Linux干货 2017-04-10
  • N25-第五周作业

    第五周 1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@zf ~]# grep -E "^[[:space:]]+[[:alnum:]]+" /boot/grub/grub.conf     &n…

    Linux干货 2016-12-26
  • 第一天参与马哥linux运维面授

    很高兴认识大家,介绍一下,我叫袁亚琼,来自美丽的云南。我是一个随和而又不随性95年女子,喜欢运动,擅长田径。学食品检测的,上学期间没怎么接触计算机,现在又来学专业计算机,内心还是有几丝恐慌,希望在今后的学习路上得到各位学友和老师的指导和帮助。等工作稳定,可以去云南放松一下,云南是个吃喝玩乐的好地方,同时也是个感受慢节奏生活的佳地。云南欢迎您哦! 选择学习li…

    Linux干货 2018-03-26
  • Linux发行版概述

    Linux发行版概述 Linux发行版有数百种之多,最主流的三个分支为Debain、Slackware、RedHat Debain Debain是三大主流发行版中唯一由社区维护的版本,无商业版本,相对较为轻巧,对使用者的技术要求较高 * Ubuntu、Knopix为Debian的主要子分支,其中Knopix是以安全著称的 Slackware(SUSE) SU…

    Linux干货 2017-07-02
  • N25期–第十五周作业

    1、 总结sed和awk的详细用法; 2、删除/boot/grub/grub.conf文件中所有行的行首的空白字符; # sed ‘s@^[[:space:]]\+@@’ /boot/grub/grub.conf 3、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字符; # sed ‘s@^#[[:space:]]*@@…

    Linux干货 2017-05-08
  • 重返课堂

    1.学习目的: i. IT业发展日新月异,当前所掌握的知识已无法满足工作需要。 ii. IT行业更新飞快,这就要求从业人员不断的去学习,不断的完善自己,才不至于被淘汰。 iii. 在这个大数据、云计算为王的时代,掌握Linux是入行“敲门砖”。 iv. 年龄已大,把握住人生不多的专门学习的机会,再博一次。 2.浅识Linux i. linux为何? Linu…

    2018-03-26

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-22 15:07

    写的很好,排版也很棒,加油