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

[root@centfils ~]# cp /etc/rc.d/rc.sysinit /tmp/ | sed -i '/^[[:space:]]\+/i\#' /tmp/rc.sysinit
[root@centfils ~]# cp /etc/rc.d/rc.sysinit /tmp/ | sed -i 's@^[[:space:]]\+@'#'&@g' /tmp/rc.sysinit#第一题用到了sed的行首追加,i,\n可实现多行追加,另一种是用替换后向引用

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

[root@centfils ~]# cp -i /boot/grub/grub.conf /tmp/ | sed -i '/^[[:space:]]\+/d' /tmp/grub.conf
[root@centfils ~]# cp -i /boot/grub/grub.conf /tmp/ | sed -i 's@^[[:space:]]\+@@g' /tmp/grub.conf
#第二题用到了sed的删除d,以及替换

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

[root@centfils ~]# sed -i '/^#[[:space:]]\+/d' /tmp/rc.sysinit
[root@centfils ~]# sed -i 's@^#[[:space:]]\+@@g' /tmp/rc.sysinit
#第三题用到了sed的删除d,以及替换

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

[root@centfils ~]# vim /tmp/grub.conf:1,3s/^/#/               #在vim末行模式中输入

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

[root@centfils ~]# vim /etc/yum.repos.d/CentOS-Media.repo:%s@=0@=1@g

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

[root@centfils ~]# which cp            #先查看cp命令的路径alias cp='cp -i'	/bin/cp[root@centfils ~]# crontab -l          #-e编辑,-l查看 下方编辑的命令最好使用绝对路径* */4 * * * /bin/cp /etc/ /backup/etc-$(date +%F%H%M%S)

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

[root@centfils ~]# crontab -ecrontab: installing new crontab[root@centfils ~]# crontab -l* */4 * * * /bin/cp /etc/ /backup/etc-$(date +%F%H%M%S)  * * * * */2 /bin/cp /var/log/messages /backup/messages_logs/messages-$(date +%F%H%M%S)

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

[root@centfils ~]# crontab -l* */2 */1 * * /bin/grep "^S" /proc/meminfo >> /stats/memory.txt

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

[root@centfils ~]# crontab -l* 10-19 * * 1-5 echo "howdy"      #每天10点到19点上班,没周一到周五上班

脚本编程练习

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

[root@centfils bin]# vim 10.sh#!/bin/bash#test=testdir-$(date +%F%H%M%S)mkdir /tmp/$test[root@centfils bin]# bash -n 10.sh[root@centfils bin]# bash -x 10.sh++ date +%F%H%M%S+ test=testdir-2016-09-06215248+ mkdir /tmp/testdir-2016-09-06215248

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

[root@centfils bin]# vim 11.sh#!/bin/bash#for i in {1..100}; do	touch file$i                    #touch为当前目录/root/bin,也可指明要创建的文件所在目录	echo "New file $i"done[root@centfils bin]# bash -n 11.sh [root@centfils bin]# bash -x 11.sh + for i in '{1..100}'+ touch file1+ echo 'New file 1'New file 1+ for i in '{1..100}'+ touch file2+ echo 'New file 2'New file 2+ for i in '{1..100}'+ touch file3+ echo 'New file 3'New file 3+ for i in '{1..100}'+ touch file4+ echo 'New file 4'...

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

[root@centfils bin]# vim 12.sh#!/bin/bash#sed -n 'n;p' /etc/passwd | awk -F: '{print $1}'[root@centfils bin]# sh 12.shbinadmsynchaltuucpgamesftpdbusvcsartkitabrtnfsnobodygdmapachepostfixtomcattcpdumpmyuser

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

[root@centfils bin]# vim 13.sh#!/bin/bash#for i in {10..19}; do        useradd user$i        echo "user$i" | passwd --stdin "user$i"done [root@centfils bin]# bash -n 13.sh [root@centfils bin]# bash -x 13.sh + for i in '{10..19}'+ useradd user10useradd:警告:此主目录已经存在。不从 skel 目录里向其中复制任何文件。正在创建信箱文件: 文件已存在+ passwd --stdin user10+ echo user10更改用户 user10 的密码 。passwd: 所有的身份验证令牌已经成功更新。+ for i in '{10..19}'+ useradd user11+ echo user11+ passwd --stdin user11更改用户 user11 的密码 ....

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

[root@centfils bin]# vim 14.sh#!/bin/bash#for i in {10..19}; do	touch /tmp/file$i	echo "New file:file$i"done[root@centfils bin]# bash -n 14.sh [root@centfils bin]# bash -x 14.sh + for i in '{10..19}'+ touch /tmp/file10+ echo 'New file:file10'New file:file10+ for i in '{10..19}'+ touch /tmp/file11+ echo 'New file:file11'New file:file11+ for i in '{10..19}'+ touch /tmp/file12+ echo 'New file:file12'....

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

#!/bin/bash#for i in {10..19}; do	chown user"$i":user"$i" /tmp/file$idone[root@centfils bin]# ll /tmp/file*-rw-r--r--. 1 user10 user10 0 9月   6 22:30 /tmp/file10-rw-r--r--. 1 user11 user11 0 9月   6 22:30 /tmp/file11-rw-r--r--. 1 user12 user12 0 9月   6 22:30 /tmp/file12-rw-r--r--. 1 user13 user13 0 9月   6 22:30 /tmp/file13-rw-r--r--. 1 user14 user14 0 9月   6 22:30 /tmp/file14-rw-r--r--. 1 user15 user15 0 9月   6 22:30 /tmp/file15-rw-r--r--. 1 user16 user16 0 9月   6 22:30 /tmp/file16-rw-r--r--. 1 user17 user17 0 9月   6 22:30 /tmp/file17-rw-r--r--. 1 user18 user18 0 9月   6 22:30 /tmp/file18-rw-r--r--. 1 user19 user19 0 9月   6 22:30 /tmp/file19