I am writing a shell script and I would like to have this code
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 $(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 $(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.
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 '{printf("%d", $1 / 3600)}' /proc/uptime
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.