Josue Araya Josue Araya - 2 years ago 72
Bash Question

Bash shell programming echo without jumping to next line

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

The output of the code above is


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

41250 Kb

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download