第六周博客作業

              

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

%s@^[[:space:]]\+[^[:space:]]@#&@g

    原文件:

2016-09-22 18_59_43-CentOS 6.8 - root@localhost_~ - Xshell 5 (Free for Home_School).png

    替換后的文件:

2016-09-22 18_58_54-CentOS 6.8 - root@localhost_~ - Xshell 5 (Free for Home_School).png

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

%s@^[[:space:]]\+@@g

    原文件:

2_1.png

    查找替換后的文件:

2_2.png

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

%s@^#[[:space:]]\+@@g

    原文件

3_1.png

    替換查找后的文件

3_2.png

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

:1,3s@.*@#&@

    原文件

5_1.png

    替換后的文件

5_2.png


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

%s@\(enabled=0|gpgcheck=0\)=0@\1=1@g

 

  原文件

4_1.png

    替換查找后的文件:

4_2.png

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

* */4 * * * /bin/cp –r /etc /backup/etc-$(date +%Y%m%d)

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

* * * * 2,4,6 /bin/cp -f /var/log/messages /backup/messages_logs/messages-$(date Y%m%d)

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

* */2 */1 * * /bin/grep "^S" /proc/meminfo >> /stats/memory.txt

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

* 9-18/2 * * 1-5 /bin/echo "howdy"

    列出上述創建的4個周期性計劃任務

[root@localhost backup]# crontab -e
crontab: installing new crontab
[root@localhost backup]# crontab -l
* 9-18/2 * * 1-5 /bin/echo "howdy"
* */2 */1 * * /bin/grep "^S" /proc/meminfo >> /stats/memory.txt
* */4 * * * /bin/cp -r /etc/ /backup/etc-$(date +%Y%m%d)
* * * * 2,4,6 /bin/cp -rf /var/log/messages /backup/messages_logs/messages-$(date +%Y%m%d)

腳本編程練習

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

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

    腳本文件

#!/bin/bash
# Author: Max Meng
# Date: 2016/09/22
# Summary:
#       10. Create a directory in /tmp: such as /tmp/testdir-20169010123030
#       11. Create 100 empty file: file1 - file100

# Create direcotry using date command
dirname="test-$(date +%Y%m%d%H%M%S)"
mkdir /tmp/$dirname

# If directory created successfully, then trying to create file1 to file100
if [ -e /tmp/$dirname ]; then
        for i in {1..100}; do
                touch /tmp/$dirname/file$i
        done
        echo "Create file1 to file100 finished"
else
        echo "create dirname failure"
        exit 1
fi

    執行結果

[root@localhost week06]# ls /tmp/test-20160923061932/
file1    file14  file2   file25  file30  file36  file41  file47  file52  file58  file63 
 file69  file74  file8   file85  file90  file96
file10   file15  file20  file26  file31  file37  file42  file48  file53  file59  file64  
file7   file75  file80  file86  file91  file97
file100  file16  file21  file27  file32  file38  file43  file49  file54  file6   file65  
file70  file76  file81  file87  file92  file98
file11   file17  file22  file28  file33  file39  file44  file5   file55  file60  file66  
file71  file77  file82  file88  file93  file99
file12   file18  file23  file29  file34  file4   file45  file50  file56  file61  file67  
file72  file78  file83  file89  file94
file13   file19  file24  file3   file35  file40  file46  file51  file57  file62  file68  
file73  file79  file84  file9   file95

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

    腳本文件

#!/bin/bash
#
sed -n 2~2p /etc/passwd | cut -d: -f1

    執行結果

[root@www week06]# bash sh14.sh 
bin
adm
sync
halt
operator
ftp
pegasus
ods
systemd-network
polkitd
apache
colord
unbound
saslauth
amandabackup
geoclue
rpc
rtkit
qemu
rpcuser
radvd
mysql
pulse
tomcat
gdm
avahi
sshd
dovenull
tcpdump
user1
hbase
max
user22
fedora
user20
user10
user12
user14
user16
user18


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

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

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

  腳本文件

#!/bin/bash
# Author: Max Meng
# Date: 2016/09/22

for i in {10..19}; do
        username=user$i
        if id $username >& /dev/null; then
                echo "user already exists"
        else
#               add user using  $username
                useradd $username
                echo $username | passwd --stdin $username >& /dev/null
                echo "add user $username successfully, the password is same as username"

                filename=file$i

#               Create empty file using $filename
                touch /tmp/$filename

#               Change file owner and file group of $filename equavilant to $username
                chown $username:$username /tmp/$filename
                echo "Create $filename successfully"

        fi
done

    執行結果

