1.如何监控、分析Linux系统的负载状态等关键信息?
$ uptime $ w $ cat /proc/loadavg $ vmstat $ mpstat -P ALL $ sar
/proc/net/dev (all netwrok interfaces for all their values) /proc/diskstats (all disks for all their values) /proc/net/snmp (total IPv4, TCP and UDP usage) /proc/net/netstat (more IPv4 usage) /proc/net/stat/nf_conntrack (connection tracking performance) /proc/net/ip_vs/stats (IPVS connection statistics) /proc/stat (CPU utilization) /proc/meminfo (memory information) /proc/vmstat (system performance)
2.Linux下的浮点数计算
搜索关键字:
- http://search.aol.com/aol/search? q=linux+Floating+point+calculation
- http://search.aol.com/aol/search? q=linux+expr+Floating+point+calculation
参考结论:
bc或awk
for maths operations, try to use bc or awk , not bash or expr
> echo "scale = 2; 20 * 100 / 30" | bc
66.66
> echo "scale = 2; 20 / 30 * 100" | bc
66.00
> echo "22 5" | awk '{printf "%.2f \n", $1/$2}'
4.40
# bc <<< 'scale=4;20+5/2'
bc <<< 'scale=4;20+5/2'
22.5000
# echo "22 5" | awk '{printf "%.2f \n", $1/$2}'
echo "22 5" | awk '{printf "%.2f \n", $1/$2}'
4.40
# expr 2+4
expr 2+4
2+4
# expr 2 + 4
expr 2 + 4
6
# echo 20 5 2 / + p | dc
echo 20 5 2 / + p | dc
22
# echo 4 k 20 5 2 / + p | dc
echo 4 k 20 5 2 / + p | dc
22.5000
# perl -E "say 20+5/2"
perl -E "say 20+5/2"
22.5
# python -c "print 20+5/2"
python -c "print 20+5/2"
22
# python -c "print 20+5/2.0"
python -c "print 20+5/2.0"
22.5
$ php -r 'echo 20+5/2;'
22.5
# ruby -e 'p 20+5/2'
ruby -e 'p 20+5/2'
22
# ruby -e 'p 20+5/2.0'
ruby -e 'p 20+5/2.0'
22.5
# sqlite3 <<< 'select 20+5/2;'
sqlite3 <<< 'select 20+5/2;'
22
# sqlite3 <<< 'select 20+5/2.0;'
sqlite3 <<< 'select 20+5/2.0;'
22.5
#!/bin/bash
# Floating point number functions.
# http://www.linuxjournal.com/content/floating-point-math-bash
# Default scale used by float functions.
float_scale=2
# Evaluate a floating point number expression.
function float_eval() {
local stat=0
local result=0.0
if [[ $# -gt 0 ]]; then
result=$(echo "scale=$float_scale; $*" | bc -q 2>/dev/null)
stat=$?
if [[ $stat -eq 0 && -z "$result" ]]; then stat=1; fi
fi
echo $result
return $stat
}
# Evaluate a floating point number conditional expression.
function float_cond() {
local cond=0
if [[ $# -gt 0 ]]; then
cond=$(echo "$*" | bc -q 2>/dev/null)
if [[ -z "$cond" ]]; then cond=0; fi
if [[ "$cond" != 0 && "$cond" != 1 ]]; then cond=0; fi
fi
local stat=$((cond == 0))
return $stat
}
# Test code if invoked directly.
if [[ $(basename $0 .sh) == 'float' ]]; then
# Use command line arguments if there are any.
if [[ $# -gt 0 ]]; then
echo $(float_eval $*)
else
# Turn off pathname expansion so * doesn't get expanded
set -f
e="12.5 / 3.2"
echo $e is $(float_eval "$e")
e="100.4 / 4.2 + 3.2 * 6.5"
echo $e is $(float_eval "$e")
if float_cond '10.0 > 9.3'; then
echo "10.0 is greater than 9.3"
fi
if float_cond '10.0 < 9.3'; then
echo "Oops"
else
echo "10.0 is not less than 9.3"
fi
a=12.0
b=3.0
c=$(float_eval "$a / $b")
echo "$a / $b" is $c
set +f
fi
fi
参考链接:
- http://stackoverflow.com/questions/12722095/how-can-i-get-a-float-division-in-bash
- http://stackoverflow.com/questions/12147040/division-in-script-and-floating-point
- http://www.linuxjournal.com/content/floating-point-math-bash
- http://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks
3.grep的一些知识点
搜索关键字:
- http://search.aol.com/aol/search?q=grep+-E+pattern
- http://search.aol.com/aol/search?q=grep+perl-regexp
- http://search.aol.com/aol/search?q=grep+-P+%27the+exclamation+mark%27
参考链接:
- https://www.gnu.org/software/grep/manual/grep.html
- http://linux.die.net/man/3/pcrepattern
- http://www.thegeekstuff.com/2011/10/grep-or-and-not-operators/ #Nice
- http://unix.stackexchange.com/questions/21764/grep-or-regex-problem #Nice
- http://ss64.com/bash/grep.html #Nice
- http://www.cyberciti.biz/faq/grep-regular-expressions/
- https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux
- http://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-lines
- http://ixyzero.com/blog/regex.html
#grep在匹配时,需要将感叹号”!”给转义掉
- http://unix.stackexchange.com/questions/33339/cant-use-exclamation-mark-in-bash
- http://stackoverflow.com/questions/13461828/escaping-the-exclamation-point-in-grep
4.用grep进行“或”匹配
==实现“或”的功能==
将下面的这3个正则合并为1个:
userID=[0-9]{16}
name='sp_id' value='[0-9]{16}'
Id=[0-9]{16}
直接在命令行下:
# grep -Ei "id(' value='|=)[0-9]{16}" all.log
# cat all.log | grep -Ei "id(' value='|=)[0-9]{16}"
在脚本中:
#!/bin/bash
regex_1="id('\svalue='|=)[0-9]{16}|CDATA\[[0-9]{16}" #需要将空格替换为’\s’
grep -Ei $regex_1 all.log
grep -Pi $regex_1 all.log
如果上面的那个空格没有被替换为’\s’就会报错:
grep: Unmatched ( or \(
# xxxx="id' value='[0-9]{16}"
# echo $xxxx
id' value='[0-9]{16}
# cat all.log | grep -Ei $xxxx
grep: value='[0-9]{16}: No such file or directory
# cat all.log | grep -Ei "id' value='[0-9]{16}"
...ok...
==
# xxxx="id\'\ value=\'[0-9]{16}"
# cat all.log | grep -Ei $xxxx
grep: Trailing backslash
==
# xxxx="id'\svalue='[0-9]{16}"
# cat all.log | grep -Ei $xxxx
...ok...
结论:记得空格用’\s’替换(特殊符号记得转义处理)。