0

In the following command i get special characters in alert.txt for the top output.How can i prevent it....

 #!/bin/sh   
 topsnapshot=`top -n 5`
  echo -e "\n\n===========================TOP COMMAND SNAPSHOT====================================================" > /tmp/alert.txt
 echo "$topsnapshot" >> /tmp/alert.txt

Output:

  ===========================TOP COMMAND SNAPSHOT====================================================
  ^[[H^[[2J^[(B^[[mtop - 18:56:01 up 112 days, 18:22,  5 users,  load average: 0.99, 0.73, 0.33^[(B^[[m^[[39;49m^[[K
  Tasks:^[(B^[[m^[[39;49m^[(B^[[m 142 ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m   2 ^[(B^[[m^[[39;49mrunning,^[(B^[[m^[[39;49m^[(B^[[m 139 ^[(B^[[m^[[39;49msleeping,^[(B^[[m^[[39;49m^[(B^[[m   0 ^[(B^[[m^[[39;49mstopped,^[(B^[[m^[[39;49m^[(B^[[m   1 ^[(B^[[m^[[39;49mzombie^[(B^[[m^[[39;49m^[[K
  Cpu(s):^[(B^[[m^[[39;49m^[(B^[[m  0.2%^[(B^[[m^[[39;49mus,^[(B^[[m^[[39;49m^[(B^[[m  0.2%^[(B^[[m^[[39;49msy,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mni,^[(B^[[m^[[39;49m^[(B^[[m 99.1%^[(B^[[m^[[39;49mid,^[(B^[[m^[[39;49m^[(B^[[m  0.4%^[(B^[[m^[[39;49mwa,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mhi,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49msi,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mst^[(B^[[m^[[39;49m^[[K
  Mem: ^[(B^[[m^[[39;49m^[(B^[[m  4142428k ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m  2291280k ^[(B^[[m^[[39;49mused,^[(B^[[m^[[39;49m^[(B^[[m  1851148k ^[(B^[[m^[[39;49mfree,^[(B^[[m^[[39;49m^[(B^[[m   569928k ^[(B^[[m^[[39;49mbuffers^[(B^[[m^[[39;49m^[[K
  Swap:^[(B^[[m^[[39;49m^[(B^[[m  5668856k ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m       76k ^[(B^[[m^[[39;49mused,^[(B^[[m^[[39;49m^[(B^[[m  5668780k ^[(B^[[m^[[39;49mfree,^[(B^[[m^[[39;49m^[(B^[[m  1088948k ^[(B^[[m^[[39;49mcached^[(B^[[m^[[39;49m^[[K
  ^[[6;1H
  ^[[7m  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                   ^[(B^[[m^[[39;49m^[[K
  ^[(B^[[m^[(B^[[m31832 root      25   0  6072 2100 1228 R 99.2  0.1   6:40.14 t.py                                                                                                      ^[(B^[[m^[[39;49m
  ^[(B^[[m    1 root      15   0  2160  644  556 S  0.0  0.0   0:17.83 init
Charles
  • 50,943
  • 13
  • 104
  • 142
Rajeev
  • 44,985
  • 76
  • 186
  • 285

3 Answers3

3

Try:

topsnapshot=`top -b -n 5`
Eli Rosencruft
  • 7,232
  • 1
  • 19
  • 17
1

This works for me on Linux:

topsnapshot=$(TERM=dumb top -n 5)

The dumb terminal has no escape sequences.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Though ouput is not formatted and the command repeats for 5 times in the output.Thsi results in only one output top -n 5 – Rajeev Mar 29 '12 at 14:50
  • 2
    How did you determine that it was not formatted? If I do: `echo "$topsnapshot"`, I get neatly formatted data. If I do: `echo $topsnapshot`, then I get a mess. This is to be expected; the shell would be misbehaving were it otherwise. – Jonathan Leffler Mar 29 '12 at 14:52
  • Also, the `-n 5` option says "produce 5 lots of output". If you want just one, then use `top -n 1`. The `-n 5` comes direct from your question. When going direct to screen, the fancy formatting characters (terminal control characters) mean you only see one lot of output at a time, but the `top` program generates 5 screens full. – Jonathan Leffler Mar 29 '12 at 17:10
0

You've claimed in your comments to other answers that you don't want it repeated 5 times, yet in your example command you use -n 5. The -n 5 tells top to repeat 5 times. So try the following to get it to repeat only once:

topsnapshot=$(top -b -n 1)

If you only want the first few lines of output, use head:

topsnapshot=$(top -b -n 1 | head -n 25)

To print the captured output, I recommend the following:

printf '%s\n' "${topsnapshot}"

Be sure to remember the double quotes around ${topsnapshot}. I don't use echo because it may or may not treat backslashes specially depending on the implementation of the shell.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97