Linux下的一些使用经验小结_2


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下的浮点数计算
搜索关键字:
参考结论:

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
参考链接:
3.grep的一些知识点
搜索关键字:
参考链接:
#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’替换(特殊符号记得转义处理)。

, ,

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注