馬哥網絡教育班第21期+第六周課程練習

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

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

[root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/
[root@localhost ~]# vim /tmp/rc.sysinit
:%s/^[[:space:]]\+/#/

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

[root@localhost ~]# cp /boot/grub/grub.conf /tmp/
[root@localhost ~]# vim /tmp/grub.conf 
:%s/^[[:space:]]//

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

[root@localhost ~]# vim /tmp/rc.sysinit 
:%s/^#[[:space:]]\+//

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

[root@localhost ~]# vim /tmp/grub.conf 
:1,3s/^/#/

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

[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Media.repo
:%s/enabled=0/enabled=1
:%s/gpgcheck=0/gpcheck=1

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

[root@localhost ~]# mkdir /backup
[root@localhost ~]# echo 'tar cf /backup/etc-$(date +%Y%m%d%H%M) /etc/*' > /root/back.sh 
[root@localhost ~]# crontab -e
0 */4  * * * bash /root/back.sh &>/dev/null

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

[root@localhost ~]# mkdir /backup/messages_logs
[root@localhost ~]# echo 'tar cf /backup/messages_logs/messages-$(date +%Y%m%d)' > /root/back.sh 
[root@localhost ~]# crontab -e
* *  * * 2,4,6 bash /root/back.sh &>/dev/null

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

[root@localhost ~]# echo 'grep ^S /proc/meminfo >> /stats/memory.txt ' > /root/back.sh 
[root@localhost ~]# crontab -e
0 */2 * * * bash /root/back.sh &>/dev/null

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

[root@localhost ~]# crontab -e
0 11,13,15,17 * * 1-5 echo "howdy"

腳本編程練習

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

[root@localhost ~]# cat test10.sh 
#!/bin/bash
mkdir  /tmp/testdir-$(date +%Y%m%d-%H%M%S)

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

[root@localhost testdir-20160721-201246]# cat test11.sh 
#!/bin/bash
for i in {1..100};
do
        touch file$i
done
[root@localhost testdir-20160721-201246]# ls
file1    file2   file30  file41  file52  file63  file74  file85  file96
file10   file20  file31  file42  file53  file64  file75  file86  file97
file100  file21  file32  file43  file54  file65  file76  file87  file98
file11   file22  file33  file44  file55  file66  file77  file88  file99
file12   file23  file34  file45  file56  file67  file78  file89  test11.sh
file13   file24  file35  file46  file57  file68  file79  file9
file14   file25  file36  file47  file58  file69  file8   file90
file15   file26  file37  file48  file59  file7   file80  file91
file16   file27  file38  file49  file6   file70  file81  file92
file17   file28  file39  file5   file60  file71  file82  file93
file18   file29  file4   file50  file61  file72  file83  file94
file19   file3   file40  file51  file62  file73  file84  file95

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

[root@localhost ~]# cat test12.sh 
#!/bin/bash
sed -n 'n;p' /etc/passwd | cut -d: -f1
[root@localhost ~]# bash test12.sh 
bin
adm
sync
halt
uucp
games
ftp
dbus
vcsa
rtkit
abrt
nfsnobody
gdm
apache
postfix
sshd
named

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

#!/bin/bash

for i in {10..19}
do
        id user$i &> /dev/null
        if [ $? -ne 0 ];then
                useradd user$i
                echo user$i:user$i | chpasswd
        else
                echo "user$i existed!"
        fi
done

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

[root@localhost tmp]# cat test14.sh 

#!/bin/bash
for i in {10..19}
do
        if [ -f file$i ];then
                echo "file $i existed!"
        else
                touch file$i
        fi
done

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

[root@localhost tmp]# cat test15.sh 
#!/bin/bash
for i in {10..19}
do
        chown user$i:user$i file$i
done

原創文章,作者:N21-天天,如若轉載,請注明出處:http://www.www58058.com/27148

(0)
N21-天天N21-天天
上一篇 2016-08-02 10:58
下一篇 2016-08-02 10:58

相關推薦

  • 運維面試題和答案

    1、簡述TCP三次握手四次揮手過程及各過程中客戶端和服務器端的狀態。 #三次握手 客戶端向服務器端發送SYN包,客戶端進入SYN_SEND狀態 服務器端收到客戶端發送的包返回ACK+SYN包,服務器端進入SYN_RECV狀態 客戶端收到服務器端返回的包再發回ACK包,客戶端進入ESTABLISHED狀態,服務器端收到包也進入ESTABLISHED狀態 客戶端…

    Linux干貨 2016-06-10
  • 邏輯卷LVM

    邏輯卷LVM 簡介     在實際生產應用中,磁盤的分區的容量是固定不變的,當出現分區容量不足的情況,除了新加磁盤,還有沒有其他方法呢?    邏輯卷(LVM)的概念就出現了,全稱叫Logical Volume Manager。它的作用是允許對卷進行方便操作的抽象層,包括重新設定…

    Linux干貨 2017-08-12
  • CentOS7中nmcli網絡管理及使用詳解

    一、網絡接口配置工具    在CentOS7系統中,強烈推薦使用nmcli管理網卡。下面記錄的是nmcli的使用詳解。    網絡接口配置工具NetworkManager(簡稱為nmcli),該命令的作用是:可以查詢網絡連接的狀態,也可以用來管理網絡(設置系統每個網卡的特性)。該命令如何使用呢,其實可以用"n…

    Linux干貨 2016-09-11
  • Linux系統目錄結構

    root 管理員家目錄home 普通用戶家目錄bin 系統啟動和運行可能會用到的普通命令sbin 管理類命令proc 虛擬文件系統,由內核參數映射而來usr 系統軟件資源存放位置include 存放C/C++頭文件的目錄lib 庫文件lib64 64位系統庫文件tmp 臨時文件目錄boot 引導加載器所需文件,系統所需圖片保存于此etc 配置文件sys 虛擬…

    Linux干貨 2018-03-03
  • 什么是文件系統

    文件系統:層級結構;有索引; /: 原初起點; 倒置樹狀結構; /dev/pts/2: 最左側/: 表示根目錄 其它的/: 表示路徑分隔符 Linux的路徑分隔符是/ Windows的是\ 文件的路徑表示: 絕對路徑:從根開始表示出的路徑  相對路徑:從當前位置開始表示出的路徑 文件名使用法則: 嚴格區分字符大小寫:file1, File1, FI…

    Linux干貨 2016-10-29
  • net25-第14周作業

    系統的INPUT和OUTPUT默認策略為DROP; ~]# iptables -P INPUT DROP ~]# iptables -P OUTPUT DROP 1、限制本地主機的web服務器在周一不允許訪問;新請求的速率不能超過100個每秒;web服務器包含了admin字符串的頁面不允許訪問;web服務器僅允許響應報文離開本機; ~]#iptables -…

    Linux干貨 2017-05-15

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-02 11:29

    寫的很好,排版也很棒,加油,工作時間是從9點開始吧,在仔細看看第7個對嗎?

欧美性久久久久