馬哥教育網絡班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 09:27
下一篇 2016-07-27 09:31

相關推薦

  • 手動創建掛載交換分區

    SWAP分區 SWAP [root@cloud ~]# fdisk /dev/sdb Command (m for help): d Selected partition 1 Command (m for help): w The…

    Linux干貨 2016-06-09
  • KVM虛擬化平臺部署及管理

    前言 KVM即Kernel Virtual Machine,最初是由以色列公司Qumranet開發。2007年2月被導入Linux 2.6.20核心中,成為內核源代碼的一部分。2008年9月4日,Redhat收購了Qumranet,至此Redhat擁有了自己的虛擬化解決方案,之后便舍棄Xen開始全面扶持KVM,從RHEL6開始KVM便被默認內置于內核中。本文…

    Linux干貨 2015-07-19
  • 曲徑通幽處,禪房花木深—-bash禪意腳本

    編程基礎 程序:指令+數據 程序編程風格: 過程式:以指令為中心,數據服務于指令 對象式:以數據為中心,指令服務于數據 shell程序:提供了編程能力,解釋執行 編程基本概念 編程邏輯處理方式: 順序執行 循環執行 選擇執行 shell編程:過程式、解釋執行 編程語言的基本結構: 數據存儲:變量、數組 表達式: a + b 語句:if shell腳本基礎 s…

    Linux干貨 2016-08-12
  • 關于壓測的宏觀個人總結

    工作角色定位 首先先從宏觀角度來評估下本次的壓測工作. 從工作職責上說本次壓測理應由組內其它同學來完成,個人從旁協助或指導即可。團隊成員的成長對我個人來說才是更大的成長。所以即使這次壓測工作完成的再出色,都會因為是由我來完成的,所以都不能稱之為優秀的。對我個人的成長最多是項目經驗的增長和問題的積累,但對組員來說如何讓他們做到現有成果的80%是我需要幫助他們來…

    Linux干貨 2015-04-21
  • N25-第四周博客

      復制/etc/ske1目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組合其他用戶均沒有任何訪問權限。            2,編輯/etc/group文件,添加組hadoop.。    &nbs…

    Linux干貨 2016-12-22
  • LNMP WordPress 配置文件修改參考 安裝LNMP + 搭建WordPress個人博客的補充

    對小黑的  http://www.www58058.com/17222 的一些補充,因為他嘿嘿嘿我嘿嘿嘿嘿….. 實際使用中必須對一下文件作出修改 建議值為以下建議值 要不然實際使用過程中問題很大 nginx.conf    優化和上傳的限制 worker_processes  8; &…

    Linux干貨 2016-06-01

評論列表(2條)

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

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

    • Snoo
      Snoo 2016-07-27 15:56

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

欧美性久久久久