linux 网络班怎么样,马哥Linux网络班作业(6)(示例代码)

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

答:[[email protected] ~]# cp -a /etc/rc.d/rc.sysinit /tmp

[[email protected] ~]# vim /tmp/rc.sysinit

## 进入末行模式编辑如下命令

:%s/^[[:space:]]/#&/

9fb0e920cf7e4284b27fb64c71abb1d0.png

00c41e9ee5603856fbb4a0ffc7258e03.png

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

答:[[email protected] ~]# cp -a /boot/grub/grub.conf /tmp

[[email protected] ~]# vim /tmp/grub.conf

## 进入末行模式编辑如下命令

:%s/^[[:space:]]\+//

dbf98ec7300d0401d34f22cba1a9a416.png

94eec829d60b56c0cd339a60d92d0ff6.png

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

答:[[email protected] ~]# vim /tmp/rc.sysinit

## 进入末行模式编辑如下命令

%s/^#[[:space:]]\+//

440c3cbeb9ae2b24a32a981343bfb079.png

597b56f8817142c47ed4383b33efec64.png

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

答:[[email protected] ~]# vim /tmp/grub.conf

## 进入末行模式编辑如下命令

:1,3s/^/#/

b2345d60d64ac1880a2dccb8f7102a0f.png

6bb849bb93aba1cab009b9db2e017b93.png

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

答:[[email protected] ~]# vim /etc/yum.repos.d/CentOS-Media.repo

## 进入末行模式编辑如下命令

:%s/enabled=0/enabled=1

:%s/gpgcheck=1/gpcheck=0 ##  默认gpgcheck=1,这里改为0

1ab2af47b81c8c10a06d631a75cdfb92.png

33c882ba739bc8adc19ac340cae9e250.png

be11b7c03d360f138da47523d72d66e8.png

7b42efe3dc3e71f1e9ff6d30cf3f2882.png

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

答:## 利用crontab命令,定义计划任务,用-e编辑新的计划任务,-l查询已经定义的计划任务

[[email protected] ~]# crontab -e

0 */4 * * * cp -rf /etc /backup/etc-`date+%Y%m%d%H%M`

[[email protected] ~]# crontab -l

0 */4 * * * cp -rf /etc /backup/etc-`date+%Y%m%d%H%M`

## 其中 * * * * * 五颗星分别代表分,时,日,月,周

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

答:## 同6题一样,利用crontab命令来创建

[[email protected] ~]# crontab -e

* 00 * * 2,4,6 cp -rf /var/log/messages /backup/message-`date +%Y%m%d%H`

[[email protected] ~]# crontab -l

0 */4 * * * cp -rf /etc /backup/etc-‘date+%Y%m%d%H%M‘

* 00 * * 2,4,6 cp -rf /var/log/messages /backup/message-`date +%Y%m%d%H`

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

答:## 还是领crontab命令

[[email protected] ~]# crontab -e

1 */2 * * 1,2,3,4,5 grep "^S" /proc/meminfo >> /stats/memry.txt

[[email protected] ~]# crontab -l

1 */2 * * 1,2,3,4,5 grep "^S" /proc/meminfo >> /stats/memry.txt

9718140542bdd13ef3e353c4a9d4b43b.png

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

答:## 使用crontab命令

[[email protected] ~]# crontab -e

1 */2 * * 1,2,3,4,5 echo "howdy"

[[email protected] ~]# crontab -l

1 */2 * * 1,2,3,4,5 echo "howdy"

74cf99efd12d3100b395a84c6d9c63af.png

脚本编程练习

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

答:[[email protected] bin]# vim testdirdate.sh

#!/bin/bash

# testdirdate文件

mkdir -pv /tmp/testdir-`date +%Y%m%d%H%M`     ##ESC进入末行模式wq退出

[[email protected] bin]# chmod u+x testdirdate.sh    ##给脚本添加执行权限

[[email protected] bin]# ./testdirdate.sh            ##运行脚本

mkdir: 已创建目录 "/tmp/testdir-201609071346"

[[email protected] bin]# ll /tmp

总用量 36

-rw-------. 1 root root   831 9月   7 11:10 grub.conf

-rwxr-xr-x. 1 root root 20586 9月   7 10:06 rc.sysinit

