homework week06

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了homework week06相关的知识,希望对你有一定的参考价值。

本周作业内容:

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;

[[email protected] ~]# sed  ‘/^[[:space:]]\+/s/\(.*\)/#\1/‘ /etc/rc.d/rc.sysinit > /tmp/rc.sysinit

[[email protected] ~]# diff /tmp/rc.sysinit /etc/rc.d/rc.sysinit  #对比原文件
13c13
< #    . /etc/sysconfig/network
---
>     . /etc/sysconfig/network
16c16
< #    HOSTNAME=localhost
---
>     HOSTNAME=localhost
20,21c20,21
< #     mount -n -t proc /proc /proc
< #     mount -n -t sysfs /sys /sys >/dev/null 2>&1
---
>       mount -n -t proc /proc /proc
>       mount -n -t sysfs /sys /sys >/dev/null 2>&1
24c24
< #     modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
---
>       modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
26c26
< #     mount -n -t usbfs /proc/bus/usb /proc/bus/usb
---
>       mount -n -t usbfs /proc/bus/usb /proc/bus/usb
42,47c42,47
< #     if [ -r "/selinux/enforce" ] ; then
< #             SELINUX_STATE=$(cat "/selinux/enforce")
< #     else
< #             # assume enforcing if you can‘t read it
< #             SELINUX_STATE=1
< #     fi
---
>       if [ -r "/selinux/enforce" ] ; then
>               SELINUX_STATE=$(cat "/selinux/enforce")
>       else
>               # assume enforcing if you can‘t read it
>               SELINUX_STATE=1
>       fi

#####ommited####

661,667c661,667
< #  if strstr "$cmdline" netprofile= ; then
< #    for arg in $cmdline ; do
< #        if [ "${arg##netprofile=}" != "${arg}" ]; then
< #         /usr/sbin/system-config-network-cmd --profile ${arg##netprofile=}
< #        fi
< #    done
< #  fi
---
>   if strstr "$cmdline" netprofile= ; then
>     for arg in $cmdline ; do
>         if [ "${arg##netprofile=}" != "${arg}" ]; then
>           /usr/sbin/system-config-network-cmd --profile ${arg##netprofile=}
>         fi
>     done
>   fi
680c680
< #     touch /var/run/confirm
---
>       touch /var/run/confirm
685c685
< #    /bin/plymouth --sysinit
---
>     /bin/plymouth --sysinit


2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

[[email protected] ~]# sed ‘s/^[[:space:]]\+//‘ /boot/grub/grub.conf > /tmp/grub.conf

[[email protected] ~]# diff /boot/grub/grub.conf /tmp/grub.conf 
15,17c15,17
<       root (hd0,0)
<       kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet vga=0x340
<       initrd /initramfs-2.6.32-504.el6.x86_64.img
---
> root (hd0,0)
> kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet vga=0x340
> initrd /initramfs-2.6.32-504.el6.x86_64.img


3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行

[[email protected] ~]# sed  ‘/^#[[:space:]]\+/d‘ /tmp/rc.sysinit 
#!/bin/bash
#
#
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
fi
if [ ! -e /proc/mounts ]; then
fi
if [ ! -d /proc/bus/usb ]; then
else
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

###ommited###

[ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii
if strstr "$cmdline" confirm ; then
fi
if [ -x /bin/plymouth ]; then
fi


4、为/tmp/grub.conf文件中前三行的行首加#号;

[[email protected] ~]# sed ‘1,3s/^/#/‘ /tmp/grub.conf  | diff - /boot/grub/grub.conf 
1,3c1,3
< ## grub.conf generated by anaconda
< ##
< ## Note that you do not have to rerun grub after making changes to this file
---
> # grub.conf generated by anaconda
> #
> # Note that you do not have to rerun grub after making changes to this file
15,17c15,17
< root (hd0,0)
< kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet vga=0x340
< initrd /initramfs-2.6.32-504.el6.x86_64.img
---
>       root (hd0,0)
>       kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet vga=0x340
>       initrd /initramfs-2.6.32-504.el6.x86_64.img


5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;

[[email protected] ~]# sed ‘/\<gpgcheck\>\|\<enabled\>/s/0/1/‘ /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
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6


6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202

通过crontab -e命令,添加
* */4 * * * /home/shell/backupetc.sh

[[email protected] shell]# cat backupetc.sh 
#!/bin/bash
readonly bakupdir=/backup
if [ ! -d "$bakupdir" ]; then
        mkdir $bakupdir
fi
daybakupdir="${bakupdir}/etc-`date +"%Y%m%d%H%M"`"
if [ ! -d "$daybakupdir" ]; then
        mkdir "$daybakupdir"
fi
/bin/cp -ru /etc/* "$daybakupdir"


7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830

通过crontab -e命令,添加
0 0 * * 2,4,6 /home/shell/backuplogs.sh 

[[email protected] shell]# cat backuplogs.sh 
#!/bin/bash

readonly bakupdir=/backup/messages_logs
if [ ! -d "$bakupdir" ]; then
        mkdir -p $bakupdir
fi

/bin/cp /var/log/messages $bakupdir/messages-`date +%Y%m%d`


8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中

通过crontab -e命令,添加
0 */2 * * * grep ‘^S‘ /proc/meminfo > /stats/memory.txt


9、工作日的工作时间内,每两小时执行一次echo "howdy"

通过crontab -e命令,添加
0 */2 * * 1-5 echo "howdy"

# 查看邮件,有howdy输出
[[email protected] ~]# mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 2 messages 2 new
>N  1 Cron Daemon           Fri Sep  9 08:00  22/734   "Cron <[email protected]> echo "howdy""
 N  2 Cron Daemon           Fri Sep  9 10:00  22/734   "Cron <[email protected]> echo "howdy""
& 1  
Message  1:
From [email protected]  Fri Sep  9 08:00:01 2016
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <[email protected]> echo "howdy"
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Fri,  9 Sep 2016 08:00:01 +0800 (CST)
Status: R
howdy

& 2
Message  2:
From [email protected]  Fri Sep  9 10:00:02 2016
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <[email protected]> echo "howdy"
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Fri,  9 Sep 2016 10:00:02 +0800 (CST)
Status: R
howdy
& q
Held 2 messages in /var/spool/mail/root


脚本编程练习

10、创建目录/tmp/testdir-当前日期时间; 

11、在此目录创建100个空文件:file1-file100

12、显示/etc/passwd文件中位于第偶数行的用户的用户名;

13、创建10用户user10-user19;密码同用户名;

14、在/tmp/创建10个空文件file10-file19; 

15、把file10的属主和属组改为user10,依次类推。

[[email protected] shell]# cat practice1.sh 
#!/bin/bash

. /etc/init.d/functions

readonly pwdfile=/etc/passwd
readonly basedir=/tmp
datadir=""

function task1()
{
        set $(date +"%F %T")
        local dvar=$1
        local tvar=$2
        datadir=${datadir:="testdir-$dvar $tvar"}
        
        echo "==> executing task1: creating directory $basedir/$datadir"
        
        mkdir -pv "$basedir/$datadir" >/dev/null 2>&1
        local ret=$?
        if [ "$ret" -ne 0 ]; then
                action "making directory $basedir/$datadir" /bin/false
                action "task1" /bin/false
        else
                action "task1" /bin/true
        fi
}

function task2()
{
        if [ ! -d "$basedir/$datadir" ]; then
                echo "the dirctory that we need is not there!"
                echo "task2 can not be exectued"
        else
                echo "==> executing task2: touching 100 empty files now"
                
                touch "$basedir/$datadir"/file{1..100} >/dev/null 2>&1
                local ret=$?
                if [ "$ret" -ne 0 ]; then
                        action "touching 100 empty files"  /bin/false
                        action "task2" /bin/false
                else
                        action "task2" /bin/true
                fi
        fi
}

function task3()
{
        echo "==> executing task3: display username in even rows of /etc/passwd file"
        
        awk -F‘:‘ ‘{if (NR % 2 == 0) print $1}‘ $pwdfile
        # or: sed -n ‘n;p‘ /etc/passwd
        action "task3" /bin/true
}

function task4()
{
        echo "==> executing task4: add 10 users, using the user name as their password"
        
        for user in user{10..19}
        do
                useradd $user >/dev/null 2>&1
                local ret=$?
                if [ "$ret" -eq 0 ]; then
                        action "add user $user" /bin/true
                        echo "$user" | passwd --stdin "$user" >/dev/null
                else
                        action "add user $user" /bin/false
                fi
        done
}

function task5()
{
        echo "==> executing task5: touching 10 empty files"
        
        touch "$basedir"/file{10..19}
        local ret=$?
        if [ "$ret" -ne 0 ]; then
                action "touching 10 empty files"  /bin/false
                action "task5" /bin/false
        else
                action "task5" /bin/true
        fi
}

function task6()
{
        echo "==> executing task6: chage ownership of files"
        local usrlist=(user{10..19})
        local grplist=(${usrlist[*]})
        local filelist=(file{10..19})
        local arrlen=$[${#filelist[@]}-1]
        local ret
        
        for i in `seq 0 1 $arrlen`
        do
                chown  ${usrlist[i]}:${grplist[i]} $basedir/${filelist[i]} >/dev/null 2>&1
                ret=$?
                if [ "$ret" -eq 0 ]; then
                        action "change ownship of file $basedir/${filelist[i]} to ${usrlist[i]}:${grplist[i]}" /bin/true
                else
                        action "change ownship of file $basedir/${filelist[i]} to ${usrlist[i]}:${grplist[i]}" /bin/false
                fi
        done
}

function main()
{
        task1
        task2
        task3
        task4
        task5
        task6
}

main


[[email protected] shell]# sh practice1.sh 
==> executing task1: creating directory /tmp/testdir-2016-09-11 08:49:36
task1 [  OK  ]
==> executing task2: touching 100 empty files now
task2 [  OK  ]
==> executing task3: display username in even rows of /etc/passwd file
bin
adm
sync
halt
uucp
games
ftp
dbus
rpc
rpcuser
haldaemon
saslauth
sshd
oprofile
dhcpd
icheck
named
mysql
slackware
MySQL
test1
hadoop
testbash
nologin
task3 [  OK  ]
==> executing task4: add 10 users, using the user name as their password
add user user10... [  OK  ]
add user user11... [  OK  ]
add user user12... [  OK  ]
add user user13... [  OK  ]
add user user14... [  OK  ]
add user user15... [  OK  ]
add user user16... [  OK  ]
add user user17... [  OK  ]
add user user18... [  OK  ]
add user user19... [  OK  ]
==> executing task5: touching 10 empty files
task5 [  OK  ]
==> executing task6: chage ownership of files
change ownship of file /tmp/file10 to user10:user10 [  OK  ]
change ownship of file /tmp/file11 to user11:user11 [  OK  ]
change ownship of file /tmp/file12 to user12:user12 [  OK  ]
change ownship of file /tmp/file13 to user13:user13 [  OK  ]
change ownship of file /tmp/file14 to user14:user14 [  OK  ]
change ownship of file /tmp/file15 to user15:user15 [  OK  ]
change ownship of file /tmp/file16 to user16:user16 [  OK  ]
change ownship of file /tmp/file17 to user17:user17 [  OK  ]
change ownship of file /tmp/file18 to user18:user18 [  OK  ]
change ownship of file /tmp/file19 to user19:user19 [  OK  ]


检查结果
# task1, task2
[[email protected] ~]# tree /tmp/testdir-2016-09-11\ 09\:02\:10/
/tmp/testdir-2016-09-11 09:02:10/
├── file1
├── file10
├── file100
├── file11
├── file12
├── file13
├── file14
├── file15
├── file16
├── file17
├── file18
├── file19
├── file2
###ommited###
├── file96
├── file97
├── file98
└── file99
0 directories, 100 files

#task3
[[email protected] ~]# cat -n /etc/passwd
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
### ommited ###
    44  hadoop:x:1001:1001::/home/hadoop:/bin/bash
    45  bash:x:3007:3007::/home/bash:/bin/bash
    46  testbash:x:3008:3008::/home/testbash:/bin/bash
    47  basher:x:3009:3009::/home/basher:/bin/bash
    48  nologin:x:3010:3010::/home/nologin:/sbin/nologin
    
#task4
[[email protected] ~]# tail -10 /etc/passwd
user10:x:3011:3011::/home/user10:/bin/bash
user11:x:3012:3012::/home/user11:/bin/bash
user12:x:3013:3013::/home/user12:/bin/bash
user13:x:3014:3014::/home/user13:/bin/bash
user14:x:3015:3015::/home/user14:/bin/bash
user15:x:3016:3016::/home/user15:/bin/bash
user16:x:3017:3017::/home/user16:/bin/bash
user17:x:3018:3018::/home/user17:/bin/bash
user18:x:3019:3019::/home/user18:/bin/bash
user19:x:3020:3020::/home/user19:/bin/bash

#task5, task6
[[email protected] ~]# ll /tmp/file{10..19}
-rw-r--r--. 1 user10 user10 0 Sep 11 08:49 /tmp/file10
-rw-r--r--. 1 user11 user11 0 Sep 11 08:49 /tmp/file11
-rw-r--r--. 1 user12 user12 0 Sep 11 08:49 /tmp/file12
-rw-r--r--. 1 user13 user13 0 Sep 11 08:49 /tmp/file13
-rw-r--r--. 1 user14 user14 0 Sep 11 08:49 /tmp/file14
-rw-r--r--. 1 user15 user15 0 Sep 11 08:49 /tmp/file15
-rw-r--r--. 1 user16 user16 0 Sep 11 08:49 /tmp/file16
-rw-r--r--. 1 user17 user17 0 Sep 11 08:49 /tmp/file17
-rw-r--r--. 1 user18 user18 0 Sep 11 08:49 /tmp/file18
-rw-r--r--. 1 user19 user19 0 Sep 11 08:49 /tmp/file19





本文出自 “睿宝宝的半熟芝士” 博客,谢绝转载!

以上是关于homework week06的主要内容,如果未能解决你的问题,请参考以下文章

homework week05

homework week04

homework week07

Python-Week2-Homework

Firs week-Homework(三级菜单)

homework week08