Linux Basic 6:vim,sed,for語句

第六週作業內容:

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

# cp /etc/rc.d/rc.sysinit /tmp
# vim /tmp/rc.sysinit
vim末行模式,輸入
:%s/^[[:space:]]/#&/g

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

# cp /boot/grub/grub.conf /tmp
# sed 's@^[[:space:]]\+@@' /etc/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_mycentosl68-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-642.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/vg_mycentosl68-lv_root rd_NO_LUKS rd_LVM_LV=vg_mycentosl68/lv_swap crashkernel=auto rd_NO_MD rd_LVM_LV=vg_mycentosl68/lv_root LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-642.el6.x86_64.img

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

# sed 's/^#[[:space:]]\+//' /tmp/rc.sysinit

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

# vim /tmp/grub.conf
vim末行模式,輸入
:1,+2s/^/#&/g
# sed -e "1,3s/^/#/" 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

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

# vim /etc/yum.repos.d/CentOS-Media.repo
末行模式,輸入
:/enabled=0/s/0/1/g
:/gpgcheck=0/s/0/1/g

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

# crontab -e
0 */4 * * * /bin/cp -r /etc /backup/etc-'date +%Y%m%d%H%M'/

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

# crontab -e
0 0 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_logs/messages-$(date '+%Y%m%d')

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

# mkdir /stats && touch /stats/memory.txt
# crontab -e
0 */2 * * * /bin/grep '^S'/proc/meminfo >> /stats/memory.txt

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

# crontab -e
0 9-18/2 * * 1-5 /bin/echo "howdy"

腳本編程練習

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

# vim /temp/testmkdir.sh
#!/bin/bash
#
tt=`date '+%Y-%m-%d_%H:%M'` && mkdir "/tmp/testdir-$tt" && ls -d /tmp/testdir*
# bash /temp/testmkdir.sh
/tmp/testdir-2016-09-02_19:38:29

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

# vim /tmp/test11.sh
#!/bin/bash
#
for n in {1..100};do
    [ -d "/tmp/file$n" ] &> /dev/null || mkdir "/tmp/file$n"
done
# bash /tmp/test11.sh
# ls -d /tmp/file*
/tmp/file1    /tmp/file18  /tmp/file27  /tmp/file36  /tmp/file45  /tmp/file54 
                                       /tmp/file63  /tmp/file72  /tmp/file81  /tmp/file90
/tmp/file10   /tmp/file19  /tmp/file28  /tmp/file37  /tmp/file46  /tmp/file55 
                                       /tmp/file64  /tmp/file73  /tmp/file82  /tmp/file91
/tmp/file100  /tmp/file2   /tmp/file29  /tmp/file38  /tmp/file47  /tmp/file56  
                                       /tmp/file65  /tmp/file74  /tmp/file83  /tmp/file92
/tmp/file11   /tmp/file20  /tmp/file3   /tmp/file39  /tmp/file48  /tmp/file57
                                       /tmp/file66  /tmp/file75  /tmp/file84  /tmp/file93
/tmp/file12   /tmp/file21  /tmp/file30  /tmp/file4   /tmp/file49  /tmp/file58 
                                       /tmp/file67  /tmp/file76  /tmp/file85  /tmp/file94
/tmp/file13   /tmp/file22  /tmp/file31  /tmp/file40  /tmp/file5   /tmp/file59 
                                       /tmp/file68  /tmp/file77  /tmp/file86  /tmp/file95
/tmp/file14   /tmp/file23  /tmp/file32  /tmp/file41  /tmp/file50  /tmp/file6
                                       /tmp/file69  /tmp/file78  /tmp/file87  /tmp/file96
/tmp/file15   /tmp/file24  /tmp/file33  /tmp/file42  /tmp/file51  /tmp/file60
                                       /tmp/file7   /tmp/file79  /tmp/file88  /tmp/file97
/tmp/file16   /tmp/file25  /tmp/file34  /tmp/file43  /tmp/file52  /tmp/file61
                                       /tmp/file70  /tmp/file8   /tmp/file89  /tmp/file98
/tmp/file17   /tmp/file26  /tmp/file35  /tmp/file44  /tmp/file53  /tmp/file62
                                       /tmp/file71  /tmp/file80  /tmp/file9   /tmp/file99

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

# vim /tmp/test12.sh
#!/bin/bash
#
sed -n 'p;n' /etc/passwd | cut -d: -f1
# bash !$
root
daemon
lp
shutdown
mail
operator
gopher
nobody
usbmuxd
rtkit
vcsa
rpcuser
haldaemon
apache
postfix
sshd
admin
slackware
hadoop
abc
asdd
gentoo
testbash
nologin
user1
user98
user11
user13
user15
user17
user19

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

# vim /tmp/test13.sh
#!/bin/bash
#
for x in {10..19};do
    if ! id user$x &> /dev/null;then
        useradd user$x   
    fi
    echo "user$x" | passwd --stdin user$x &> /dev/null
