1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;
[root@mageedu?tmp]#?cp?/etc/rc.d/rc.sysinit?. [root@mageedu?tmp]#?vim?rc.sysinit?? :%?s/^[[:space:]]/#&/?????????????#按Esc進入vim的末行模式,并輸入
2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;
[root@mageedu?tmp]#?cp?/boot/grub/grub.conf?. [root@mageedu?tmp]#?vim?grub.conf :%?s#^[[:space:]]##??????????????#按Esc進入vim的末行模式,并輸入
3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符
[root@mageedu?tmp]#?vim?rc.sysinit :%?s/^[[:space:]]\+//??????????????#按Esc進入vim的末行模式,并輸入
4、為/tmp/grub.conf文件中前三行的行首加#號;
[root@mageedu?tmp]#?vim?grub.conf :1,3?s/^#/#&/?????????????????????#按Esc進入vim的末行模式,并輸入 1?##?grub.conf?generated?by?anaconda 2?## 3?##?Note?that?you?do?not?have?to?rerun?grub?after?making?changes?to?this?file
5、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改為1;
[root@mageedu?tmp]#?vim?/etc/yum.repos.d/CentOS-Media.repo :/enabled=0/s/0/1/g :/gpgcheck=0/s/0/1/g??????????????#按Esc進入vim的末行模式,并輸入
6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為形如etc-201608300202
[root@mageedu?tmp]#?mkdir?/backup [root@mageedu?tmp]#?crontab?-e *?*/4?*?*?*?cp?-r?/etc/?/backup/etc-`date?+%Y%m%d%H%M` [root@mageedu?tmp]#?/etc/init.d/crond?start
7、每周2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20160830
[root@mageedu?tmp]#?mkdir?/backup/messages_logs [root@mageedu?tmp]#?crontab?-e *?*?*?*?*/2,4,6?cp?/var/log/messages?/backup/messages_logs/messages-`date?+%Y%m%d` [root@mageedu?tmp]#?/etc/init.d/crond?restart
8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中
[root@mageedu?tmp]#?mkdir?-pv?/stats/memory.txt [root@mageedu?tmp]#?grep?"^S"?/proc/meminfo? SwapCached:????????????0?kB SwapTotal:???????2097148?kB SwapFree:????????2097148?kB Shmem:???????????????240?kB Slab:??????????????73812?kB SReclaimable:??????14712?kB SUnreclaim:????????59100?kB [root@mageedu?tmp]#?crontab?-e *?*/2?*/1?*?*?grep?"^S"?/proc/meminfo?>>?/stats/memory.txt [root@mageedu?tmp]#?/etc/init.d/crond?restart
9、工作日的工作時間內,每兩小時執行一次echo "howdy"
[root@mageedu?tmp]#?crontab?-e *?9-18/2?*?*?*/1-5?echo?"howdy" [root@mageedu?tmp]#?/etc/init.d/crond?restart
腳本編程練習
10、創建目錄/tmp/testdir-當前日期時間;?
[root@mageedu?tmp]#?vim?testdir.sh #!/bin/bash mkdir?-pv??/tmp/testdir-$(date?+%Y%m%d%H%M) [root@mageedu?tmp]#?bash?-n?testdir.sh? [root@mageedu?tmp]#?bash?testdir.sh? mkdir:?created?directory?`/tmp/testdir-201608181859' root@mageedu?tmp]#?cd?/tmp/ [root@mageedu?tmp]#?ls a.sh???grub.conf????testdir-201608181859 a.txt??rc.sysinit????testdir.sh
11、在此目錄創建100個空文件:file1-file100
[root@mageedu?tmp]#?vim?file.sh #!/bin/bash for?i?in?{1..100};?do mkdir?-pv?/tmp/testdir-201608181859/file$i done [root@mageedu?tmp]#?bash?-n?file.sh?? [root@mageedu?tmp]#?bash?file.sh mkdir:?created?directory?`/tmp/testdir-201608181859/file1' mkdir:?created?directory?`/tmp/testdir-201608181859/file2' mkdir:?created?directory?`/tmp/testdir-201608181859/file3' ....... mkdir:?created?directory?`/tmp/testdir-201608181859/file98' mkdir:?created?directory?`/tmp/testdir-201608181859/file99' mkdir:?created?directory?`/tmp/testdir-201608181859/file100' [root@mageedu?tmp]#?cd?testdir-201608181859/ [root@mageedu?testdir-201608181859]#?ls 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/passw d文件中位于第偶數行的用戶的用戶名;
[root@mageedu?tmp]#?vim?passwd.sh #!/bin/bash sum=$(wc?-l?/etc/passwd?|?cut?-d'?'?-f1) for?i?in?$(seq?2?2?$sum);?do head?-n$i?/etc/passwd?|?tail?-n?1?|?cut?-d:?-f1 done [root@mageedu?tmp]#?bash?-n?passwd.sh [root@mageedu?tmp]#?bash?passwd.sh? bin adm sync halt uucp games ftp vcsa postfix zengzy basher nologin slackware mysql hadoop1 fedora
13、創建10用戶user10-user19;密碼同用戶名;
[root@mageedu?tmp]#?vim?user.sh #!/bin/bash if?[?!?$UID?-eq?0?];?thenecho?"welcome?root?crate?user"exit?1 fi for?i?in?{10..19};?doif?id?user$i?&>?/dev/null?;?thenecho?"user?exists"elseuseradd?user$iif?[?$??-eq?0?];?thenecho?"user$i"?|?passwd?--stdin?user$i?&>?/dev/nullecho?"New?user:user$i?add?success"fifi done [root@mageedu?tmp]#?bash?-n?user.sh? [root@mageedu?tmp]#?su?zengzy [zengzy@mageedu?tmp]$?bash?user.sh? welcome?root?crate?user [zengzy@mageedu?tmp]$?exit [root@mageedu?tmp]#?bash?user.sh? New?user:user10?add?success New?user:user11?add?success New?user:user12?add?success New?user:user13?add?success New?user:user14?add?success New?user:user15?add?success New?user:user16?add?success New?user:user17?add?success New?user:user18?add?success New?user:user19?add?success
14、在/tmp/創建10個空文件file10-file19;?
[root@mageedu?tmp]#?vim?newfile.sh #!/bin/bash for?i?in?{10..19};?do touch?/tmp/file$i done [root@mageedu?tmp]#?bash?-n?newfile.sh? [root@mageedu?tmp]#?bash?newfile.sh [root@mageedu?tmp]#?ls a.sh????file12??file16??file.sh?????rc.sysinit????????????testdir.sh a.txt???file13??file17??grub.conf???testdir-201608181852??user.sh file10??file14??file18??newfile.sh??testdir-201608181857 file11??file15??file19??passwd.sh???testdir-201608181859
15、把file10的屬主和屬組改為user10,依次類推。
[root@mageedu?tmp]#?vim?prmfile.sh #!/bin/bash for?i?in?{10..19};?do chown?user$i:user$i?/tmp/file$i donemageedu?tmp]#?ll total?68 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file10 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file11 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file12 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file13 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file14 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file15 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file16 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file17 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file18 -rw-r--r--.???1?root?root?????0?Aug?18?19:49?file19 [root@mageedu?tmp]#?bash?-n?prmfile.sh? [root@mageedu?tmp]#?bash?prmfile.sh? [root@mageedu?tmp]#?ll total?68 -rw-r--r--.???1?user10?user10?????0?Aug?18?19:49?file10 -rw-r--r--.???1?user11?user11?????0?Aug?18?19:49?file11 -rw-r--r--.???1?user12?user12?????0?Aug?18?19:49?file12 -rw-r--r--.???1?user13?user13?????0?Aug?18?19:49?file13 -rw-r--r--.???1?user14?user14?????0?Aug?18?19:49?file14 -rw-r--r--.???1?user15?user15?????0?Aug?18?19:49?file15 -rw-r--r--.???1?user16?user16?????0?Aug?18?19:49?file16 -rw-r--r--.???1?user17?user17?????0?Aug?18?19:49?file17 -rw-r--r--.???1?user18?user18?????0?Aug?18?19:49?file18 -rw-r--r--.???1?user19?user19?????0?Aug?18?19:49?file19
轉載于:https://blog.51cto.com/zengzeyang/1851682