bash (standard_in) 1: 解析错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10941991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(standard_in) 1: parse error
提问by Numpty
I'm writing a quick script for calculating individual interface throughput from data pulled from /proc/net/dev and I'm having an issue. It converts it from bytes to megabits.
我正在编写一个快速脚本,用于根据从 /proc/net/dev 中提取的数据计算各个接口吞吐量,但我遇到了问题。它将它从字节转换为兆位。
This is working on my ubuntu server (3.2.0 kernel), however when I try and run this on older devices (2.6.18 era) it's not working. I'm not sure what I'm doing wrong.
这在我的 ubuntu 服务器(3.2.0 内核)上运行,但是当我尝试在旧设备(2.6.18 时代)上运行它时,它不起作用。我不确定我做错了什么。
Here's a snippet of my code:
这是我的代码片段:
int1_byte_rx=`cat $logfile | grep $int1 | awk '{print }' | awk '{sum+=} END {print sum}'`
int1_byte_tx=`cat $logfile | grep $int1 | awk '{print }' | awk '{sum+=} END {print sum}'`
int1_rx_thrpt=$(echo "($int1_byte_rx * 0.00000762939453) / $iterations / ($time * 60)" | bc -l)
int1_tx_thrpt=$(echo "($int1_byte_tx * 0.00000762939453) / $iterations / ($time * 60)" | bc -l)
When I run this I get the following error (from debug mode):
当我运行它时,我收到以下错误(来自调试模式):
int1_rx_thrpt=$(echo "($int1_byte_rx * 0.00000762939453) / $iterations / ($time * 60)" | bc -l)
echo "($int1_byte_rx * 0.00000762939453) / $iterations / ($time * 60)" | bc -l
++ echo '(1.13417e+10 * 0.00000762939453) / 57 / (5 * 60)'
++ bc -l
(standard_in) 1: parse error
(standard_in) 1: parse error
+ int1_rx_thrpt=
int1_tx_thrpt=$(echo "($int1_byte_tx * 0.00000762939453) / $iterations / ($time * 60)" | bc -l)
echo "($int1_byte_tx * 0.00000762939453) / $iterations / ($time * 60)" | bc -l
++ echo '(9.78048e+09 * 0.00000762939453) / 57 / (5 * 60)'
++ bc -l
(standard_in) 1: parse error
(standard_in) 1: parse error
I've been able to trace the problem back to bc itself, however I'm not actually sure how to correct it.
我已经能够将问题追溯到 bc 本身,但是我实际上不确定如何纠正它。
Any advice is welcome.
欢迎任何建议。
Thanks for your time,
谢谢你的时间,
采纳答案by sarnold
My /proc/net/devdoesn't have large enough values to force awkto print any output in scientific notation, so I cannot easilytest this, but here's my suggested fix:
我/proc/net/dev没有足够大的值来强制awk以科学记数法打印任何输出,因此我无法轻松测试这一点,但这是我建议的修复方法:
int1_byte_rx=`cat $logfile | grep $int1 | awk '{print }' | awk '{sum+=} END {printf "%f", sum}'`
int1_byte_tx=`cat $logfile | grep $int1 | awk '{print }' | awk '{sum+=} END {printf "%f", sum}'`
(Note the printf "%f",portion near the end.)
(注意printf "%f",靠近结尾的部分。)
The trick is to prevent awkfrom generating the scientific format in the first place.
诀窍是首先防止awk生成科学格式。
回答by Paused until further notice.
You don't need those long pipelines and you don't need to use bc.
您不需要那些长管道,也不需要使用bc.
int1_byte_rx=$(awk -v int="$int1" 'read -r int1_byte_rx int1_byte_tx <<< $(awk -v int="$int1" '##代码## ~ int {sumrx += ; sumtx+= } END {print sumrx, sumtx}' "$logfile")
~ int {sum += } END {print sum}' "$logfile")
int1_byte_tx=$(awk -v int="$int1" '##代码## ~ int {sum += } END {print sum}' "$logfile")
int1_rx_thrpt=$(awk -v int1_byte_rx="$int1_byte_rx" -v iter="$iterations" -v time="$time" 'BEGIN {printf "%12.2f", (int1_byte_rx * 0.00000762939453) / iter / (time * 60)}')
int1_tx_thrpt=$(awk -v int1_byte_tx="$int1_byte_tx" -v iter="$iterations" -v time="$time" 'BEGIN {printf "%12.2f", (int1_byte_tx * 0.00000762939453) / iter / (time * 60)}')
You can combine the first two lines like this:
您可以像这样组合前两行:
##代码##and you'll only have to read the logfile once.
并且您只需读取一次日志文件。
Another alternative would be to write the entire script in AWK or another language that supports floating point arithmetic such as Python or Perl.
另一种选择是用 AWK 或其他支持浮点运算的语言(如 Python 或 Perl)编写整个脚本。

