第六週作業

第六週作業

1、複製/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;

[root@STCO6 tmp]# cp /etc/rc.d/rc.sysinit /tmp
[root@STCO6 tmp]# sed -i 's@^[[:space:]]\+@#&@' rc.sysinit


2、複製/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;

[root@STCO6 tmp]# cp /boot/grub/grub.conf /tmp
[root@STCO6 tmp]# sed -i 's@^[[:space:]]\+@@' grub.conf


3、刪除/tmp/rc.sysinit文件中的以#開頭,且後面跟了至少一個空白字符的行行的#和空白字符

[root@STCO6 tmp]# sed -i 's@^#[[:space:]]\+@@' rc.sysinit


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

[root@STCO6 tmp]# sed -i '1,3s@.*@#&@' grub.conf

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

[root@STCO6 tmp]# sed -i 's@enabled=0@enabled=1@g' /etc/yum.repos.d/CentOS-Media.repo
[root@STCO6 tmp]# sed -i 's@gpgcheck=0@gpgcheck=1@g' /etc/yum.repos.d/CentOS-Media.repo


6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名爲形如etc-201504020202

[root@STCO6 /]# crontab -e
0 */4 * * * cp -rf /etc /backup/etc-$(date +\%Y\%m\%d\%H\%M)

7、每週2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20150402

[root@STCO6 /]# crontab -e
0 0 * * 2,4,6 cp /var/log/messages /backup/messages_logs/messages-$(date +\%Y\%m\%d)


8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中

[root@STCO6 /]# crontab -e
0 */2 * * * grep -e '^S' /proc/meminfo >> /stats/memory.txt


9、工作日的工作時間內,每兩小時執行一次echo "howdy"

[root@STCO6 /]# crontab -e
0 */2 * * 1,2,3,4,5 echo "howdy"


腳本編程練習

10、創建目錄/tmp/testdir-當前日期時間;

[root@STCO6 /]# cat /tmp/testScript1 
#!/bin/bash
#
mkdir /tmp/testdir-$(date +\%Y\%m\%d\%H\%M)

11、在此目錄創建100個空文件:file1-file100

[root@STCO6 ~]# cat /tmp/testScript2
#!/bin/bash
#
for i in {1..100};do
    touch /tmp/testdir-201609092326/file$i
done
[root@STCO6 ~]# ll /tmp/testdir-201609092326/
total 0
-rw-r--r--. 1 root root 0 Sep 10 08:33 file1
-rw-r--r--. 1 root root 0 Sep 10 08:33 file10
-rw-r--r--. 1 root root 0 Sep 10 08:33 file100
-rw-r--r--. 1 root root 0 Sep 10 08:33 file11
-rw-r--r--. 1 root root 0 Sep 10 08:33 file12
-rw-r--r--. 1 root root 0 Sep 10 08:33 file13
-rw-r--r--. 1 root root 0 Sep 10 08:33 file14
-rw-r--r--. 1 root root 0 Sep 10 08:33 file15
-rw-r--r--. 1 root root 0 Sep 10 08:33 file16
-rw-r--r--. 1 root root 0 Sep 10 08:33 file17
-rw-r--r--. 1 root root 0 Sep 10 08:33 file18
-rw-r--r--. 1 root root 0 Sep 10 08:33 file19
-rw-r--r--. 1 root root 0 Sep 10 08:33 file2
-rw-r--r--. 1 root root 0 Sep 10 08:33 file20
-rw-r--r--. 1 root root 0 Sep 10 08:33 file21
-rw-r--r--. 1 root root 0 Sep 10 08:33 file22
-rw-r--r--. 1 root root 0 Sep 10 08:33 file23
-rw-r--r--. 1 root root 0 Sep 10 08:33 file24
-rw-r--r--. 1 root root 0 Sep 10 08:33 file25
-rw-r--r--. 1 root root 0 Sep 10 08:33 file26
-rw-r--r--. 1 root root 0 Sep 10 08:33 file27
-rw-r--r--. 1 root root 0 Sep 10 08:33 file28
-rw-r--r--. 1 root root 0 Sep 10 08:33 file29
-rw-r--r--. 1 root root 0 Sep 10 08:33 file3
-rw-r--r--. 1 root root 0 Sep 10 08:33 file30
-rw-r--r--. 1 root root 0 Sep 10 08:33 file31
-rw-r--r--. 1 root root 0 Sep 10 08:33 file32
-rw-r--r--. 1 root root 0 Sep 10 08:33 file33
-rw-r--r--. 1 root root 0 Sep 10 08:33 file34
-rw-r--r--. 1 root root 0 Sep 10 08:33 file35
-rw-r--r--. 1 root root 0 Sep 10 08:33 file36
-rw-r--r--. 1 root root 0 Sep 10 08:33 file37
-rw-r--r--. 1 root root 0 Sep 10 08:33 file38
-rw-r--r--. 1 root root 0 Sep 10 08:33 file39
-rw-r--r--. 1 root root 0 Sep 10 08:33 file4
-rw-r--r--. 1 root root 0 Sep 10 08:33 file40
-rw-r--r--. 1 root root 0 Sep 10 08:33 file41
-rw-r--r--. 1 root root 0 Sep 10 08:33 file42
-rw-r--r--. 1 root root 0 Sep 10 08:33 file43
-rw-r--r--. 1 root root 0 Sep 10 08:33 file44
-rw-r--r--. 1 root root 0 Sep 10 08:33 file45
-rw-r--r--. 1 root root 0 Sep 10 08:33 file46
-rw-r--r--. 1 root root 0 Sep 10 08:33 file47
-rw-r--r--. 1 root root 0 Sep 10 08:33 file48
-rw-r--r--. 1 root root 0 Sep 10 08:33 file49
-rw-r--r--. 1 root root 0 Sep 10 08:33 file5
-rw-r--r--. 1 root root 0 Sep 10 08:33 file50
-rw-r--r--. 1 root root 0 Sep 10 08:33 file51
-rw-r--r--. 1 root root 0 Sep 10 08:33 file52
-rw-r--r--. 1 root root 0 Sep 10 08:33 file53
-rw-r--r--. 1 root root 0 Sep 10 08:33 file54
-rw-r--r--. 1 root root 0 Sep 10 08:33 file55
-rw-r--r--. 1 root root 0 Sep 10 08:33 file56
-rw-r--r--. 1 root root 0 Sep 10 08:33 file57
-rw-r--r--. 1 root root 0 Sep 10 08:33 file58
-rw-r--r--. 1 root root 0 Sep 10 08:33 file59
-rw-r--r--. 1 root root 0 Sep 10 08:33 file6
-rw-r--r--. 1 root root 0 Sep 10 08:33 file60
-rw-r--r--. 1 root root 0 Sep 10 08:33 file61
-rw-r--r--. 1 root root 0 Sep 10 08:33 file62
-rw-r--r--. 1 root root 0 Sep 10 08:33 file63
-rw-r--r--. 1 root root 0 Sep 10 08:33 file64
-rw-r--r--. 1 root root 0 Sep 10 08:33 file65
-rw-r--r--. 1 root root 0 Sep 10 08:33 file66
-rw-r--r--. 1 root root 0 Sep 10 08:33 file67
-rw-r--r--. 1 root root 0 Sep 10 08:33 file68
-rw-r--r--. 1 root root 0 Sep 10 08:33 file69
-rw-r--r--. 1 root root 0 Sep 10 08:33 file7
-rw-r--r--. 1 root root 0 Sep 10 08:33 file70
-rw-r--r--. 1 root root 0 Sep 10 08:33 file71
-rw-r--r--. 1 root root 0 Sep 10 08:33 file72
-rw-r--r--. 1 root root 0 Sep 10 08:33 file73
-rw-r--r--. 1 root root 0 Sep 10 08:33 file74
-rw-r--r--. 1 root root 0 Sep 10 08:33 file75
-rw-r--r--. 1 root root 0 Sep 10 08:33 file76
-rw-r--r--. 1 root root 0 Sep 10 08:33 file77
-rw-r--r--. 1 root root 0 Sep 10 08:33 file78
-rw-r--r--. 1 root root 0 Sep 10 08:33 file79
-rw-r--r--. 1 root root 0 Sep 10 08:33 file8
-rw-r--r--. 1 root root 0 Sep 10 08:33 file80
-rw-r--r--. 1 root root 0 Sep 10 08:33 file81
-rw-r--r--. 1 root root 0 Sep 10 08:33 file82
-rw-r--r--. 1 root root 0 Sep 10 08:33 file83
-rw-r--r--. 1 root root 0 Sep 10 08:33 file84
-rw-r--r--. 1 root root 0 Sep 10 08:33 file85
-rw-r--r--. 1 root root 0 Sep 10 08:33 file86
-rw-r--r--. 1 root root 0 Sep 10 08:33 file87
-rw-r--r--. 1 root root 0 Sep 10 08:33 file88
-rw-r--r--. 1 root root 0 Sep 10 08:33 file89
-rw-r--r--. 1 root root 0 Sep 10 08:33 file9
-rw-r--r--. 1 root root 0 Sep 10 08:33 file90
-rw-r--r--. 1 root root 0 Sep 10 08:33 file91
-rw-r--r--. 1 root root 0 Sep 10 08:33 file92
-rw-r--r--. 1 root root 0 Sep 10 08:33 file93
-rw-r--r--. 1 root root 0 Sep 10 08:33 file94
-rw-r--r--. 1 root root 0 Sep 10 08:33 file95
-rw-r--r--. 1 root root 0 Sep 10 08:33 file96
-rw-r--r--. 1 root root 0 Sep 10 08:33 file97
-rw-r--r--. 1 root root 0 Sep 10 08:33 file98
-rw-r--r--. 1 root root 0 Sep 10 08:33 file99


12、顯示/etc/passwd文件中位於第偶數行的用戶的用戶名;

[root@STCO6 ~]# cat /tmp/testScript3
#!/bin/bash
#
sed -n 'n;p' /etc/passwd | cut -d: -f1 >> /tmp/evenuser
[root@STCO6 ~]# cat /tmp/evenuser 
bin
adm
sync
halt
uucp
games
ftp
dbus
rpc
avahi-autoipd
abrt
nfsnobody
ntp
saslauth
gdm
sshd
suyi
testuser1
testuser3
testuser5
slackware
mysql
bash
basher
fedora

13、創建10用戶user10-user19;密碼同用戶名;

[root@STCO6 ~]# cat /tmp/testScript4
#!/bin/bash
#
for i in {10..19};do
    if id user$i &> /dev/null;then
    echo "user$i exists"
    else
        useradd user$i
        echo "user$i" | passwd --stdin user$i
    fi
done


[root@STCO6 ~]# chmod +x /tmp/testScript4
[root@STCO6 ~]# bash /tmp/testScript4
Changing password for user user10.
passwd: all authentication tokens updated successfully.
Changing password for user user11.
passwd: all authentication tokens updated successfully.
Changing password for user user12.
passwd: all authentication tokens updated successfully.
Changing password for user user13.
passwd: all authentication tokens updated successfully.
Changing password for user user14.
passwd: all authentication tokens updated successfully.
Changing password for user user15.
passwd: all authentication tokens updated successfully.
Changing password for user user16.
passwd: all authentication tokens updated successfully.
Changing password for user user17.
passwd: all authentication tokens updated successfully.
Changing password for user user18.
passwd: all authentication tokens updated successfully.
Changing password for user user19.
passwd: all authentication tokens updated successfully.
[root@STCO6 ~]# tail -n 10 /etc/passwd
user10:x:3010:3010::/home/user10:/bin/bash
user11:x:3011:3011::/home/user11:/bin/bash
user12:x:3012:3012::/home/user12:/bin/bash
user13:x:3013:3013::/home/user13:/bin/bash
user14:x:3014:3014::/home/user14:/bin/bash
user15:x:3015:3015::/home/user15:/bin/bash
user16:x:3016:3016::/home/user16:/bin/bash
user17:x:3017:3017::/home/user17:/bin/bash
user18:x:3018:3018::/home/user18:/bin/bash
user19:x:3019:3019::/home/user19:/bin/bash

14、在/tmp/創建10個空文件file10-file19;

[root@STCO6 ~]# cat /tmp/testScript5
#!/bin/bash
#
for i in {10..19};do
    if [ -e /tmp/file$i ];then
        echo "file$i exists"
    else
        touch /tmp/file$i
    fi
done
[root@STCO6 ~]# ll /tmp/file*
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file10
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file11
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file12
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file13
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file14
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file15
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file16
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file17
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file18
-rw-r--r--. 1 root root 0 Sep 10 10:06 /tmp/file19

15、把file10的屬主和屬組改爲user10,依次類推。

[root@STCO6 ~]# cat /tmp/testScript6
#!/bin/bash
#
for i in {10..19};do
    if [ -e /tmp/file$i ];then
        chown user$i:user$i /tmp/file$i
    else
        echo "file$i no exists"
    fi
done


[root@STCO6 ~]# ll /tmp/file*
-rw-r--r--. 1 user10 user10 0 Sep 10 10:06 /tmp/file10
-rw-r--r--. 1 user11 user11 0 Sep 10 10:06 /tmp/file11
-rw-r--r--. 1 user12 user12 0 Sep 10 10:06 /tmp/file12
-rw-r--r--. 1 user13 user13 0 Sep 10 10:06 /tmp/file13
-rw-r--r--. 1 user14 user14 0 Sep 10 10:06 /tmp/file14
-rw-r--r--. 1 user15 user15 0 Sep 10 10:06 /tmp/file15
-rw-r--r--. 1 user16 user16 0 Sep 10 10:06 /tmp/file16
-rw-r--r--. 1 user17 user17 0 Sep 10 10:06 /tmp/file17
-rw-r--r--. 1 user18 user18 0 Sep 10 10:06 /tmp/file18
-rw-r--r--. 1 user19 user19 0 Sep 10 10:06 /tmp/file19


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章