Josue Araya Josue Araya - 5 months ago 18
Bash Question

Bash shell programing echo without jumping to next line

#!/bin/bash
echo "Enter process name"
read process
if pgrep "$process" > /dev/null
then
echo 0 $(awk '/Rss/ {print "+", $2}' /proc/`pidof $process`/smaps) | bc;
echo "Kb"
else
echo "Process $process not running"
fi


The output of the code above is

41250
Kb


and I need the "Kb" output in the same line as the number like this

41250 Kb


Thanks in advance

Answer

You can use echo -n to output data without a trailing newline but, for this particular case, awk is perfectly capable of doing arithmetic and formatting on its own, without involving bc or unnecessary sub-processes:

awk '/Rss/ {sum += $2} END {print sum" Kb"}' /proc/`pidof $process`/smaps

You can see that in the following transcript which adds up the two Rss figures 75 and 11 to get 86:

pax> printf Rss 75\nCss 22\nRss 11\n' | awk '/Rss/ {sum += $2} END {print sum" Kb"}'
86 Kb