[root@www week06]# bash sh13.sh 
add user user10 successfully, the password is same as username
Create file10 successfully
add user user11 successfully, the password is same as username
Create file11 successfully
add user user12 successfully, the password is same as username
Create file12 successfully
add user user13 successfully, the password is same as username
Create file13 successfully
add user user14 successfully, the password is same as username
Create file14 successfully
add user user15 successfully, the password is same as username
Create file15 successfully
add user user16 successfully, the password is same as username
Create file16 successfully
add user user17 successfully, the password is same as username
Create file17 successfully
add user user18 successfully, the password is same as username
Create file18 successfully
add user user19 successfully, the password is same as username
Create file19 successfully
[root@www week06]# cat /etc/passwd | grep user1
user10:x:1024:1024::/home/user10:/bin/bash
user11:x:1025:1025::/home/user11:/bin/bash
user12:x:1026:1026::/home/user12:/bin/bash
user13:x:1027:1027::/home/user13:/bin/bash
user14:x:1028:1028::/home/user14:/bin/bash
user15:x:1029:1029::/home/user15:/bin/bash
user16:x:1030:1030::/home/user16:/bin/bash
user17:x:1031:1031::/home/user17:/bin/bash
user18:x:1032:1032::/home/user18:/bin/bash
user19:x:1033:1033::/home/user19:/bin/bash
[root@www week06]# ll /tmp | grep file1
-rw-r--r--. 1 user10       user10              0 Sep 23 09:30 file10
-rw-r--r--. 1 user11       user11              0 Sep 23 09:30 file11
-rw-r--r--. 1 user12       user12              0 Sep 23 09:30 file12
-rw-r--r--. 1 user13       user13              0 Sep 23 09:30 file13
-rw-r--r--. 1 user14       user14              0 Sep 23 09:30 file14
-rw-r--r--. 1 user15       user15              0 Sep 23 09:30 file15
-rw-r--r--. 1 user16       user16              0 Sep 23 09:30 file16
-rw-r--r--. 1 user17       user17              0 Sep 23 09:30 file17
-rw-r--r--. 1 user18       user18              0 Sep 23 09:30 file18
-rw-r--r--. 1 user19       user19              0 Sep 23 09:30 file19

原創文章,作者:mxb93,如若轉載,請注明出處:http://www.www58058.com/48458

(0)
mxb93mxb93
上一篇 2016-12-05 17:03
下一篇 2016-12-05 17:03

相關推薦

  • 馬哥教育網絡第21期-第1周課程練習

    第一周博客 1、描述計算機的組成及其功能。 計算機的組成:運算器、控制器、儲存器、輸入設備、輸出設備 運算器+控制器 = CPU 儲存器 = RAM(內存) I/O(輸入/輸出)設備 = 硬盤、打印機、鼠標、鍵盤、顯示器 CUP:運算和邏輯運算 儲存器:緩存和儲存數據 I/O設備:計算機與用戶交互的設備 2、按系列羅列Linux的發行版,并描述不同發行版之間…

    Linux干貨 2016-06-26
  • Linux下文件權限管理輕微總結

    Linux下文件權限管理輕微總結       此文對linux下文件的權限及其如何管理權限進行介紹,涉及到讀“r”寫“w”執行“x”三個權限,特殊權限SUID、SGID、STICKY、ACL、attr的使用。同時對更改文件的指令chmod、getfacl、setfacl、chattr、lsattr等進行說明。  &nb…

    Linux干貨 2016-11-07
  • LAMP 通過使用腳本的方式安裝并部署Discuz

    該腳本有很多輔助的部分,主要是為了讓腳本在中途運行失敗可以多次運行,所產生的結果是預期的,與第一運行并成功的結果是一樣的。初次攝入bash腳本有很多不足,敬請指正。 所需要的軟件 apr-1.5.2.tar.bz2 apr-util-1.5.4.tar.bz2 Discuz_X3.2_SC_UTF8.zip freetype-2.5.4.tar.gz htt…

    Linux干貨 2016-04-05
  • LAMP搭建wordpress博客站點

    使用WordPress可以搭建功能強大的網絡信息發布平臺,但更多的是應用于個性化的博客。針對博客的應用,WordPress能讓您省卻對后臺技術的擔心,集中精力做好網站的內容。本文將教您如何通過LAMP搭建自己的wordpress博客站點。 LAMP聽起來很高大上,其實是集中技術合起來的稱呼: L:linux A:apache httpd M:mysql、ma…

    2017-06-01
  • 點名腳本

    腳本要求:1、隨機抽點80以內的隨機證書;                2、可以一次抽取多個隨機數;同時間抽取的隨機數要唯一;                3、被抽取之后的隨機數,之后不會再抽??;…

    2017-05-08
  • N26 第三周作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@localhost ~]# useradd tom [root@localhost ~]# echo "123456" | passwd –st…

    Linux干貨 2017-01-20
欧美性久久久久