简体   繁体   English

没有换行符的回声会导致语法错误

[英]Echo without a newline character results a syntax error

I am writing a shell script and I would like to have this code 我正在编写一个shell脚本,我想拥有此代码

echo $(awk '{print $1}' /proc/uptime) / 3600 | bc

without the newline character at the end. 末尾没有换行符。 I wanted to write it using echo -n, but this code 我想使用echo -n编写它,但是这段代码

echo -n $(awk '{print $1}' /proc/uptime) / 3600 | bc

results a syntax error: 导致语法错误:

(standard_in) 1: syntax error

Can you help me with this? 你能帮我吗? Thank you very much! 非常感谢你!

echo $(awk '{print $1}' /proc/uptime) / 3600 | bc | tr -d "\\n"

备择方案:

echo -n $(($(cut -d . -f 1 /proc/uptime)/3600))

mapfile A </proc/uptime; echo -n $((${A%%.*}/3600))

A solution using echo -n : 使用echo -n的解决方案:

echo -n $(echo $(awk '{print $1}' /proc/uptime) / 3600 | bc)

In general, if foo produces a line of output, you can print the same output without a newline using echo -n $(foo) , even if foo is complicated. 通常,如果foo产生一行输出,即使foo很复杂,您也可以使用echo -n $(foo)打印相同的输出而无需换行。

A more straightforward solution using pure awk (since awk does arithmetic and output formatting, there's not much point in using both awk and bc ): 使用纯awk更直接的解决方案(由于awk算术输出格式化,因此同时使用awkbc没什么意义):

awk '{printf("%d", $1 / 3600)}' /proc/uptime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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