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.178linux.com/57200
评论列表(1条)
可以把sed 和awk的一些基本参数总结一下,期待你的佳作