马哥教育网络班20期+第6周课程练习

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

[root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp
[root@localhost ~]# vim /tmp/rc.sysinit
按Esc进入vim的末行模式,并输入
:%s/^[[:space:]]/#&/g

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

[root@localhost ~]# cp /boot/grub/grub.conf /tmp
[root@localhost ~]# vim /tmp/grub.conf
按Esc进入vim的末行模式,并输入
:%s/^[[:space:]]\+//g

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

[root@localhost ~]# vim /tmp/rc.sysinit
按Esc进入vim的末行模式,并输入
:%s/^[[:space:]]\{0,\}#[[:space:]]\+//g

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

[root@localhost ~]# vim /tmp/grub.conf
按Esc进入vim的末行模式,并输入
:1,+2s/^/#&/g

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

[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Media.repo
按Esc进入vim的末行模式
:/enabled=0/s/0/1/g
:/gpgcheck=0/s/0/1/g
或
%s@enabled=0@enabled=1@g
%s@gpgcheck=0@gpgcheck=1@g

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

[root@localhost ~]# crontab -e
crontab: installing new crontab
* */4 * * * cp -r /etc/ /backup/etc-`date +%Y%m%d%H%M`
[root@localhost ~]# /etc/init.d/crond start

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

* * * * */2,4,6 cp -r /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`

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

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

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

* 9-17/2 * * */1-5 echo "howdy"

脚本编程练习

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

[root@localhost tmp]# vim create.sh
#!/bin/bash
mkdir -pv /tmp/testdir-$(date +%F-%H-%M-%S)
[root@localhost tmp]# bash -n create.sh
[root@localhost tmp]# bash create.sh
mkdir: 已创建目录 "/tmp/testdir-2016-07-13-16-12-37"

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

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

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

[root@localhost tmp]# sed -n 'p;n' /etc/passwd | cut -d: -f1

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

[root@localhost tmp]# vim usercreate.sh
#!/bin/bash
for i in {10..19};do
    if id user$i &>/dev/null;then
    echo "user$i exists."
    else
    useradd user$i
    if [ $? -eq 0 ];then
        echo "user$i" | passwd --stdin user$i &> /dev/null
        echo "Add user$i finished."
    fi
    fi
done
[root@localhost tmp]# bash -n usercreate.sh
[root@localhost tmp]# bash usercreate.sh
Add user10 finished.
Add user11 finished.
Add user12 finished.
Add user13 finished.
Add user14 finished.
Add user15 finished.
Add user16 finished.
Add user17 finished.
Add user18 finished.
Add user19 finished.

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

[root@localhost tmp]# vim file.sh
#!/bin/bash
for i in {10..19};do
    mkdir /tmp/file$i
done
[root@localhost tmp]# bash file.sh
[root@localhost tmp]# ls | grep file
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file.sh

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

[root@localhost tmp]# vim file1.sh
#!/bin/bash
cd /tmp
for i in {10..19};do
    chown user$i:user$i /tmp/file$i
done
[root@localhost tmp]# bash -n file1.sh
[root@localhost tmp]# bash file1.sh
[root@localhost tmp]# ll | grep file
drwxr-xr-x. 2 user10 user10     4096 7月  13 16:38 file10
drwxr-xr-x. 2 user11 user11     4096 7月  13 16:38 file11
drwxr-xr-x. 2 user12 user12     4096 7月  13 16:38 file12
drwxr-xr-x. 2 user13 user13     4096 7月  13 16:38 file13
drwxr-xr-x. 2 user14 user14     4096 7月  13 16:38 file14
drwxr-xr-x. 2 user15 user15     4096 7月  13 16:38 file15
drwxr-xr-x. 2 user16 user16     4096 7月  13 16:38 file16
drwxr-xr-x. 2 user17 user17     4096 7月  13 16:38 file17
drwxr-xr-x. 2 user18 user18     4096 7月  13 16:38 file18
drwxr-xr-x. 2 user19 user19     4096 7月  13 16:38 file19
-rw-r--r--. 1 root   root         77 7月  13 16:47 file1.sh
-rw-r--r--. 1 root   root         77 7月  13 16:46 file.sh

 

原创文章,作者:二极管,如若转载,请注明出处:http://www.178linux.com/24374

(0)
二极管二极管
上一篇 2016-07-16
下一篇 2016-07-16

相关推荐

  • 定时任务应用示例

    定时任务应用示例 1.每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202 mkdir /backup crontab -e * */4 * * * cp -ar /etc/ /backup/etc-$(date +%Y%m%d%H%M) 2.每周2,4,6备份/var/log/messages…

    Linux干货 2017-08-06
  • Linux软件管理

      **rpm** 查寻rpm -q httpd //精确查找已安装的包的全名rpm -qa |grep httpd //查找所有以httpd开头的rpm包rpm -ql httpd //查寻安装httpd包进生成的文件 rpm -f :查看置顶的文件由哪个程序包安装生成 rpm -p rpm file :针对尚未安装的程序包文件做查询操作 rpm…

    Linux笔记 2018-05-13
  • 文本处理章练习题

    2017.7.27练习 1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址 ifconfig |head -n 2 |tail -n 1|tr -s ” ” : |cut -d: -f4   2、查出分区空间使用率的最大百分比值 df|tr -s ‘ ‘ %|sort -t% -k5 …

    2017-07-29
  • 权限管理

    权限管理 在linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问这些文件和目录。 我们先来看看文件的属性: 权限: r:可获取文件数据(读取文件) w:可修改文件的数据(写入数据) x:可以把此文件提请内核启动为一个进程 (执行) 文件的权限主要针对三类对象进行定义:  owner: 属主, u  grou…

    Linux干货 2016-08-05
  • 初识Linux

                              初认识Linux 1.1 Linux的登录界面 在虚拟机安装CentOS 7中有提到,我在安装Linux操作系统的时候,采用的是命令行界面的登录方式,当然也有像Windows那样…

    Linux干货 2016-10-27
  • Apache配置压缩优化时报错——undefined symbol: inflateEnd

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1345264    圣诞都过了,好久没来51发博文了。最近一直在忙考试和其他一些私人事务,感觉长期不发博文,有点不好。不是不发,实在是最近…

    Linux干货 2016-08-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-07-16 22:35

    写的很好,排版也很漂亮,加油,每天的不要/1,每周的直接写就可以