第六周
請詳細總結vim編輯器的使用并完成以下練習題
文本編輯器:
vim: 模式化的編輯器 基本模式: 編輯模式,命令模式, 輸入模式 末行模式: 內置的命令行借口; 打開文件: 編輯模式:默認模式 編輯模式-->輸入模式: i:insert,在光標所在處輸入; a:append,在光標所在處后方輸入; o:在光標所在處的下方打開一個新行; I:子啊光標所在行的行首輸入; A:在光標所在行的行尾輸入; O:在光標所在處的上方打開一個新行; 輸入模式-->編輯模式 ESC 編輯模式-->末行模式 : 末行模式-->編輯模式 ESC 關閉文件: ZZ:保存并提出; :q 退出 :q! 強制退出,不保存此前的編輯操作 :wq 保存并退出; :w,:q :x 保存并退出; :w /PATH/TO/SOMEFILE 另存為 光標跳轉: 字符間跳轉 h:左 j:下 k:上 l:右 #COMMAND:跳轉由#指定的個數的字符; 單詞間跳轉 w:下一個單詞的詞首; e:當前或后一個單詞的詞尾; b:當前或前一個單詞的詞首; #COMMAND:跳轉由#指定的個數的單詞; 行首行尾跳轉 ^:跳轉至行首的第一個非空白字符; 0:跳轉至行首; $:跳轉至行尾; 行間跳轉 #G:跳轉至#指定的行; 1G,gg:第一行; G:最后一行; 句間跳轉 ) 下一句 ( 上一句 #):跳轉后#句 #(:跳轉前#句 段間跳轉 } 上一段 { 下一段 #}:跳轉后#段 #{:跳轉前#段 翻屏: Ctrl+f:向文件尾部翻一屏 Ctrl+b:向文件首部翻一屏 Ctrl+d:向文件尾部翻半屏 Ctrl+u:向文件首部翻半屏 Enter:按行向后翻
vim的編輯命令:
字符編輯 x:刪除光標所在處的字符; #x:刪除光標所在處起始的#個字符; xp:交換光標所在處的字符與其后面的字符的位置; 替換命令(replace): r:替換光標所在處的字符; rCHAR 刪除命令: d:刪除命令,可結合光標跳轉字符,實現范圍刪除; d$: d^: dw: de: db: #COMMAND: dd:刪除光標所在處的行; #dd:刪除光標所處的行起始的共#行; 粘貼命令(p,put。paste): p:緩沖區的內容如果為整行,則粘貼在當前光標所在行的下方;否則粘貼至當前光標所在處的后方; P:緩沖區的內容如果為整行,則粘貼在當前光標所在行的上方;否則粘貼至當前光標所在處的前方; 復制命令(yank,y): y:復制,工作行為相似于d命令 y$ y^ y0 ye yw yb #COMMAND yy:復制一整行 #yy:復制#行 改變命令(change,c):修改 編輯模式-->輸入模式 ,實現刪除操作; c$ c^ c0 cb ce cw #COMMAND cc:刪除光標所在的行,并轉換為輸入模式; #cc:
其他的編輯操作:
可視化模式:選定(向鼠標一樣選定) v:按字符選定; V:按行選定; 結合編輯命令:d,c,y 撤銷(undo)操作: u:撤銷此前的操作; #u:撤銷此前的#個操作; 撤銷此前的撤銷: Ctrl+r 重復執行前一個編輯操作; . vim自帶的練習教程:vimtutor
vim末行模式: 內建命令行接口
1.地址界定 :start_pos[,end_pos] #:特定的第#行,例如5即di5行; .:當前行; $:最后一行; #,#:指定行范圍,左側為起始行,右側為結束行; #,+#:指定范圍,左側為起始絕對編號,右側為相對左側行號的偏移量;例如:3,+7 .,$-1 1,$ %:全文 /pattern/:從光標所在處起始向文件尾部第一次被模式所匹配到的行; /first/,$ /pat1/,/pat2/:從光標所在處起始,第一次由pat1匹配到的行開始,至第一次由pat2匹配到的行結束之間的所有行; 可同編輯命令一同使用,實現編輯操作: d y c w /PATH/TO/SOMEFILE:將范圍內的文本保存至指定的文件中; r /PATH/FROM/SOMEFILE:將指定的文件中的文本讀取并插入至指定位置; 2.查找 /PATTERN:從當前光標所在處向文件尾部查找能夠被當前模式匹配到的所有字符串; ?/PATTERN:從當前光標所在處向文件首部查找能夠被當前模式匹配到的所有字符串; n:下一個,與命令方向相同; N:上一個,與命令方向相反; 3.查找并替換 s:末行模式的命令;使用格式: s/要查找的內容/替換為的內容/修飾符 要查找的內容:可使用正則表達式; 替換為的內容:不能使用正則表達式,但可以引用; 如果“要查找的內容”部分在模式中使用分組符號:在“替換為的內容”中使用后向引用; 直接引用查找模式匹配到的全部文本; 修飾符: i:忽略大小寫; g:全局替換,意味著一行中如果匹配到多次,則均替換; 可把分隔符替換為其他非常用字符: s@@@ s###
1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;
[root@zf ~]# cp /etc/rc.d/rc.sysinit /tmp [root@zf ~]# vi /tmp/rc.sysinit :%s@^\([[:space:]]\{1,\}.*\)@#\1@ 381 次替換,共 381 行
2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;
[root@zf ~]# cp /boot/grub/grub.conf /tmp [root@zf ~]# vi /tmp/grub.conf :%s@^[[:space:]]\+@@ 3 次替換,共 3 行
3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符
[root@zf ~]# vi /tmp/rc.sysinit :%s@^#[[:space:]]\+@@ 38 次替換,共 38 行
4、為/tmp/grub.conf文件中前三行的行首加#號;
[root@zf ~]# vi /tmp/grub.conf :1,3s@^\(.*\)@#\1@ 或 :1,3s@^@#@ 3 次替換,共 3 行
5、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改為1;
[root@zf ~]# /etc/yum.repos.d/CentOS-Media.repo :%s@\(enabled\|gpgcheck\)=0@\1=1@
6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為形如etc-201504020202
[root@zf ~]# mkdir /backup [root@zf ~]# crontab -e 0 */4 * * * /bin/cp -a /backup/etc-$(date "+%Y%m%d%H%M") > /dev/null
7、每周2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20150402
[root@zf ~]# mkdir /backup/messages_logs [root@zf ~]# crontab -e 0 0 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_logs/messages-$(date "+%Y%m%d" > /dev/null
8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中
[root@zf ~]# mkdir /stats [root@zf ~]# crontab -e 0 */2 * * * /bin/grep -i "^s" /proc/meminfo >> /stats/memory.txt
9、工作日的工作時間內,每兩小時執行一次echo "howdy"
[root@zf ~]# crontab -e 0 */2 * * 1-5 /bin/echo "howdy"
腳本編程練習
10、創建目錄/tmp/testdir-當前日期時間;
[root@zf ~]# vim date.sh #!/bin/bash date1=$(date "+%Y%m%d%H%M%S") mkdir /tmp/testdir-$date1 [root@zf ~]# chmod +x date.sh [root@zf ~]# ./date.sh [root@zf ~]# ls /tmp/ | grep testdir testdir-20161224140750 testdir-20161224140843
11、在此目錄創建100個空文件:file1-file100
[root@zf test]# vim file.sh #!/bin/bash for number in {1..100} do touch file$number done [root@zf test]# bash file.sh [root@zf test]# ls file1 file19 file29 file39 file49 file59 file69 file79 file89 file99 file10 file2 file3 file4 file5 file6 file7 file8 file9 file.sh file100 file20 file30 file40 file50 file60 file70 file80 file90 file11 file21 file31 file41 file51 file61 file71 file81 file91 file12 file22 file32 file42 file52 file62 file72 file82 file92 file13 file23 file33 file43 file53 file63 file73 file83 file93 file14 file24 file34 file44 file54 file64 file74 file84 file94 file15 file25 file35 file45 file55 file65 file75 file85 file95 file16 file26 file36 file46 file56 file66 file76 file86 file96 file17 file27 file37 file47 file57 file67 file77 file87 file97 file18 file28 file38 file48 file58 file68 file78 file88 file98
12、顯示/etc/passwd文件中位于第偶數行的用戶的用戶名;
[root@zf ~]# vim user2.sh #!/bin/bash num=$(wc -l /etc/passwd | cut -d' ' -f1) for n in $(seq 2 2 $num) do grep -n '^' /etc/passwd | grep "^$n:"| cut -d: -f2 done [root@zf test]# bash user2.sh bin adm sync halt uucp games ftp dbus vcsa rtkit abrt nfsnobody gdm apache postfix sshd nginx zabbix mageia openstack bash basher fedora user11 user13 user15 user17 user19
13、創建10用戶user10-user19;密碼同用戶名;
[root@zf test]# vim user.sh #!/bin/bash for n in {10..19} do useradd user$n echo "user$n" | passwd "user$n" --stdin done [root@zf test]# bash -x user.sh + for n in '{10..19}' + useradd user10 + echo user10 + passwd user10 --stdin 更改用戶 user10 的密碼 。 passwd: 所有的身份驗證令牌已經成功更新。 + for n in '{10..19}' + useradd user11 + echo user11 + passwd user11 --stdin 更改用戶 user11 的密碼 。 ... [root@zf test]# tail /etc/passwd user10:x:3009:3009::/home/user10:/bin/bash user11:x:3010:3010::/home/user11:/bin/bash user12:x:3011:3011::/home/user12:/bin/bash user13:x:3012:3012::/home/user13:/bin/bash user14:x:3013:3013::/home/user14:/bin/bash user15:x:3014:3014::/home/user15:/bin/bash user16:x:3015:3015::/home/user16:/bin/bash user17:x:3016:3016::/home/user17:/bin/bash user18:x:3017:3017::/home/user18:/bin/bash user19:x:3018:3018::/home/user19:/bin/bash
14、在/tmp/創建10個空文件file10-file19;
[root@zf tmp]# vi file.sh #!/bin/bash for number in {10..19} do touch /tmp/file$number done [root@zf tmp]# ls /tmp | grep "file[0-9][0-9]" file10 file11 file12 file13 file14 file15 file16 file17 file18 file19
15、把file10的屬主和屬組改為user10,依次類推。
[root@zf tmp]# vi user_file.sh #!/bin/bash for n in {10..19} do chown user$n:user$n /tmp/file$n done [root@zf tmp]# ls -l /tmp | grep "file[0-9][0-9]" -rw-r--r--. 1 root root 0 12月 25 11:20 file10 -rw-r--r--. 1 root root 0 12月 25 11:20 file11 -rw-r--r--. 1 root root 0 12月 25 11:20 file12 -rw-r--r--. 1 root root 0 12月 25 11:20 file13 -rw-r--r--. 1 root root 0 12月 25 11:20 file14 -rw-r--r--. 1 root root 0 12月 25 11:20 file15 -rw-r--r--. 1 root root 0 12月 25 11:20 file16 -rw-r--r--. 1 root root 0 12月 25 11:20 file17 -rw-r--r--. 1 root root 0 12月 25 11:20 file18 -rw-r--r--. 1 root root 0 12月 25 11:20 file19 [root@zf tmp]# bash -x user_file.sh + for n in '{10..19}' + chown user10:user10 file10 + for n in '{10..19}' + chown user11:user11 file11 + for n in '{10..19}' + chown user12:user12 file12 + for n in '{10..19}' + chown user13:user13 file13 + for n in '{10..19}' + chown user14:user14 file14 + for n in '{10..19}' + chown user15:user15 file15 + for n in '{10..19}' + chown user16:user16 file16 + for n in '{10..19}' + chown user17:user17 file17 + for n in '{10..19}' + chown user18:user18 file18 + for n in '{10..19}' + chown user19:user19 file19 [root@zf tmp]# ls -l /tmp | grep "file[0-9][0-9]" -rw-r--r--. 1 user10 user10 0 12月 25 11:20 file10 -rw-r--r--. 1 user11 user11 0 12月 25 11:20 file11 -rw-r--r--. 1 user12 user12 0 12月 25 11:20 file12 -rw-r--r--. 1 user13 user13 0 12月 25 11:20 file13 -rw-r--r--. 1 user14 user14 0 12月 25 11:20 file14 -rw-r--r--. 1 user15 user15 0 12月 25 11:20 file15 -rw-r--r--. 1 user16 user16 0 12月 25 11:20 file16 -rw-r--r--. 1 user17 user17 0 12月 25 11:20 file17 -rw-r--r--. 1 user18 user18 0 12月 25 11:20 file18 -rw-r--r--. 1 user19 user19 0 12月 25 11:20 file19
原創文章,作者:N25-深圳-尋覓,如若轉載,請注明出處:http://www.www58058.com/65124