馬哥教育網絡班20期+第6周練習博客

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


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

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

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


[root@bogon ~]# cp /boot/grub/grub.conf  /tmp/grub.conf
[root@bogon ~]# vim /tmp/grub.conf
[root@bogon ~]# :%s/^#[[:space:]]\+//g

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


[root@bogon ~]#: vim/tmp/rc.sysinit
[root@bogon ~]#: %s/^[[:space:]]\{0,\}#\+//g


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


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

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


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

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


[root@bogon ~]# crontab -e
* */4 * * * cp -r /etc/ /backup/etc-`date +%Y%m%d%H%M`
[root@bogon ~]# crond start


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


[root@bogon ~]# * * * * */2,4,6 cp -r /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`


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


[root@bogon ~]# * */2 */1 * * grep "^S" /proc/meminfo >> /stats/memory.txt


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

[root@bogon ~]#  * 9-17/2 * * */1-5 echo "howdy"


腳本編程練習

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


 [root@bogon ~]# vim /tmp/testdir.sh 
 #!/bin/bash
  mkdir -pv /tmp/testdir-$(date +%F-%H-%M-%S)

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


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


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


[root@bogon ~]# cat -n  /etc/passwd | sed -n '2~2p' |cut -d: -f1
     2  bin
     4  adm
     6  sync
     8  halt
    10  uucp
    12  games
    14  ftp
    16  dbus
    18  vcsa
    20  avahi-autoipd
    22  haldaemon
    24  ntp
    26  saslauth
    28  pulse
    30  tcpdump
    32  bash
    34  basher
    36  fedora
    38  dhcpd

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


[root@bogon ~]# cat ./useradd.sh
#!/bin/bash
for i in {10..19};do
    if id user$i &>/dev/null;then
    echo "user$i exists."
    else
    useradd user$i
    if [ $? -eq 0 ];then
        echo "user$i" | passwd --stdin user$i &> /dev/null
        echo "Add user$i is finished."
    fi
    fi
done

[root@bogon ~]# bash useradd.sh
Add user10 is finished.
Add user11 is finished.
Add user12 is finished.
Add user13 is finished.
Add user14 is finished.
Add user15 is finished.
Add user16 is finished.
Add user17 is finished.
Add user18 is finished.
Add user19 is finished.
[root@bogon ~]#


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


[root@bogon ~]# touch /tmp/file{10..19}


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


[root@bogon ~]# vim ./usertest.sh
#!/bin/bash
cd /tmp
for i in {10..19};do
    chown user$i:user$i /tmp/file$i
done


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

(0)
huan918huan918
上一篇 2016-07-22 10:10
下一篇 2016-07-22 10:10

相關推薦

  • MAN手冊各章節功能介紹及快捷鍵鍵位整理

       初學linux ,請教別人的時候,遇到人不耐煩回答我等菜菜的問題,都會說一句,“自己去man”,“翻過man了嗎,再來問吧”,哦,my gd,這個man到底是何方超人啊,其實man就是linux自帶的文檔,假如你不知道ls這個命令的用法,你可以試著敲入 man  ls  ,然后來看看,會有啥結果。效果不錯吧,一般…

    Linux干貨 2016-10-18
  • 非常不錯的編程技術教程

    下面是一些非常不錯的編程教程,當然,全是英文版的。不過因為是新手教程,所以非常容易閱讀,可以在學習技術的同時加強一下自己的英語閱讀能力。 如果你是一個新手,建議你把本頁設為你的收藏夾。C Introduction to C Programming C Optimization Tutorial Compiling C and C…

    Linux干貨 2016-05-10
  • 位置變量&特殊變量總結

    位置變量 常用的位置變量有 $1, $2, $3 ……,表示命令行傳給腳本的第一個參數,第二個參數,第三個參數。。。 $0 表示腳本的文件名,比如a.sh 位置變量在腳本中的主要作用,是讓腳本通過他們來獲取命令行傳遞給腳本的參數。 變量位置調整 shift [n] 用于調整變量位置 第n+1個位置變量會被重新命名為$1…

    Linux干貨 2016-08-15
  • awk,systemctl,破解7root口令

    awk -F 指明輸入時用到的字段分隔符 默認空格為分隔符 -v 自定義變量 基本格式:awk [options] 'program' file $1,$2..$n稱為域標識,$0為所有域。 文件的每一行稱為記錄 awk '{print}' /etc/passwd 默認 print $0 顯示全段   awk…

    Linux干貨 2016-10-05
  • 推薦-LVM

    LVM LVM Linux應用 1.LVM簡介 LVM:Logical Volume Manager,邏輯卷管理器。LVM利用Linux內核的device-mapper模塊來實現存儲系統的虛擬化(系統分區獨立于底層硬件)。通過LVM,你可以實現存儲空間的抽象化并在上面建立虛擬分區(virtual partitions),可以更簡便地擴大和縮小分區,可以增刪分…

    2016-04-11

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-07-22 10:21

    寫的很好,排版也很棒,第3 5 好像不對吧,crontab每周,直接寫就可以,不用除以,每天也不用除以1,加油

欧美性久久久久