码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Basic 6:vim及一些的Shell脚本

时间:2016-09-06 23:21:00      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:linuxbasic

本周作业内容:

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

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

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

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

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

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

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

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

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

脚本编程练习

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

# tt=`date ‘+%Y-%m-%d_%H:%M‘` && mkdir "/tmp/testdir-$tt" && ls -d !$
/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
#




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


Linux Basic 6:vim及一些的Shell脚本

标签:linuxbasic

原文地址:http://wanghu2050.blog.51cto.com/5461349/1846967

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!