Week six Homework
1. Copy the/etc/rc.d/rc.sysinit file to the/tmp directory and add # to the beginning of the line with at least one whitespace character in the/tmp/rc.sysinit file;
Command:
Cp/etc/rc.d/rc.sysinit/tmp/sed-i ' [Email protected]^[[:space:]]@# @g
Effect:
[[email protected] ~]# cp /etc/rc.d/rc.sysinit /tmp/[[email protected] ~]# ls /tmpkeyring-ifz3jm orbit-gdm orbit-root pulse-rspriwmzvy2z pulse-vmb549h8t0st rc.sysinit Virtual-root. jw5orz yum.log[[email protected] ~]# sed ' [email protected]^[[: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 -mif [ -f /etc/sysconfig/network ]; then# . /etc/sysconfig/networkfiif [ -z "$ HOSTNAME " -o $HOSTNAME" = "(none)" ]; then# Hostname=localhostfi
2, copy/boot/grub/grub.conf to/tmp directory, delete the blank character of the beginning of the/tmp/grub.conf file;
Command:
Cp/boot/grub/grub.conf/tmp/sed ' s/^[[:space:]]+//'/tmp/grub.conf
Effect:
[[email protected] ~]# 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/sdadefault=0timeout=5splashimage= (hd0,0)/grub/splash.xpm.gzhiddenmenutitle 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 quietinitrd /initramfs-2.6.32-431.el6.x86_64.img[[email protected] ~]#
3. Remove the # and white space characters from the/tmp/rc.sysinit file that begin with # and followed by at least one white-space character
Command:
Sed ' s/^#[[:space:]]\+//g '/tmp/rc.sysinit
Effect:
[[email protected] ~]# 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 -mif [ -f / etc/sysconfig/network ]; then . /etc/sysconfig/networkfiif [ - z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then hostname=localhostfiif [ ! -e /proc/mounts ]; then mount -n -t proc /proc /proc mount -n -t sysfs /sys /sys >/dev/null 2>&1fiif [ ! -d /proc/bus/usb ]; then modprobe usbcore >/dev/null 2≫&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usbelse mount -n -t usbfs /proc/bus/usb /proc/bus/ usbfi#remount /dev/shm to set attributes from fstab #669700mount -n -o remount /dev/shm >/dev/null 2>&1#remount /proc to set attributes from fstab #984003mount -n -o remount /proc >/dev/ null 2>&1. /etc/init.d/functionsplymouth=[ -x /bin/plymouth ] && plymouth=yescheck selinux statusselinux_state= .....
4. Add # to the beginning of the first three lines in the/tmp/grub.conf file;
Command:
Sed ' 1,3s/^/#/'/tmp/grub.conf
Effect:
[[Email protected] ~]# sed ' 1,3s/^/#/'/tmp/grub.conf## grub.conf generated by anaconda#### Erun Grub After making changes to this file# notice:you has a/boot partition. This means that# all kernel and initrd paths is relative to/boot/, eg.# root (hd0,0)
5. Change the last 0 of all enabled=0 or gpgcheck=0 in the/etc/yum.repos.d/centos-media.repo file to 1;
Command:
Sed-i ' s/enabled=0/enabled=1/g;s/gpgcheck=0/gpgcheck=1/g '/etc/yum.repos.d/centos-media.repo
Effect:
[[email protected] ~]# 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 - mediabaseurl=file:///media/ Centos/ file:///media/cdrom/ file:///media/ cdrecorder/gpgcheck=1enabled=1gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-6[[email protected] ~]#
6, every 4 hours to perform a backup of the/etc directory, back up to the/backup directory, save the directory named Shape etc-201608300202
[Email protected] ~]# crontab-l0 */4 * * * tar-cvf/bakcup/etc-' date +%y%m%d%h%m%s '. Tar/etc &>/dev/null[[em AIL protected] ~]#
7, weekly 2,4,6 backup/var/log/messages file to/backup/messages_logs/directory, save the file name like messages-20160830
[Email protected] ~]# crontab-l0 0 * * 2,4,6 tar-cvf/bakcup/messages_logs/messages-' date +%y%m%d '. tar/var/log/mess Ages &>/dev/null[[email protected] ~]#
8, every two hours every day to take the current system/proc/meminfo file in the beginning of all the information in the S./stats/memory.txt file
[[email protected] ~]# crontab-l0 0-23/2 * * * grep ^s/proc/meminfo >>/stats/memory.txt[[email protected] ~]#
9, working days of work time, every two hours to perform the echo "Howdy"
[Email protected] ~]# crontab-l0 9-17/2 * * 1-5 echo "Howdy" [[email protected] ~]#
Script Programming Exercises
10, create the directory/tmp/testdir-the current date and time;
#!/bin/bash#declare day= ' Date +%y%m%d ' mkdir/tmp/testdir-$day
11. Create 100 empty files in this directory: file1-file100
#!/bin/bash#declare day= ' Date +%y%m%d ' mkdir/tmp/testdir-$daytouch/tmp/testdir-$day/file{1..10}
12. Display the user name of the user who is located in the/ETC/PASSW D file in the first even row;
[email protected] bin]# Cat passwd2.sh#!/bin/bash#cat/etc/passwd | awk nr%2==0 | Cut-d:-f1[[email protected] bin]# bash-x passwd2.sh+ cut-d:-f1+ awk nr%2==0+ CAT/ETC/PASSWDBINADMSYNCHALTUUCPGAMESF Tpdbusvcsartkitabrtnfsnobodygdmapachepostfixsshd[[email protected] bin]#
13. Create 10 user user10-user19, password and user name;
[email protected] 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 fidone[[email protected] bin]#
14, create 10 empty files file10-file19 in/tmp/;
#!/bin/bashtouch/tmp/file{10..19}
15. Change the file10 and the genus to user10, and so on.
[[email protected] bin]# bash -n grant10-19.sh[[email protected] bin]#[[ Email protected] 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 fidone[[email protected] bin]#
Week six Homework