简体   繁体   中英

Bash Awk division gets different results

I'm trying to calculate some values using awk.But I get different results when dividing with 100 and using linux command getconf CLK_TCK.

In my case the command always returns 100:

[centos@op5test1 ~]$ getconf CLK_TCK
100

Now I'm trying to calculate some values using the above command output value.

[centos@op5test1 ~]$ echo 26178 4861 | awk '{print ($1+$2)/$(getconf CLK_TCK)}'
1.18569

and now giving the value 100

[centos@op5test1 ~]$ echo 26178 4861 | awk '{print ($1+$2)/100}'
310.39

Why do I get differnt values ?

Right way to pass output of your getconf command from command line:

echo 26178 4861 | awk -v val=$(getconf CLK_TCK) '{print ($1+$2)/val}'
310.39

$(getconf CLK_TCK) when used directly inside awk command is evaluated to $0 which is 26178 4861 and you're then effectively doing:

print ($1+$2)/$0

which is performing this arithmetic:

(26178 + 4861)/26178 => 1.18569

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM