N25第六周 vim,crontab命令的使用

請詳細總結vim編輯器的使用并完成以下練習題

vim是一款功能強大的文本編輯器,是程序員的必備神器。



vim工作模式分為三種:編輯模式,輸入模式,末行模式,三種工作模式可以進行來自由切換
    編輯模式---》輸入模式: 直接鍵i 
    輸入模式---》編輯模式: Esc
    編輯模式---》末行模式: Ctrl+:
    末行模式---》輸入模式: 無法直接切換,需要經由編輯模式切換。

    編輯模式下命令:
    a:光標跳至行尾進入輸入模式;
    h:向左移動;
    l:向右移動;
    j:向下移動;
    J:把下行移動到光標所在行行尾;
    K:向上移動;
    g: gg---跳轉是第一行;
    G: 跳轉至末行:
       #G---跳轉至指定行;
    w: 跳至下個單詞詞首;
    b: 跳至上個單詞詞首;
    e: 當前或下個但是詞尾;
    o: 在光標所在行下行插入空白行并切換至輸入模式;
    O:在光標所在行上行插入空白行并切換至輸入模式;
    P: 粘貼命令,在光標所在行下一行粘貼已復制的內容;
    P: 粘貼命令,在光標所在行下一行粘貼已復制的內容;
    x: 直接刪除光標所在的字符;
    X: 刪除光標所在字符的上個字符;
    r: replace, 以新字符取代光標所在的字符,按r后輸入新字符;
    u: 撤銷此前的操作;
    U: 撤銷“撤銷”操作;
    d: 配合其他鍵使用
       dd---直接刪除所在行;
       dw---刪除光標所在的單詞;
       d$---刪除光標至行尾的字符串;
       d^---刪除光標至行首的字符串;
    y: 與d鍵使用方法類似
       yy---復制光標所在行;
       yw---復制光標所在的單詞;
       y$---復制光標至行尾的字符串;
       y^---復制光標至行首的字符串;

    v: visual 類似是鼠標移動選定文本功能,配合h,j,k,l鍵使用可以上下左右選定文本;

    c: 與d鍵用法類似,不同的是其執行命令完后切換至輸入模式;
    ^: 光標跳至行首;
    $: 光標跳至行尾;
    Z:ZZ---保存文本并退出vim;   

對文本進行翻屏: 
    向前翻一屏:ctrl+f;
    向后翻一屏:ctrl+b;
    Enter      : 向后按行翻屏
    向前翻半屏:ctrl+u; 
    向后翻半屏:ctrl+d;

輸入模式下命令:與普通的輸入模式無異。

末行模式:
     q!:不保存退出 vim;
     wq: 保存文件并退出;
     w:  w /path/to/somewhere/filename, 當前文件保存至指定路徑下;

 對文本指定的行進行定位:
      #1,#2: #1---開始的行號
               #2---結束的行號
               可以對此區間的行進行刪除復制等操作:例如,3,5y 表示復制3行至5行的內容
       #,$:指定行至文本行尾;
      %:文本全文;

 對文本內容進行替換處理:
     #1,#2 s/原內容/新內容/g
            原內容:支持正則表達式;
            新內容:不支持正則表達式,但是可以使用&代替原內容; 



 在末行模式下可以對vim特性進行設置:
     set number: 顯示行號;
     set number: 不顯示行號;
     set hlsearch: 高亮顯示匹配結果;
     set nohlsearch: 不高亮顯示皮別結果;
     set ai        :  自動縮進;
     set no ai     :  不自動縮進;
     syntax on     : 語法檢查開啟;
     syntax off    : 語法檢查關閉

    注意:此配置僅對當前編輯有效,如需要全局有效則在 /etc/vimrc 中進行配置。

vim支持同時打開多個文件:
        vim /path/to/somewhere/filename1 /path/to/somewhere/filename2. 
        當filename不存在時 系統將自動建立文件.。
    支持多窗口:
        水平:vim -o /path/to/somewhere/filename1 /path/to/somewhere/filename2       
        垂直:vim -O /path/to/somewhere/filename1 /path/to/somewhere/filename2

    各窗口間切換: ctrl+w+箭頭;


  通知單文件也支持多窗口:

        ctrl+W,S 水平窗口
        ctrl+w,v 垂直窗口

1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;

 復制:cp /etc/rc.d/rc.sysinit /tmp

 打開:vim /tmp/rc.sysinit.

 切換至末行模式: %s@^[[:space:]]\+.*@#&@

2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;

 復制: cp /boot/grub/grub.conf  /tmp

 打開: vim /tmp/grub.conf

 切換至末行模式:  %s#^[[:space:]]##

