首页 > 代码库 > 第六周作业
第六周作业
第六周作业
1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
命令:
cp /etc/rc.d/rc.sysinit /tmp/ sed -i ‘s@^[[:space:]]@# @g
效果:
[root@liam ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@liam ~]# ls /tmp keyring-IFZ3Jm orbit-gdm orbit-root pulse-rsPriwMzVy2z pulse-vMB549h8T0sT rc.sysinit virtual-root.JW5ORZ yum.log [root@liam ~]# sed ‘s@^[[:space:]]@# @g‘ /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
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
命令:
cp /boot/grub/grub.conf /tmp/ sed ‘s/^[[:space:]]+//‘ /tmp/grub.conf
效果:
[root@liam ~]# sed ‘s/^[[:space:]]\+//g‘ /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_liam-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.32-431.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_liam-lv_root rd_NO_LUKS rd_LVM_LV=vg_liam/lv_swap rd_LVM_LV=vg_liam/lv_root rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM LANG=en_US.UTF-8 rhgb quiet initrd /initramfs-2.6.32-431.el6.x86_64.img [root@liam ~]#
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
命令:
sed ‘s/^#[[:space:]]\+//g‘ /tmp/rc.sysinit
效果:
[root@liam ~]# sed ‘s/^#[[:space:]]\+//g‘ /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 modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb else mount -n -t usbfs /proc/bus/usb /proc/bus/usb fi #remount /dev/shm to set attributes from fstab #669700 mount -n -o remount /dev/shm >/dev/null 2>&1 #remount /proc to set attributes from fstab #984003 mount -n -o remount /proc >/dev/null 2>&1 . /etc/init.d/functions PLYMOUTH= [ -x /bin/plymouth ] && PLYMOUTH=yes Check SELinux status SELINUX_STATE= ..........
4、为/tmp/grub.conf文件中前三行的行首加#号;
命令:
sed ‘1,3s/^/#/‘ /tmp/grub.conf
效果:
[root@liam ~]# sed ‘1,3s/^/#/‘ /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;
命令:
sed -i ‘s/enabled=0/enabled=1/g;s/gpgcheck=0/gpgcheck=1/g‘ /etc/yum.repos.d/CentOS-Media.repo
效果:
[root@liam ~]# sed ‘s/enabled=0/enabled=1/g;s/gpgcheck=0/gpgcheck=1/g‘ /etc/yum.repos.d/CentOS-Media.repo # CentOS-Media.repo # # This repo can be used with mounted DVD media, verify the mount point for # CentOS-6. You can use this repo and yum to install items directly off the # DVD ISO that we release. # # To use this repo, put in your DVD and use it with the other repos too: # yum --enablerepo=c6-media [command] # # or for ONLY the media repo, do this: # # yum --disablerepo=\* --enablerepo=c6-media [command] [c6-media] name=CentOS-$releasever - Media baseurl=file:///media/CentOS/ file:///media/cdrom/ file:///media/cdrecorder/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [root@liam ~]#
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
[root@liam ~]# crontab -l 0 */4 * * * tar -cvf /bakcup/etc-`date +%Y%m%d%H%M%S`.tar /etc &>/dev/null [root@liam ~]#
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
[root@liam ~]# crontab -l 0 0 * * 2,4,6 tar -cvf /bakcup/messages_logs/messages-`date +%Y%m%d`.tar /var/log/messages &>/dev/null [root@liam ~]#
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
[root@liam ~]# crontab -l 0 0-23/2 * * * grep ^S /proc/meminfo >> /stats/memory.txt [root@liam ~]#
9、工作日的工作时间内,每两小时执行一次echo "howdy"
[root@liam ~]# crontab -l 0 9-17/2 * * 1-5 echo "howdy" [root@liam ~]#
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
#!/bin/bash # declare day=`date +%Y%m%d` mkdir /tmp/testdir-$day
11、在此目录创建100个空文件:file1-file100
#!/bin/bash # declare day=`date +%Y%m%d` mkdir /tmp/testdir-$day touch /tmp/testdir-$day/file{1..10}
12、显示/etc/passw d文件中位于第偶数行的用户的用户名;
[root@liam bin]# cat passwd2.sh #!/bin/bash # cat /etc/passwd | awk NR%2==0 | cut -d: -f1 [root@liam bin]# bash -x passwd2.sh + cut -d: -f1 + awk NR%2==0 + cat /etc/passwd bin adm sync halt uucp games ftp dbus vcsa rtkit abrt nfsnobody gdm apache postfix sshd [root@liam bin]#
13、创建10用户user10-user19;密码同用户名;
[root@liam bin]# cat useradd10-19.sh #!bin/bash # for i in {10..19}; do if id user$i &> /dev/null ; then echo "user$i exits" else useradd user$i if [ $? -eq 0 ] ; then echo user$i | passwd --stdin user$i &> /dev/null echo "user$i add finish" fi fi done [root@liam bin]#
14、在/tmp/创建10个空文件file10-file19;
#!/bin/bash touch /tmp/file{10..19}
15、把file10的属主和属组改为user10,依次类推。
[root@liam bin]# bash -n grant10-19.sh [root@liam bin]# [root@liam bin]# cat grant10-19.sh #!bin/bash # touch /tmp/file{10..19} for i in {10..19}; do if id user$i &> /dev/null ; then echo "user$i exits" else useradd user$i if [ $? -eq 0 ] ; then echo user$i | passwd --stdin user$i &> /dev/null echo "user$i add finish" chmod user$i:user$i /tmp/file$i &> dev/null echo "file%i grant to user$i susscess" fi fi done [root@liam bin]#
第六周作业