詳細總結VIM編輯器的使用并完成以下練習題
1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#
cp /etc/rc.d/rc.sysinit /tmp vim /tmp/rc.sysinit :%s/^[[:space:]]\+/#/g
2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符
cp /boot/grub/grub.conf /tmp vim /tmp/grub.conf :%s@^[[:space:]]\+@@g
3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行的#和空白字符
:%s@^#[[:space:]]\+@@g
4、為/tmp/grub.conf文件中前三行的行首加#號;
:1,3s/^/#/g
5、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改為1
:%s/\<enabled=0\>/enabled=1/g :%s/\<gpgcheck=0\>/gpgcheck=1/g
6、每4小時執行一次對/etc目錄的備份,備份至/backup/目錄中,保存的文件名形如messages-20160502
# mkdir /backup # crontab -l 0 */4 * * * /bin/cp -a /etc/ /backup/etc-$(date +%Y%m%d%H%M) &>/dev/null
7、 每周2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20150402
0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-$(date +%Y%m%d) &>/dev/null
8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中
0 */2 * * * /bin/egrep '^(S|s)' /proc/meminfo >> /stats/memory.txt &>/dev/null
9、工作日的工作時間內,每兩小時執行一次echo “howdy”
0 8-18/2 * * 1-5 /bin/echo "howdy" &>/dev/null
腳本練習題
10、創建目錄/tmp/testdir-當前日期時間;在此目錄創建100個空文件:file1-file100
[root@rhel-6 test]# cat test1.sh #!/bin/bash TestDir=/tmp/testdir-$(date +%Y%m%d%H%M) TestLog=${TestDir}/failure.log mkdir ${TestDir} &>/dev/null [[ $? -eq 0 ]] && echo "Create ${TestDir} success." for (( i = 1; i < 101; i++ )); do touch ${TestDir}/file$i || echo "Failure:file$i" >> ${TestLog} done if [[ -s "${TestLog}" ]]; then cat ${TestLog} else echo 'Create file1-file100 success.' fi [root@rhel-6 test]# sh test1.sh Create /tmp/testdir-201608132309 success. Create file1-file100 success.
12、顯示/etc/passwd文件中謂語第偶數行的用戶的用戶名
[root@rhel-6 test]# cat test2.sh #!/bin/bash NO_passwd=$(cat /etc/passwd | wc -l) echo 'The UserName list is:' #for (( i = 0; i <= ${NO_passwd}; i+=2 )); do for i in $(seq 2 2 ${NO_passwd});do head -n $i /etc/passwd | tail -n 1 | cut -d':' -f1 done [root@rhel-6 test]# sh test2.sh The UserName list is: bin adm ... ...
13、創建10個用戶user10-user19;密碼同用戶名;
[root@rhel-6 test]# cat test3.sh #!/bin/bash for i in {10..19}; do id user$i &>/dev/null if [[ $? -eq 0 ]]; then echo "user$i exist." echo user$i | passwd --stdin user$i &>/dev/null [[ $? -eq 0 ]] && echo "user$i password change success." || echo "user$i password change failed." else useradd user$i echo "user$i" | passwd --stdin user$i &>/dev/null [[ $? -eq 0 ]] && echo "Create user$i success." || echo "Creat user$i failure." fi done
[root@rhel-6 test]# useradd user13 [root@rhel-6 test]# sh test3.sh Create user10 success. ... user13 exist. user13 password change success. Create user14 success. ...
14、在/tmp/創建10個空文件file10-file19;
[root@rhel-6 test]# cat test4.sh#!/bin/bashTmpFile=/tmp/filefor i in {10..19}; do if [[ -f "${TmpFile}$i" ]]; then echo "${TmpFile}$i exist." chown user$i:user$i ${TmpFile}$i && echo "${TmpFile}$i: owner and group change to user$i:user$i" else touch "${TmpFile}$i" if [[ $? -eq 0 ]]; then echo "${TmpFile}$i create success." chown "user$i:user$i" ${TmpFile}$i && echo "${TmpFile}$i: owner and group change to user$i:user$i" else echo "${TmpFile}$i create failure." fi fi done[root@rhel-6 test]# touch /tmp/file12[root@rhel-6 test]# sh test4.sh/tmp/file10 create success./tmp/file10: owner and group change to user10:user10/tmp/file11 create success./tmp/file11: owner and group change to user11:user11/tmp/file12 exist./tmp/file12: owner and group change to user12:user12......
原創文章,作者:N21-chenggb,如若轉載,請注明出處:http://www.www58058.com/35971
寫的很好,排版也很棒,加油