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

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

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

[root@centos ~]# cp /etc/rc.d/rc.sysinit /tmp/
[root@centos ~]# vim /tmp/rc.sysinit 
:%s@^[[:space:]]\+@#&@g
[root@centos ~]# cat /tmp/rc.sysinit | less
#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
#    . /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
#    HOSTNAME=localhost
fi
if [ ! -e /proc/mounts ]; then
#       mount -n -t proc /proc /proc
#       mount -n -t sysfs /sys /sys >/dev/null 2>&1

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

[root@centos ~]# cp /boot/grub/grub.conf /tmp/
:%s@^[[:space:]]\+@@g
[root@centos ~]# cat /tmp/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_centos-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-504.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-504.el6.x86_64.img

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

:%s@^#[[:space:]]\+@@g
[root@centos ~]# less /tmp/rc.sysinit 
#!/bin/bash
#
/etc/rc.d/rc.sysinit - run once at boot time
#
Taken in part from Miquel van Smoorenburg's bcheckrc.
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
HOSTNAME=localhost
fi
if [ ! -e /proc/mounts ]; then
mount -n -t proc /proc /proc
mount -n -t sysfs /sys /sys >/dev/null 2>&1
fi
if [ ! -d /proc/bus/usb ]; then

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

:1,3s@.*@#&@g

[root@centos ~]# cat /tmp/grub.conf 
## grub.conf generated by anaconda
##
## Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)

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

:%s@=[0-1]@=1@g 
[root@centos ~]# cat /etc/yum.repos.d/CentOS-Media.repo | grep "="
#  yum --enablerepo=c6-media [command]
#  yum --disablerepo=\* --enablerepo=c6-media [command]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

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

