1、總結sed和awk的詳細用法;
2、刪除/boot/grub/grub.conf文件中所有行的行首的空白字符;
# sed 's/^[[:space:]]*//' /boot/grub/grub.conf
3、刪除/etc/fstab文件中所有以#開頭,后跟至少一個空白字符的行的行首的#和空白字符;
# sed 's/^#[[:space:]]\+//' /etc/fstab
4、把/etc/fstab文件的奇數行另存為/tmp/fstab.3;
# sed 'n;d' /etc/fstab > /tmp/fstab.3 # awk '{if (NR%2==0) next; print}' /etc/fstab > /tmp/fstab.3
5、echo一個文件路徑給sed命令,取出其基名;進一步的,取出其路徑名;
# echo "/tmp/test/fstab" | sed 's/[^/]\+\/\?$//' # echo "/tmp/test/fstab" | sed 's/\(\/.*\/\)//'
6、統計指定文件中所有行中每個單詞出現的次數;
# awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for(j in count) {print j,count[j]}}' /etc/fstab
7、統計當前系統上所有tcp連接的各種狀態的個數;
# netstat -tan | awk '/^tcp\>/{test[$NF]++}END{for(j in test) { print j,test[j]}}'
8、統計指定的web訪問日志中各ip的資源訪問次數;
# awk '{ip[$1]++}END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log
9、寫一個腳本:定義一個數組,數組元素為/var/log目錄下所有以.log結尾的文件的名字;顯示每個文件的行數;
#!/bin/bash declare -a test test=$(ls /var/log/*.log) for i in $(seq 0 $[${#test[*]}-1]); do wc -l ${test[$i]} done
10、寫一個腳本,能從所有同學中隨機挑選一位同學回答問題;進一步的,可接受一個參數,作為要挑選的同學的個數;
從給定的同學中隨機挑選一位回答問題
#!/bin/bash echo "Please enter some name of students,and the separator is space!!!" read -a student random=$((RANDOM % ${#student[@]})) echo "Please ${student[$random]} answer the question!"
可接受一個參數,作為要挑選同學的個數
#!/bin/bash echo "Please enter some name of students,and the separator is space!!!" read -a student read -p "Please enter the number of students to answer question !" num if [ $num -gt ${#student[@]} ]; then echo "The number should be smaller than the total of you enter!" exit fi echo "The list to answer the question is:" for ((i=0;i<num;i++)); do random=$((RANDOM % ${#student[@]})) echo ${student[$random]} done
11、授權centos用戶可以運行fdisk命令完成磁盤管理,以及使用mkfs或mke2fs實現文件系統管理;
# visudo Cmnd_Alias DISK = /sbin/fdisk, /sbin/mkfs, /sbin/mke2fs centos ALL=(root) DISK
12、授權gentoo用戶可以運行邏輯卷管理的相關命令;
# visudo Cmnd_Alias LVMMANAGE = /sbin/*create, /sbin/*reduce,/sbin/*scan, /sbin/*display, /sbin/fsck, /sbin/mke2fs gentoo ALL=(root) LVMMANAGE
13、基于pam_time.so模塊,限制用戶通過sshd服務遠程登錄只能在工作時間進行;
# vim /etc/pam.d/sshd account required pam_time.so -->//要位于文件的第一行 # vim /etc/security/time.conf *;*;*;MoTuWeThFr0900-1800 -->//表示工作時間的9點到下午6點
14、基于pam_listfile.so模塊,定義僅某些用戶,或某些組內的用戶可以登錄系統。
# vim /etc/sshd_userlist -->//在此文件中添加一些用戶來登錄系統 # vim /etc/pam.d/sshd auth required pam_listfile.so item=user sense=allow file=/etc/sshd_userlist onerr=succeed
原創文章,作者:Jeason,如若轉載,請注明出處:http://www.www58058.com/58298
寫的很好,排版也很棒,希望能夠再接再厲