1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#。
第一步: [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp 第二步: [root@localhost tmp]# vim rc.sysinit 在末行模式下輸入: %s/^[[:space:]]\+[[:space:]]/#&/g 或者 %s@^[[:space:]]\+[[:space:]]@#&@g
2、復制/boot/grub/grub.conf文件至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符。
第一步: [root@localhost ~]# cp /boot/grub/grub.conf /tmp 第二步: [root@localhost tmp]# vim grub.conf 在末行模式下輸入: %s/^[[:space:]]\+//
3、刪除/tmp/rc.sysinit文件中以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符。
[root@localhost tmp]# vim rc.sysinit 在末行模式下輸入: %s/^#[[:space:]]\+//
4、為/tmp/grub.conf文件中前三行的行首加#號。
[root@localhost tmp]# vim grub.conf 在末行模式下輸入: 1,3s/^/#&/
5、將/etc/yum.repos.d/Centos-Media.repo文件中所以的enabled=0或gpgcheck=0的最后的0修改為1。
[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Media.repo 在末行模式下輸入: %s#\(enabled\|gpgcheck\)=0#\1=1#g
6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為行如etc-201504020202。
[root@localhost ~]# mkdir /backup [root@localhost ~]# crontab -e 0 */4 * * * /bin/cp -a /etc /backup/etc-$(date +\%Y\%m\%d\%H\%M) > /dev/null
7、每周2,4,6備份/var/log/mseeages文件至/backup/messages_log/目錄中,保存的文件名形如messages-
20150402。
[root@localhost ~]# mkdir /backup/messages_log [root@localhost ~]# crontab -e 0 0 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_log/messages-$(date +\%Y\%m\%d) > /dev/null
8、每天每兩小時去當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中。
[root@localhost ~]# mkdit /stats [root@localhost ~]# crontab -e 0 */2 * * * /bin/cat /proc/meminfo | grep "^S" >> /stats/memory.txt
9、工作日的工作時間內,每兩小時執行一次echo "howdy"。
[root@localhost ~]# crontab -e 0 9-18/2 * * 1-5 /bin/echo "howdy"
腳本練習:
1、創建目錄/tmp/testdir-當前日期時間;在此目錄創建100個空文件:file1-file100。
#!/bin/bash # dir=/tmp/testdir-$(date +%m%d%H%M) mkdir $dir for i in {1..100};do touch $dir/file$i done
2、顯示/etc/passwd文件中位于第偶數行的用戶的用戶名。
#!/bin/bash # sed '1d;n;d' /etc/passwd
3、創建10個用戶user10-user19,密碼等同用戶名。
#!/bin/bash # for i in {10..19};do if id user$i &> /dev/null;then echo "user$i exists" else useradd user$i echo "user$i" | passwd --stdin user$i &> /dev/null fi done
4、在/tmp/創建10個空文件file10-file19;把file10的屬主和屬組改為user10,依次類推。
#!/bin/bash # f=/tmp/file for i in {10..19};do if [ -f "$f$i" ];then echo "File file$i exists" else touch $f$i chown user$i:user$i $f$i fi done
原創文章,作者:641348038@qq.com,如若轉載,請注明出處:http://www.www58058.com/62007
其實后面的問題也可能用多種方法來實現,來擴展知識!