done
# bash /tmp/test13.sh
# grep "^user1[0-9]" /etc/passwd
user1:x:3016:3018::/home/user1:/bin/bash
user90:x:3017:3030::/home/user90:/bin/bash
user98:x:3018:3031::/home/user98:/bin/bash
user10:x:3019:3020::/home/user10:/bin/bash
user11:x:3020:3021::/home/user11:/bin/bash
user12:x:3021:3022::/home/user12:/bin/bash
user13:x:3022:3032::/home/user13:/bin/bash
user14:x:3023:3023::/home/user14:/bin/bash
user15:x:3024:3024::/home/user15:/bin/bash
user16:x:3025:3025::/home/user16:/bin/bash
user17:x:3026:3026::/home/user17:/bin/bash
user18:x:3027:3027::/home/user18:/bin/bash
user19:x:3028:3028::/home/user19:/bin/bash
# grep "^user1[0-9]" /etc/shadow |cut -d: -f1,2
user10:$6$pTZJNXMe$K8/wlDIBuHlXDmMZV89FWCNXCb3lpgbDqNYeZ19xReO3MwVaCxVEUCsojbSEzOqpbkTh1wUZQ39Yy$
user11:$6$t/gEqH.L$4g2MczV22cWfVD03z7rV7g8dmk/u8DKODAU5XmOVYwDMGFypo3zN8xHzJjcaE0oxdCayi/9EKikgF$
user12:$6$apvOHbyV$7qWYd7R.KJ4zRyNUEjcLFzrECKsdEiWVP1PaqzdBkoMdPQiwoT2u1mRq8KPYIN4xDvDTpJiz/Efyl$
user13:$6$kv8uPAa9$DGvK5hhp4xVr.nPVTI1mAMzYRvoEwxXef1HLaStQnshq9x3jUrBJTgLLTbOCjy2Zxa43XxgpWebwc$
user14:$6$1R4cY8Be$mKfSf3aFBHIh2W4JgzjT5Iio2Ge2Y00fv5/oEkx/6QwO/P95fhKP70RxRiQil9/bKCSI1gH7YooCX$
user15:$6$B0uStd5w$Br4EjVrpLImx.Emv4Y8IH.vg.uf9.t4tzNaiQJYoHSQJvSaFpZ/ygncON4qXK8U2wp/rhElnlh8j5$
user16:$6$WF3ODFsZ$rk6.dR79P/wCPwq45k1C.m5WJrSosrcytL50eh.1uCXl..joir/zVwY5BkyeV6Iujonhw7LW0Oh34$
user17:$6$QMSzh8Wr$qLTNqaK/viIytlZ6OgdhFgdYzVkZCHgcviFwt8w6ILnCNchwgqEO8G/lj.P462yfGCVnU2OxcBXjy$
user18:$6$qgjPui4i$1qrveMxEBo3oLXWGWuWSnUUMp0S0IVGdDWwsXWUdI9lvnGvyD6vKXa9051qXyuE0CaRcUtSnhbVsp$
user19:$6$EhlB9BFU$BpOUPP8ghCsP.i8nLhSQEPzTxtgE2p/s9FgpeplS6G9myVjbJhpdWHcCPJTvr63iQkZo10l5lYEpi$

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

# vim /tmp/test14.sh
#!/bin/bash
#
for createfile in {10..19};do
    if [ ! -e "/tmp/file$createfile" ];then
       touch "/tmp/file$createfile"
    fi
done
ls -l /tmp/file1[0-9]
# bash /tmp/test14.sh
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file10
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file11
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file12
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file13
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file14
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file15
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file16
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file17
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file18
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file19

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

# vim /tmp/test15.sh
#!/bin/bash
#
for i in {10..19};do
    if ! grep "^user$i" /etc/group >& /dev/null;then
       groupadd "user$i"
    fi
    if ! id "user$i" &> /dev/null;then
       useradd -g user$i user$i
    fi
    chown user$i:user$i "/tmp/file$i"
done
[root@myCentOSl68 ~]# bash /tmp/test15.sh | ls -l /tmp/file1[0-9]
-rw-r--r--. 1 user10 user10 0 9月   2 16:58 /tmp/file10
-rw-r--r--. 1 user11 user11 0 9月   2 16:58 /tmp/file11
-rw-r--r--. 1 user12 user12 0 9月   2 16:58 /tmp/file12
-rw-r--r--. 1 user13 user13 0 9月   2 16:58 /tmp/file13
-rw-r--r--. 1 user14 user14 0 9月   2 16:58 /tmp/file14
-rw-r--r--. 1 user15 user15 0 9月   2 16:58 /tmp/file15
-rw-r--r--. 1 user16 user16 0 9月   2 16:58 /tmp/file16
-rw-r--r--. 1 user17 user17 0 9月   2 16:58 /tmp/file17
-rw-r--r--. 1 user18 user18 0 9月   2 16:58 /tmp/file18
-rw-r--r--. 1 user19 user19 0 9月   2 16:58 /tmp/file19


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