Week six Homework

Source: Internet
Author: User

1. Copy the/etc/rc.d/rc.sysinit file to the/tmp directory and add # to the beginning of the line with at least one whitespace character in the/tmp/rc.sysinit file;

[Email protected] ~]# cp/etc/rc.d/rc.sysinit/tmp/| Sed-i '/^[[:space:]]\+/i\# '/tmp/rc.sysinit
[Email protected] ~]# cp/etc/rc.d/rc.sysinit/tmp/| Sed-i ' [email protected]^[[:space:]]\[email protected] ' # ' &@g '/tmp/rc.sysinit# the first question was added to the beginning of the SED, i,\n can be used to add multiple lines, The other is to use the alternate back reference


2, copy/boot/grub/grub.conf to/tmp directory, delete the blank character of the beginning of the/tmp/grub.conf file;

[Email protected] ~]# Cp-i/boot/grub/grub.conf/tmp/| Sed-i '/^[[:space:]]\+/d '/tmp/grub.conf
[Email protected] ~]# Cp-i/boot/grub/grub.conf/tmp/| Sed-i ' [email protected]^[[:space:]]\[email protected]@g '/tmp/grub.conf
#第二题用到了sed的删除d, and replace


3. Remove the # and white space characters from the/tmp/rc.sysinit file that begin with # and followed by at least one white-space character

[Email protected] ~]# sed-i '/^#[[:space:]]\+/d '/tmp/rc.sysinit
[Email protected] ~]# sed-i ' [email protected]^#[[:space:]]\[email protected]@g '/tmp/rc.sysinit
#第三题用到了sed的删除d, and replace


4. Add # to the beginning of the first three lines in the/tmp/grub.conf file

[Email protected] ~]# vim/tmp/grub.conf:1,3s/^/#/#在vim末行模式中输入


5. Change the last 0 of all enabled=0 or gpgcheck=0 in the/etc/yum.repos.d/centos-media.repo file to 1;

[Email protected] ~]# vim/etc/yum.repos.d/centos-media.repo:%[email protected][email protected][email protected]


6, every 4 hours to perform a backup of the/etc directory, back up to the/backup directory, save the directory named Shape etc-201608300202

[email protected] ~]# which CP #先查看cp命令的路径alias cp= ' cp-i '/bin/cp[[email protected] ~]# crontab-l #- e Edit,-l view edited commands below best use absolute path * */4 * * */bin/cp/etc//backup/etc-$ (date +%f%h%m%s)


7, weekly 2,4,6 backup/var/log/messages file to/backup/messages_logs/directory, save the file name like messages-20160830

[Email protected] ~]# crontab-ecrontab:installing new crontab[[email protected] ~]# crontab-l* */4 * * */bin/cp/etc/ /backup/etc-$ (Date +%f%h%m%s) * * * * */2/bin/cp/var/log/messages/backup/messages_logs/messages-$ (date +%F%H%M%S)


8, every two hours every day to take the current system/proc/meminfo file in the beginning of all the information in the S./stats/memory.txt file

[Email protected] ~]# crontab-l* */2 */1 * */bin/grep "^s"/proc/meminfo >>/stats/memory.txt


9, working days of work time, every two hours to perform the echo "Howdy"

[Email protected] ~]# crontab-l* 10-19 * * 1-5 echo "Howdy" #每天10点到19点上班, didn't work Monday to Friday


Script Programming Exercises

10. Create directory/tmp/testdir-Current date time

[Email protected] bin]# vim 10.sh#!/bin/bash#test=testdir-$ (date +%f%h%m%s) mkdir/tmp/$test [[email protected] bin]# Bash-n 10.sh[[email protected] bin]# bash-x 10.sh++ date +%f%h%m%s+ test=testdir-2016-09-06215248+ mkdir/tmp/testdir-20 16-09-06215248

1 1. Create 100 empty files in this directory: file1-file100

[[email protected] bin]# vim 11.sh#!/bin/bash#for  i in {1..100}; dotouch file$i                      #touch为当前目录/root/bin, you can also indicate the directory where the files are to be created Echo   "new file  $i" Done[[email protected] bin]# bash -n 11.sh [[email  protected] bin]# bash -x 11.sh + for i in  ' {1..100} ' +  touch file1+ echo  ' new file 1 ' new file 1+ for i in  ' { 1..100} ' + touch file2+ echo  ' new file 2 ' new file 2+ for i  in  ' {1..100} ' + touch file3+ echo  ' new file 3 ' new file 3+  for i in  ' {1..100} ' + touch file4+ echo  ' new file 4 ' ... 


12. Displays the user name of the user in the/ETC/PASSW D file that is located in the even row

[Email protected] bin]# vim 12.sh#!/bin/bash#sed-n ' n;p '/etc/passwd | Awk-f: ' {print $} ' [[email protected] bin]# sh 12. Shbinadmsynchaltuucpgamesftpdbusvcsartkitabrtnfsnobodygdmapachepostfixtomcattcpdumpmyuser

13. Create 10 user user10-user19; password with user name

[Email protected] bin]# vim 13.sh#!/bin/bash#for i in {10..19}; Do Useradd user$i echo "User$i" | passwd--stdin "user$i" done [[e-mail protected] bin]# bash-n 13.sh [[email protected] bin]# bash-x 13.sh + for I in ' {10. . +} ' + Useradd user10useradd: Warning: This home directory already exists. Do not copy any files from the Skel directory to them. Creating mailbox file: File already exists + passwd--stdin user10+ echo user10 change user user10 password. PASSWD: All the authentication tokens have been successfully updated. + for I in ' {10..19} ' + Useradd user11+ echo user11+ passwd--stdin user11 change user user11 password ....


14, create 10 empty files file10-file19 in/tmp/;

[Email protected] bin]# vim 14.sh#!/bin/bash#for i in {10..19};  Dotouch/tmp/file$iecho "New file:file$i" Done[[email protected] bin]# bash-n 14.sh [[email protected] bin]# bash-x 14.sh + for I in ' {10..19} ' + touch/tmp/file10+ echo ' new File:file10 ' new file:file10+ for I in ' {10..19} ' + touch/tmp/file11+ Echo ' New File:file11 ' new file:file11+ for I in ' {10..19} ' + touch/tmp/file12+ echo ' new File:file12 ' ....

15. Change the File10 and the genus Group to User10, and so on.

#!/bin/bash#for i in {10..19}; dochown user "$i": User "$i"  /tmp/file$idone[[email  protected] bin]# ll /tmp/file*-rw-r--r--.  1 user10 user10 0 9 Month    6 22:30 /tmp/file10-rw-r--r--.  1 user11 user11 0 9 Month     6 22:30 /tmp/file11-rw-r--r--.  1 user12 user12 0 9 Month     6 22:30 /tmp/file12-rw-r--r--.  1 user13 user13 0 9 Month     6 22:30 /tmp/file13-rw-r--r--.  1 user14 user14 0 9 Month    6  22:30 /tmp/file14-rw-r--r--.  1 user15 user15 0 9 Month    6  22:30 /tmp/file15-rw-r--r--.  1 user16 user16 0 9 Month    6 22:30  /tmp/file16-rw-r--r--.  1 user17 user17 0 9 Month    6 22:30  /tmp/file17-rw-r--r--.  1 user18 user18 0 9 Month    6 22:30 /tmp/file18-rw-r--r--.  1 user19 user19 0 9 Month    6 22:30 /tmp/file19


This article is from the "Linux Sailing" blog, make sure to keep this source http://jiayimeng.blog.51cto.com/10604001/1846980

Week six Homework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.