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 '1~2p' /etc/fstab >/tmp/fstab.3sed 'n;d' /etc/fstab >/tmp/fstab.3
5、echo一個文件路徑給sed命令,取出其基名;進一步地,取出其路徑名;
echo "/var/log" | sed -r 's@(/.*/)@@g'echo "/etc/sysconfig/" | sed 's@[^/]\+/\?$@@'
6、統計指定文件中所有行中每個單詞出現的次數;
awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}' /etc/fstab
7、統計當前系統上所有tcp連接的各種狀態的個數;
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
8、統計指定的web訪問日志中各ip的資源訪問次數:
awk '{ip[$1]++} END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log
9、寫一個腳本:定義一個數組,數組元素為/var/log目錄下所有以.log結尾的文件的名字;顯示每個文件的行數;
vim line.sh #!/bin/bash line=$(ls /var/log/*.log) for i in $(seq 0 $[${#line[*]}-1]);do wc -l ${line[$i]} done
10、寫一個腳本,能從所有同學中隨機挑選一個同學回答問題;進一步地:可接受一個參數,做為要挑選的同學的個數;
vim pick.sh declare -a student for in in {1..99};do pick=$i-1 student[ $k ]=student$i done echo "Now ,We have $i students that you can pick up!" read -p "How many students do you want:" count ! let count++ &>/dev/null && echo "Must be a number!" && exit 13 [ $count -eq 0 -o $count -gt 99 ] && echo "Please selcet a number between 1 and 99!" && exit 12 m=1 while [ $m -lt $count ];do rand=${RANDOM:0-2} if echo $rand | grep "^0" &>/dev/null;then index=${rand:0-1} echo "${student[ $index ]}, You answer my questions Please!" else echo "${student[ $rand ]}, You answer my questions Please!" fi let m++ done
11、授權centos用戶可以運行fdisk命令完成磁盤管理,以及使用mkfs或mke2fs實現文件系統管理;
visudo centos 10.0.0.2=/sbin/mkfs, /sbin/mke2fs, /sbin/ifconfig ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:47:BE:80 inet addr:10.0.0.2 Bcast:10.255.255.255 Mask:255.0.0.0 inet6 addr: fe80::20c:29ff:fe47:be80/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4340 errors:0 dropped:0 overruns:0 frame:0 TX packets:3313 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:439145 (428.8 KiB) TX bytes:480457 (469.1 KiB) shutdown -h now shutdown: Need to be root
12、授權gentoo用戶可以運行邏輯卷管理的相關命令;
visudo gentoo 10.0.0.2=/sbin/*create, /sbin/*reduce,/sbin/*scan, /sbin/*display, /sbin/fsck,/sbin/resize2fs
13、基于pam_time.so模塊,限制用戶通過sshd服務遠程登錄只能在工作時間進行;
grep -i "usepam" /etc/ssh/sshd_config #UsePAM noUsePAM yes 確保sshd開啟Pam模塊認證ls /lib64/security/pam_time.so 保證pam_time.so 存在 ls /etc/pam.d/sshd /etc/security/time.conf 保證Pam模塊配置文件存在 /etc/pam.d/sshd 添加 session required pam_time.so /etc/security/time.conf 添加 sshd;ttyp*;root;!ALSa0000-2400 工作日時寫成 !ALWd0000-2400 Connecting to 192.168.40.128:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Sun Jun 5 03:56:43 2016 from 192.168.40.1 Connection closed by foreign host. Disconnected from remote host(192.168.40.) at 08:32:07.
14、基于pam_listfile.so模塊,定義僅某些用戶,或某些組內的用戶可登錄系統;
grep "pam_listfile" /etc/pam.d/sshd auth required pam_listfile.so item=user sense=deny file=/etc/nossh onerr=fail cat /etc/nossh derulo
原創文章,作者:N21_ Dominic,如若轉載,請注明出處:http://www.www58058.com/57200
可以把sed 和awk的一些基本參數總結一下,期待你的佳作