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

]# cp /etc/rc.d/rc.sysinit /tmp  

vim /tmp/rc.sysinit

:%s@^\([[:space:]]\+\)@#\1@


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

cp /boot/grub/grub.conf /tmp

vim /tmp/grub.conf

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

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

vim /tmp/rc.sysinit

:%s@#[[:space:]]\+@@

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

vim /tmp/grub.conf

:1,3s@\(\)@#\1@

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

vim /etc/yum.repos.d/CentOS-Media.repo

:%s@enabled=0@enabled=1@ 

:%s@gpgcheck=0@gpgecheck=1@

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

# mkdir /backup

]# crontab -e

1 */4 * * * /bin/cp -a /etc/ /backup/etc-`date +\%Y\%m\%d\%H\%M`

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

]# mkdir /backup/messages_logs

crontab -e

1 1 * * 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

crontab -e

1 2/* * * *  /bin/grep -i "^s" /proc/meminfo>>/stats/memory.txt

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

crontab -e

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

脚本编程练习

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

    ]# vim mkdir.sh

    #!/bin/bash

    #

    mkdir -p /tmp/testdir-`date +%F`

     

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

]# vim touchfile.sh

 

#!/bin/bash

#

i=0

while [ $i -le 100 ] ;do

        touch file$i

        let i++

Done

]# bash -x touchfile.sh

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

#!/bin/bash

#

declare -i i=$( wc -l /etc/passwd | cut -d" " -f1 )

for i in $(seq 2 2 $i) ;do

         sed -n ${i}p /etc/passwd |cut -d":" -f1

done

          

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

 

#!/bin/bash

i=1

while [ $i -le 19 ]; do

        useradd user$i

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

        let i++

done

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

]# vim touchfile1-19.sh

 

#!/bin/bash

#

i=10

while [ $i -le 19 ] ;do

        touch /tmp/file$i

        let i++

done

 

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

#!/bin/bash

#

i=10

while [ $i -le 19 ] ;do

        touch /tmp/file$i

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

        let i++

done