shell脚本语句

 grep -w zhangsan 1.txt|awk '{sum += $2} END {print sum}'

grep -w跟grep -o的区别 :精准匹配。

一、linux中过滤IP地址

最简短

ifconfig|awk NR==2|awk -F '[ :]+' '{print $4}'`

最麻烦

ifconfig eth4|grep "inet addr:"|awk -F: '{print $2}'|awk '{print $1}'

其他

IP=`ifconfig eth4|awk NR==2|cut -f2 -d":"|cut -f1 -d" "`

VIP=`ifconfig|awk NR==2|awk -F: '{print $2}'|awk '{print $1}'`

for循环语句

#!/bin/bash
for x in one two three four
do
echo number: $x
done

for i in /root/find/*
do
# echo $(basename $i) is a file living in /root/find
# echo $i
# echo $(basename $i)
echo `basename $i`
done

find命令查找

find . 查看当前目录下的文件及子文件中的内容

find . -name "*.txt"   #查找当前目录下已.txt结尾的文件

find . -iname "*.txt"   #查找当前目录下已.txt结尾的文件并不分大小写

find . ( -iname "*.txt" -o -name "*.pdf" ) #

find . -iname "*.txt" -o -name "*.pdf"    #查找当前目录下已.txt结尾已.pdf结尾的文件.txt结尾的文件不区分大小写

find /root/find/ -name "*.txt"    ==    find /root/find/ -path "*.txt"    匹配文件路径或文件

find正则表达式

#查找根目录下以/find结尾的文件或目录

find / -regex ".*/find"
/usr/bin/find
/bin/find
/root/find

#查找根目录下以find结尾的文件或目录

find / -regex ".*find"
/usr/bin/find
/usr/bin/gst-typefind
/usr/bin/oldfind
/bin/find
/root/find

运用regex对文件路径进行匹配,iregex忽略大小写。

否定参数

find . ! -name "*.txt"    打印出所有不以.txt结尾的文件

type类型参数

find . -type d

f 普通文件    l 连接文件    d 目录

根据文件时间戳进行查找

find . -type f  

 按照文件大小进行查找

 

原文地址:https://www.cnblogs.com/xiaoyongzhuo/p/7412054.html