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.178linux.com/31947
评论列表(1条)
写的很好,排版也很棒,加油