Marco 2016 new Linux+python high-end op Viban-linux vim last-line mode, sed command, basic bash script

Source: Internet
Author: User
Tags create directory


Content of this week's job:

1. Copy the/etc/rc.d/rc.sysinit file to the/tmp directory and add the line beginning with at least one white-space character in the/tmp/rc.sysinit file #
:%[email protected]^[[:space:]]\[email Protected]#&@g
384 Substitutions on 384 lines
#vim末行模式下
%: Indicates full text equivalent to 1,$
S: Complete the Find and replace operation in the last line mode
s/what to look for/replace with content/modifiers
What to look for: Available modes
Replace with: cannot use mode, but can use \1, \2, ... You can also use "&" to refer to the entire content found in the previous lookup;
Modifier:
I: Ignore case
G: global substitution; By default, each row replaces only the first occurrence;
Find separators in substitutions/can be replaced with other characters, such as
[Email protected]@@
s###

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

:% [email protected] ^ [[: space:]] \ [email protected] @g
#This question is understood as replacing the line expelled from the empty table character with a blank, which is deleted.
3. Delete the # and blank characters that start with # in the /tmp/rc.sysinit file and follow at least one blank character

:% [email protected] ^ # [[: space:]] \ [email protected] @g
4. Add # to the beginning of the first three lines in the /tmp/grub.conf file;

: 1, [email protected] ^.*@#&
# 1,3: means 1-3 lines
# &: Indicates that the entire content found earlier is 1-3 lines of content
# Address delimitation
    : start_pos, end_pos
        #: Specific line #, for example 2 means line 2;
        #, #: From the left # means the beginning of the line, to the right # means the end of the line;
        #, + #: Start from the line indicated by # on the left, and add the number of lines indicated by # on the right;
        .: Current line
        $: Last line
            ., $-1
        %: Full text, equivalent to 1, $
        
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] @ [email protected]
6. Perform a backup of the / etc directory every 4 hours, and back up to the / backup directory. The name of the saved directory is like etc-201608300202

:% [email protected] \ (enabled = \) [email protected] \ [email protected]
# \ (enabled = \) 1 means that the group matches to enabled = 1 \ 1 means that the content in \ (\) is back-referenced.
7. Back up the / var / log / messages file to the / backup / messages_logs / directory on 2, 4, and 6 every week.

[[email protected] ~] # crontab -e
crontab: installing new crontab
[[email protected] ~] # crontab -l
* * * * 2, 4, 6, / bin / tar cvf / backup / messages_logs / messages-$ (date +% F) / var / log / messages
#Cyclic task plan:
            System cron tasks
                # Example of job definition:
                # .---------------- minute (0-59)
                # | .------------- hour (0-23)
                # | | .---------- day of month (1-31)
                # | | | .------- month (1-12) OR jan, feb, mar, apr ...
                # | | | | .---- day of week (0-6) (Sunday = 0 or 7) OR sun, mon, tue, wed, thu, fri, sat
                # | | | | |
                # * * * * * user-name command to be executed
                The order is: minute, hour, day, month, week
            crontab command:
                    crontab [-u user] [-l | -r | -e] [-i]
                        -l: list all tasks;
                        -e: edit task;
                        -r: remove all tasks;
                        -i: used with -r to allow users to selectively remove specified tasks in an interactive mode;

                        -u user: Only root can run and manage cron tasks for designated users;
# $ (date +% F):% F full date; same as% Y-% m-% d; the whole is expressed as referencing the current system time,-in the form of% F.
# $ (date +% y% m% d): You can also use this method to add the year, month and date to the end of the file.

#Example: [[email protected] messages_logs] # tar cvf / backup / messages_logs / messages-$ (date +% y% m% d) / var
        / log / messagestar: remove the leading "/" from the member name
        / var / log / messages
        [[email protected] messages_logs] # ls
        messages-160909
8. Take every information in the current system / proc / meminfo file that starts with S every two hours every day to the /stats/memory.txt file

[[email protected] ~] # crontab -e
crontab: installing new crontab
[[email protected] ~] # crontab -l
0 * / 2 * * * / bin / egrep ‘^ S’ / proc / meminfo >> /stats/memory.txt
#When creating a scheduled task, be sure to test whether there is a problem with the command yourself. For example, every two hours, it is best to assign a time to the minute.
9. Echo "howdy" is executed every two hours during working hours on working days

[[email protected] ~] # crontab -e
crontab: installing new crontab
[[email protected] ~] # crontab -l
0 * / 2 * * 1-5 / bin / echo "Howdy."
Scripting exercises

10. Create directory / tmp / testdir-current date and time;

   1 #! / Bin / bash
  2 #
  3 mkdir / tmp / testdir-$ (date +% F-% H-% M-% S)
    [[email protected] homework] # bash -x 10.sh
    ++ date +% F-% H-% M-% S
    + mkdir / tmp / testdir-2016-09-09-21-06-23
