1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;
# cp -a /etc/rc.d/rc.sysinit /tmp/ # ls /tmp/ | grep rc.sysinit rc.sysinit # vim /tmp/rc.sysinit %s@^[[:space:]]\{1,\}@#&@g
執行結果片段:
[ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii if strstr "$cmdline" confirm ; then # touch /var/run/confirm fi # Let rhgb know that we're leaving rc.sysinit if [ -x /bin/plymouth ]; then # /bin/plymouth --sysinit fi 385 次替換,共 385 行
總結:
a、@替換前@替換后@參數
b、^[[:space:]],匹配以空格開頭的行
c、{1,}表示匹配至少一次之前的RE字符
d、&表示匹配之前的內容
2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;
[root@C67-X64-A0 ~]# cp -a /boot/grub/grub.conf /tmp/ [root@C67-X64-A0 ~]# cat -n /tmp/grub.conf 1 # grub.conf generated by anaconda 2 # 3 # Note that you do not have to rerun grub after making changes to this file 4 # NOTICE: You have a /boot partition. This means that 5 # all kernel and initrd paths are relative to /boot/, eg. 6 # root (hd0,0) 7 # kernel /vmlinuz-version ro root=/dev/sda5 8 # initrd /initrd-[generic-]version.img 9 #boot=/dev/sda 10 default=0 11 timeout=5 12 splashimage=(hd0,0)/grub/splash.xpm.gz 13 hiddenmenu 14 title CentOS 6 (2.6.32-573.el6.x86_64) 15 root (hd0,0) 16 kernel /tboot.gz logging=vga,serial,memory 17 module /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 intel_iommu=on amd_iommu=on rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=128M.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet 18 module /initramfs-2.6.32-573.el6.x86_64.img [root@C67-X64-A0 ~]# vim /tmp/grub.conf %s#^[[:space:]]##g [root@C67-X64-A0 ~]# cat -n /tmp/grub.conf 1 # grub.conf generated by anaconda 2 # 3 # Note that you do not have to rerun grub after making changes to this file 4 # NOTICE: You have a /boot partition. This means that 5 # all kernel and initrd paths are relative to /boot/, eg. 6 # root (hd0,0) 7 # kernel /vmlinuz-version ro root=/dev/sda5 8 # initrd /initrd-[generic-]version.img 9 #boot=/dev/sda 10 default=0 11 timeout=5 12 splashimage=(hd0,0)/grub/splash.xpm.gz 13 hiddenmenu 14 title CentOS 6 (2.6.32-573.el6.x86_64) 15 root (hd0,0) 16 kernel /tboot.gz logging=vga,serial,memory 17 module /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 intel_iommu=on amd_iommu=on rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=128M.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet 18 module /initramfs-2.6.32-573.el6.x86_64.img
3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符
# vim /tmp/rc.sysinit :%s@^#[[:space:]]\+@@g 執行結果如下: Now that we have all of our basic modules loaded and the kernel going, let's dump the syslog ring somewhere so we can find it later [ -f /var/log/dmesg ] && mv -f /var/log/dmesg /var/log/dmesg.old dmesg -s 131072 > /var/log/dmesg create the crash indicator flag to warn on crashes, offer fsck with timeout touch /.autofsck &> /dev/null [ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii if strstr "$cmdline" confirm ; then touch /var/run/confirm fi Let rhgb know that we're leaving rc.sysinit if [ -x /bin/plymouth ]; then /bin/plymouth --sysinit fi
4、為/tmp/grub.conf文件中前三行的行首加#號;
# vim /tmp/grub.conf 末行模式下: 1,3s@^.*@#&
5、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改為1;
%s@\(enabled\|gpgcheck\)=0@\1=1@g
6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為形如etc-201504020202
# mkdir -p /backup # crontab -l 0 */4 * * * /bin/cp -a /etc/ /backup/etc-$(date +%Y%m%d%H%M)
7、每周2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20150402
# mkdir -p /backup/messages_logs # cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d` # ls -l /backup/messages_logs/messages-201608 messages-201608 messages-20160809 說明: messages-20160809 滿足此條件 [root@C67-X64-A0 cron]# crontab -e 0 */4 * * * /bin/cp -a /etc/ /backup/etc-$(date +%Y%m%d%H%M) >/dev/null 2>&1 0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`>/dev/null 2>&1 [root@C67-X64-A0 cron]# cat /var/spool/cron/root 0 */4 * * * /bin/cp -a /etc/ /backup/etc-$(date +%Y%m%d%H%M) >/dev/null 2>&1 0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`>/dev/null 2>&1
8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中
[root@C67-X64-A0 cron]# mkdir -p /stats [root@C67-X64-A0 cron]# touch /stats/memory.txt [root@C67-X64-A0 cron]# grep "^S" /proc/meminfo >>/stats/memory.txt [root@C67-X64-A0 cron]# cat /stats/memory.txt SwapCached: 0 kB SwapTotal: 4194300 kB SwapFree: 4194300 kB Shmem: 1340 kB Slab: 134788 kB SReclaimable: 91524 kB SUnreclaim: 43264 kB # crontab -e 0 */2 * * * grep "^S" /proc/meminfo >>/stats/memory.txt
9、工作日的工作時間內,每兩小時執行一次echo "howdy"
0 9-18/2 * * 1-5 /bin/echo "howdy"
腳本編程練習(這里沒考慮文件、用戶或目錄本來就存在的情況)
10、創建目錄/tmp/testdir-當前日期時間;
# mkdir -p /tmp/testdir-$(date +%F)
11、在此目錄創建100個空文件:file1-file100
#!/bin/bash #created by molewan for i in `seq 1 100` do touch file$i done
12、顯示/etc/passwd文件中位于第偶數行的用戶的用戶名;
# sed -n 'n;p' /etc/passwd |awk -F":" '{print $1}'
13、創建10用戶user10-user19;密碼同用戶名;
#!/bin/bash #created by molewan for i in $(seq 10 19) do useradd user$i echo user$i | passwd --stdin user$i done
14、在/tmp/創建10個空文件file10-file19;
15、把file10的屬主和屬組改為user10,依次類推。
#!/bin/bash #created by molewan for i in $(seq 10 19) do touch file$i chown user$i:user$i /tmp/file$i done
原創文章,作者:Net21-冰凍vs西瓜,如若轉載,請注明出處:http://www.www58058.com/31947
寫的很好,排版也很棒,加油