[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
[root@centos ~]# cat backup.sh 
#!/bin/bash
if [ -e /backup ]; then 
cp -r /etc /backup/etc-%Y%m%d%H%M
else
mkdir /backup && cp -r /etc /backup/etc-%Y%m%%d%H%M
fi
exit 0

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

[root@centos ~]# chmod +x backup1.sh 
[root@centos ~]# crontab -u root -e
crontab: installing new crontab
[root@centos ~]# bash -n backup1.sh 
[root@centos ~]# bash -x backup1.sh 
+ '[' -e /backup/messages_logs ']'
+ mkdir -p /backup/messages_logs
+ cp /var/log/messages /backup/messages_logs/messages-%Y%m%d
+ exit 0
[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
0 0 * * 2,4,6 /root/backup1.sh
[root@centos ~]# cat backup1.sh 
#!/bin/bash
if [ -e /backup/messages_logs ]; then 
cp /var/log/messages  /backup/messages_logs/messages-%Y%m%d
else
mkdir -p /backup/messages_logs && cp /var/log/messages  /backup/messages_logs/messages-%Y%m%d
fi
exit 0

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

[root@centos ~]# bash -n meminfo.sh 
[root@centos ~]# bash -x meminfo.sh 
+ '[-e' /status ']'
meminfo.sh: line 2: [-e: command not found
+ mkdir /status
+ grep '^S' /proc/meminfo
+ exit 0
[root@centos ~]# cat meminfo.sh 
#!/bin/bash
if [-e /status ];then
grep "^S" /proc/meminfo >>/status/memory.txt
else
mkdir /status && grep "^S" /proc/meminfo >>/status/memory.txt
fi
exit 0
[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
0 0 * * 2,4,6 /root/backup1.sh
0 */2 * * * /root/meminfo.sh

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

[root@centos ~]# crontab -u root -l
0 */4 * * * /root/backup.sh
0 0 * * 2,4,6 /root/backup1.sh
0 */2 * * * /root/meminfo.sh
0 9-18/2 * * 1,2,3,4,5 /root/echo.sh

腳本編程練習

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

[root@centos test]# bash -x exercise1.sh 
++ date +%F-%T
+ mkdir /tmp/testdir-2016-07-23-20:31:14
++ date +%F-%T
+ echo '/tmp/testdir-2016-07-23-20:31:14 finished! '
/tmp/testdir-2016-07-23-20:31:14 finished! 
[root@centos test]# cat excercise1.sh 
#!/bin/bash
mkdir /tmp/testdir-$(date +%F-%T) && echo "/tmp/testdir-$(date +%F-%T) finished! "

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

[root@centos test]# bash -n exercise2.sh 
[root@centos test]# bash -x exercise2.sh 
+ for i in '{1..100}'
+ touch file1
+ for i in '{1..100}'
+ touch file2
+ for i in '{1..100}'
+ touch file3
+ for i in '{1..100}'
[root@centos test]# cat excersice2.sh 
#!/bin/bash
for i in {1..100};do
touch file$i
done
exit 0

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

[root@centos test]# bash -x  exercise3.sh
+ cut -d: -f2
+ awk '{if($1%2!=0) next ;printf"%s:%s\n", $1, $2}'
+ cat -n /etc/passwd
bin
adm
sync
halt
uucp
games
ftp
dbus
rpc
rtkit
avahi-autoipd
postfix
nfsnobody
gdm
apache
sshd
derulo
testbash
nologin
user1
[root@centos test]# cat excercise3.sh 
#!/bin/bash
cat -n /etc/passwd | awk  '{if($1%2!=0) next ;printf"%s:%s\n", $1, $2}' | cut -d: -f2

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

[root@centos test]# bash -n exercise4.sh 
[root@centos test]# bash -x exercise4.sh 
+ for i in '{10..19}'
+ useradd user10
+ passwd --stdin user10
+ echo user9
Changing password for user user10.
passwd: all authentication tokens updated successfully.
+ echo 'user10 added!'
user9 added!
+ for i in '{10..19}'
+ useradd user11
+ passwd --stdin user11
+ echo user11
Changing password for user user11.
[root@centos test]# cat exercise4.sh 
#!/bin/bash
for i in {10..19}; do
useradd user$i && echo "user$i" | passwd --stdin user$i
echo "user$i added!"
done

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

[root@centos test]# bash -n  exercise5.sh 
[root@centos test]# bash -x  exercise5.sh 
+ for i in '{10..19}'
+ touch /tmp/file10
+ echo 'file /tmp/file10 added!'
file /tmp/file10 added!
+ for i in '{10..19}'
+ touch /tmp/file11
+ echo 'file /tmp/file11 added!'
[root@centos test]# cat exercise5.sh 
#!/bin/bash
for i in {10..19}; do
touch /tmp/file$i
echo "file /tmp/file$i added!"
done

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

[root@centos test]# bash -n  exercise6.sh
[root@centos test]# bash -x  exercise6.sh
+ for i in '{10..19}'
+ chown user10:user10 /tmp/file10
+ echo 'file /tmp/file10  user and group have changed!!'
file /tmp/file10  user and group have changed!!
+ for i in '{10..19}'
+ chown user11:user11 /tmp/file11
+ echo 'file /tmp/file11  user and group have changed!!'
file /tmp/file11  user and group have changed!!
[root@centos test]# cat exercise6.sh 
#!/bin/bash
for i in {10..19}; do
chown user$i:user$i /tmp/file$i
echo "file /tmp/file$i  user and group have changed!!"
done
[root@centos test]# ls -lh /tmp/file*
-rw-r--r--. 1 user10 user10 0 Jul 24 11:03 /tmp/file10
-rw-r--r--. 1 user11 user11 0 Jul 24 11:03 /tmp/file11
-rw-r--r--. 1 user12 user12 0 Jul 24 11:03 /tmp/file12
-rw-r--r--. 1 user13 user13 0 Jul 24 11:03 /tmp/file13
-rw-r--r--. 1 user14 user14 0 Jul 24 11:03 /tmp/file14
-rw-r--r--. 1 user15 user15 0 Jul 24 11:03 /tmp/file15
-rw-r--r--. 1 user16 user16 0 Jul 24 11:03 /tmp/file16
-rw-r--r--. 1 user17 user17 0 Jul 24 11:03 /tmp/file17
-rw-r--r--. 1 user18 user18 0 Jul 24 11:03 /tmp/file18
-rw-r--r--. 1 user19 user19 0 Jul 24 11:03 /tmp/file19

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

(0)
SnooSnoo
上一篇 2016-07-27
下一篇 2016-07-27

相關推薦

  • Apache運行機制剖析

    1. B/S交互過程 瀏覽器(Browser)和服務器(Web Server)的交互過程:   1、  瀏覽器向服務器發出HTTP請求(Request)。 2、  服務器收到瀏覽器的請求數據,經過分析處理,向瀏覽器輸出響應數據(Response)。 3、  瀏覽器收到服務器的響應數據,經過分析處理,將最終結果顯示在瀏覽…

    Linux干貨 2015-04-10
  • DevOps如何重構IT戰略

    翻譯: 至尊寶 原文: http://www.citeworld.com/article/2897738/development/how-devops-can-redefine-your-it-strategy.html?page=2   DevOps究竟是曇花一現還是你一直在尋找的競爭優勢?我們咨詢了一些專家,對于這種趨勢他們的想法是什…

    Python干貨 2015-03-26
  • Linux各文件顏色

    Linux系統中默認將不同的文件類型以不同的顏色加以區分: 1、普通文件   白色 2、目錄文件  藍色 3、鏈接文件  青色 4、套接字文件  粉紅色 5、可執行文件  綠色 6、管道文件  7、塊文件   黃色 8、壓縮文件  紅色 默認文件顯示顏色可以在…

    Linux干貨 2016-10-19
  • 文本處理之sed

     sed:是一種行編輯器,它在處理行時會把要處理的行讀入模式空間中,處理的是模式空間的內容,一行一行的處理,然后把處理結果顯示在屏幕中,不對原文做修改,除非強制重定向。   好處:可同時編輯一個或多個文件,簡化了對文件的反復操作。 sed用法:   格式: sed [options ]…'script&#0…

    Linux干貨 2016-08-15
  • DNS高級應用

        DNS高級應用     1、主從復制      應用場景: (1)、當主DNS服務器壓力過大,無法正常處理過多的DNS解析請求時,從DNS服務器可以起到負載均衡的作用。 (2)、當主DNS服務器出現故障時,從DNS服務器可以為其提供冗余備份功能。     實驗環…

    Linux干貨 2015-06-18

評論列表(2條)

  • 馬哥教育
    馬哥教育 2016-07-27 11:12

    寫的很好,排版也很棒,第8題的腳本是不是錯了?加油

    • Snoo
      Snoo 2016-07-27 15:56

      @馬哥教育目測少了個空格,bash -n 不嚴謹啊,,

欧美性久久久久