11. Create 100 empty files in this directory: file1-file100

Method 1: [[email protected] homework] # vim 11.sh
          1 #! / Bin / bash
          2 #
          3 for i in {1..100}; do
          4 touch file $ i
          5 done
    [[email protected] homework] # bash -n 11.sh
    [[email protected] homework] # bash -x 11.sh
Method 2:
        [[email protected] test] # cat 111.sh
        #! / bin / bash
        ##
        for ((i = 1; i <101; i ++)); do
            if [-f file $ i]; then
            continue;
            fi
            touch file $ i
        done
12. Display the user name of the user on the even-numbered line in the / etc / passwd file;

  1 #! / Bin / bash
  2 #
  3 sed -n ‘n; p’ / etc / passwd | cut -d: -f1
  #Knowledge point:
      usage:
        sed [option] ... ‘script‘ inputfile ...

            script:
                ‘Address Command’

            Common options:
                -n: Do not output the content in the mode to the screen;
                -e: multi-point editing;
                -f / PATH / TO / SCRIPT_FILE: read the editing script from the specified file;
                -r: support the use of extended regular expressions;
                -i: edit in place;
                        sed -n ‘n; p’ FILE: display even lines
                    sed ‘1! G; h; $! d’ FILE: reverse display of file content
                    sed ‘$! N; $! D’ FILE: two lines after taking out the file;
                    sed ‘$! d’ FILE: remove the last line of the file;
                    sed ‘G’ FILE: append a blank line to each line;
                    sed ‘/ ^ $ / d; G’ FILE: append a blank line to each line, if there are multiple blank lines, merge one blank line
                    sed ‘n; d’ FILE: display odd lines;
                    sed -n ‘1! G; h; $ p’ FILE: display each line in the file in reverse;
    Commonly used advanced editing commands:
                    sed -n ‘n; p’ FILE: display even lines;
                    sed ‘1! G; h; $! d’ FILE: reverse display of file content
                    sed ‘$! N; $! D’ FILE: two lines after taking out the file;
                    sed ‘$! d’ FILE: remove the last line of the file;
                    sed ‘G’ FILE: append a blank line to each line;
                    sed ‘/ ^ $ / d; G’ FILE: append a blank line to each line, if there are multiple blank lines, merge one blank line
                    sed ‘n; d’ FILE: display odd lines;
                    sed -n ‘1! G; h; $ p’ FILE: display each line in the file in reverse;

13. Create 10 users user10-user19; the password is the same as the user name;

    [[email protected] ljohn] # ls / home /
    bash linux openstacks user1 user14 user19 user6 users1 users5
    basher liu slackware user10 user15 user2 user7 users10 users6
    centos mandirva testbash user11 user16 user3 user8 users2 users7
    fedora mysqltuser1 user12 user17 user4 user9 users3 users8
    hadoop nologin user user13 user18 user5 useradd1 users4 users9
    [[email protected] ljohn] # cat 13.sh
    #! / bin / bash
    ##
    for i in {10..19}; do
        if id user $ i &> / dev / null; then
            echo "user $ i is exists."
        else
            useradd user $ i && echo "user $ i" | passwd --stdin user $ i
        fi
    done
14. Create 10 empty files file10-file19 in / tmp /;

    [[email protected] homework] # for i in {10..19}; do touch / tmp / file $ i; done
15. Change the owner and group of file10 to user10, and so on.

    [[email protected] homework] # cat 15.sh
    #! / bin / bash
    ##
    for i in {10..19}; do
        touch / tmp / file $ i && chown user $ i: user $ i / tmp / file $ i
    done
    [[email protected] tmp] # ll
    Total consumption 192
    -rw-r--r--. 1 root root 2 September 5 10:32 -rwxrwxrwt. 1 root root 2569 August 30 16:50 a.txt
    -rw-r--r--. 1 root root 852 August 21 11:31 etc.test
    -rw-r--r--. 1 user10 user10 0 September 9 22:52 file10
    -rw-r--r--. 1 user11 user11 0 September 9 22:52 file11
    -rw-r--r--. 1 user12 user12 0 September 9 22:52 file12
    -rw-r--r--. 1 user13 user13 0 September 9 22:52 file13
    -rw-r--r--. 1 user14 user14 0 September 9 22:52 file14
    -rw-r--r--. 1 user15 user15 0 September 9 22:52 file15
    -rw-r--r--. 1 user16 user16 0 September 9 22:52 file16
    -rw-r--r--. 1 user17 user17 0 September 9 22:52 file17
    -rw-r--r--. 1 user18 user18 0 September 9 22:52 file18
    -rw-r--r--. 1 user19 user19 0 September 9 22:52 file19


This article is from the "Ljohn" blog

Marco 2016 new Linux + Python high-end operation and maintenance class-Linux vim last line mode, sed command, basic bash script

Related Article

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.