drwx------. 2 root root  4096 9月   7 09:59 ssh-BOYIU19192

drwxr-xr-x. 2 root root  4096 9月   7 13:46 testdir-201609071346    ##查看执行结果

[[email protected] bin]#

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

答:[[email protected] bin]# vim fileNum.sh

#!/bin/bash

for i in {1..100};do

touch /tmp/testdir-201609071346/file$i

done

##ESC进入末行模式wq退出

[[email protected] bin]# chmod u+x fileNum.sh          ## 添加脚本执行权限

[[email protected] bin]# ./fileNum.sh                  ## 执行脚本

[[email protected] bin]# ls /tmp/testdir-201609071346/

file1    file18  file27  file36  file45  file54  file63  file72  file81  file90

file10   file19  file28  file37  file46  file55  file64  file73  file82  file91

file100  file2   file29  file38  file47  file56  file65  file74  file83  file92

file11   file20  file3   file39  file48  file57  file66  file75  file84  file93

file12   file21  file30  file4   file49  file58  file67  file76  file85  file94

file13   file22  file31  file40  file5   file59  file68  file77  file86  file95

file14   file23  file32  file41  file50  file6   file69  file78  file87  file96

file15   file24  file33  file42  file51  file60  file7   file79  file88  file97

file16   file25  file34  file43  file52  file61  file70  file8   file89  file98

file17   file26  file35  file44  file53  file62  file71  file80  file9   file99

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

答:[[email protected] bin]# vim echopwd.sh

#!/bin/bash

sed -n ‘n;p‘ /etc/passwd | cut -d: -f1

##ESC进入末行模式wq退出

[[email protected] bin]# chmod u+x echopwd.sh          ## 添加脚本执行权限

[[email protected] bin]# ./echopwd.sh                  ## 执行脚本

bin

adm

sync

halt

uucp

games

ftp

dbus

vcsa

rpcuser

haldaemon

saslauth

sshd

mysql

oprofile

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

答:[[email protected] bin]# vim adduser.sh

#!/bin/bash

for i in {10..19};do

useradd user$i

echo "user$i" | passwd --stdin user$i

done

## ESC进入末行模式wq保存退出

[[email protected] bin]# bash adduser.sh        ##直接使用bash运行

更改用户 user10 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user11 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user12 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user13 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user14 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user15 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user16 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user17 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user18 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

更改用户 user19 的密码 。

passwd: 所有的身份验证令牌已经成功更新。

[[email protected] bin]#

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

答:[[email protected] bin]# vim filetmp.sh

#!/bin/bash

for i in {10..19};do

touch /tmp/file$i

done

## ESC进入末行模式wq退出

[[email protected] bin]# bash filetmp.sh    ##使用bash直接运行

[[email protected] bin]# ls /tmp            ##产看运行结果

file10  file13  file16  file19      ssh-BOYIU19192        testdir-201609071404

file11  file14  file17  grub.conf   ssh-kyZlD20011        user.txt

file12  file15  file18  rc.sysinit  testdir-201609071346

[[email protected] bin]#

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

答:[[email protected] bin]# vim chownfile.sh

#!/bin/bash

for i in {10..19};do

chown user$i:user$i /tmp/file$i

done

## ESC进入末行模式wq保存退出

[[email protected] bin]# bash chownfile.sh            ## 使用bash直接运行

[[email protected] bin]# ll /tmp/                    ##查看运行结果

总用量 44

-rw-r--r--. 1 user10 user10     0 9月   7 14:43 file10

-rw-r--r--. 1 user11 user11     0 9月   7 14:43 file11

-rw-r--r--. 1 user12 user12     0 9月   7 14:43 file12

-rw-r--r--. 1 user13 user13     0 9月   7 14:43 file13

-rw-r--r--. 1 user14 user14     0 9月   7 14:43 file14

-rw-r--r--. 1 user15 user15     0 9月   7 14:43 file15

-rw-r--r--. 1 user16 user16     0 9月   7 14:43 file16

-rw-r--r--. 1 user17 user17     0 9月   7 14:43 file17

-rw-r--r--. 1 user18 user18     0 9月   7 14:43 file18

-rw-r--r--. 1 user19 user19     0 9月   7 14:43 file19

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值