3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符;

  打開: vim /tmp/rc.sysinit

  末行模式下: %s@^#\+[[:space:]]\+@@

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

  打開: vim  /tmp/grub.conf

  末行模式下: 1,3s/^/#&/

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

    [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
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    :%s@\(enabled\|gpgcheck\)=0@\1=1@

6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為形如etc-201504020202

   [root@localhost spool]# crontab -l

   0 */4 * * * /bin/cp -r /etc /backup/etc-$(date +\%Y\%m\%d\%H\%M)


   注意:%在crontab有特殊含義必須使用轉義字符。

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

 [root@localhost spool]# crontab -u admin -l

 0 12 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_logs/messages_$(date +\%Y\%m\%d)

8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中

  [root@localhost spool]# crontab -l

  0 */2 * * * /bin/cat /proc/meminfo | /bin/grep "^S.*" >/stats/memory.txt

  注意:memory.txt文件必須事先創立,否則任務執行會失敗。

9、工作日的工作時間內,每兩小時執行一次echo "howdy"

[root@localhost ~]# crontab -l

0 9-18/2 * * 1-5 /bin/echo "howdy"

腳本編程練習

10、創建目錄/tmp/testdir-當前日期時間;

[root@localhost tmp]# cat /tmp/scripts1.sh #!/bin/bash mkdir /tmp/testdir-$(date +%F%H%M)

[root@localhost tmp]# ./scripts1.sh
[root@localhost tmp]# ls -l | grep testdir
drwxr-xr-x. 2 root  root   4096 Dec 27 07:01 testdir-2016-12-270701
drwxr-xr-x. 2 root  root   4096 Dec 27 07:03 testdir-2016-12-270703
drwxr-xr-x. 2 root  root   4096 Dec 27 07:09 testdir-2016-12-270709
[root@localhost tmp]#

11、在此目錄創建100個空文件:file1-file100;

   #! /bin/bash

   for i in {1..100};do
      touch ./file$i
      done
      echo "100 files completely created"


    [root@localhost tmp]# bash scripts2
    100 files completely created
    [root@localhost tmp]# ls -l ./
    total 260
    -rwsr-xr-x. 1 root  root  48568 Dec 26 00:40 cat
    -rw-------. 1 root  root     87 Dec 27 06:36 crontab.hpk75h
    -rw-------. 1 root  root     56 Dec 27 06:35 crontab.YLrMNM
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file1
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file10
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file100
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file11
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file12
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file13
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file14
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file15
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file16
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file17
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file18
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file19
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file2
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file20
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file21

  限于篇幅只截取部分顯示內容。

12、顯示/etc/passwd文件中位于第偶數行的用戶的用戶名;

   [root@localhost ~]# cat /tmp/scripts3
    #!/bin/bash
    sed -n '2~2p' /etc/passwd |cut -d: -f1
    [root@localhost ~]# bash  /tmp/scripts3
    bin
    bash
    nologin
    user1
    user10
    user12
    user14
    user16
    user18
    user20
    user22
    user24
    user26
    user28
    [root@localhost ~]#

13、創建10用戶user10-user19;密碼同用戶名;

    [root@localhost tmp]# cat /tmp/scripts4
    #!/bin/bash

    for i in {20..29};do
        if ( id user$i &> /dev/null); then
           echo "user exits"
        else useradd user$i && echo "user$i" | passwd user$i --stdin 
        fi
    done
       echo "ten users added completely"


   [root@localhost tmp]# bash scripts4
    Changing password for user user20.
    passwd: all authentication tokens updated successfully.
    Changing password for user user21.
    passwd: all authentication tokens updated successfully.
    Changing password for user user22.
    passwd: all authentication tokens updated successfully.
    Changing password for user user23.
    passwd: all authentication tokens updated successfully.
    Changing password for user user24.
    passwd: all authentication tokens updated successfully.
    Changing password for user user25.
    passwd: all authentication tokens updated successfully.
    Changing password for user user26.
    passwd: all authentication tokens updated successfully.
    Changing password for user user27.
    passwd: all authentication tokens updated successfully.
    Changing password for user user28.
    passwd: all authentication tokens updated successfully.
    Changing password for user user29.
    passwd: all authentication tokens updated successfully.
    ten users added completely
    [root@localhost tmp]# cat /etc/passwd | tail
    user20:x:517:517::/home/user20:/bin/bash
    user21:x:518:518::/home/user21:/bin/bash
    user22:x:519:519::/home/user22:/bin/bash
    user23:x:520:520::/home/user23:/bin/bash
    user24:x:521:521::/home/user24:/bin/bash
    user25:x:522:522::/home/user25:/bin/bash
    user26:x:523:523::/home/user26:/bin/bash
    user27:x:524:524::/home/user27:/bin/bash
    user28:x:525:525::/home/user28:/bin/bash
    user29:x:526:526::/home/user29:/bin/bash

14、在/tmp/創建10個空文件file10-file19;

    #!/bin/bash
        for i in {10..19};do
             touch /tmp/file$i
        done
        echo " ten files completely created "
    ~                                                                                                                                              
    ~                              

   [root@localhost tmp]# bash scripts5
   ten files completely created

15、把file10的屬主和屬組改為user10,依次類推。

    #!/bin/bash
       for ((i=10;i<=19;i++));do
              chown user$i:user$i file$i
            ls -l /tmp/file$i
            done
       echo " changing the owner of files completed " 
    ~                                                                                                                                              

    [root@localhost tmp]# bash  scripts6
    -rw-r--r--. 1 user10 user10 0 Dec 27 11:53 /tmp/file10
    -rw-r--r--. 1 user11 user11 0 Dec 27 11:53 /tmp/file11
    -rw-r--r--. 1 user12 user12 0 Dec 27 11:53 /tmp/file12
    -rw-r--r--. 1 user13 user13 0 Dec 27 11:53 /tmp/file13
    -rw-r--r--. 1 user14 user14 0 Dec 27 11:53 /tmp/file14
    -rw-r--r--. 1 user15 user15 0 Dec 27 11:53 /tmp/file15
    -rw-r--r--. 1 user16 user16 0 Dec 27 11:53 /tmp/file16
    -rw-r--r--. 1 user17 user17 0 Dec 27 11:53 /tmp/file17
    -rw-r--r--. 1 user18 user18 0 Dec 27 11:53 /tmp/file18
    -rw-r--r--. 1 user19 user19 0 Dec 27 11:53 /tmp/file19
     changing the owner of files completed 
    [root@localhost tmp]# vim scripts6

原創文章,作者:diglinux,如若轉載,請注明出處:http://www.www58058.com/65139

(0)
diglinuxdiglinux
上一篇 2016-12-29
下一篇 2016-12-29

相關推薦

欧美性久久久久