1

When I execute unison tours 2> /dev/null | grep -A 2 '<-?->' on a bash prompt, I get the exact output i'm looking for:

*changed  <-?-> changed    Future Tours/File Clash Test.docx  
local        : changed file       modified on 2013-06-05 at 16:24:40  size 10221     rw-rw----  
bj-server... : changed file       modified on 2013-06-05 at 16:23:53  size 10232     rw-rw----*

When I put this into a shell script:

#!/bin/bash
tmp=$(unison tours 2> /dev/null | grep -A 2 '<-?->')
echo "$tmp" > /home/users/itsystem/test.log

... it comes out strange. Running more test.log , I get the following:

- Systems Sheets/Feedback She
                                                                oup).xls
| Past Tours/2012/CE0
                        12 Cherry (N...our Menus/Lijiang Tour  Menus.xls

- Past Tours/2012/UC041112 St
- Past Tours/2012/UC081112 Lopata (N... Menus - UC081012 Lo
                2/VC110912 Levin (VI... Menus - VC110912 Levin (VI).xlsx
                                                                    xlsx
| Future Tours/2013 Futur
/ Inquiries/Outstanding/Terence Park... Itinerary (Trev
changed   changed    Future Tours/File Clash Test.docx
local        : changed file       modified on 2013-06-05 at 16:24:40  size 10221     rw-rw----
bj-server... : changed file       modified on 2013-06-05 at 16:23:53  size 10232     rw-rw----

Where is the other crap coming from?

If you simply run unison then you do get an animated one line of output that is constantly refreshed with a rotating -\|/ showing the working folder, but which eventually disappears.

If my grep works on the bash prompt, why not from the shell script?

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
tcpman
  • 11
  • 2

1 Answers1

2

The animated line contains of a series of control characters that modify the cursor position on the terminal. The same effect is not achieved when the output is directed to a file; you merely get a record of all the bytes that were written to the terminal.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks @chepner - that explains the additional output, thanks, though given my grep clause... why is it still ending up in the output to file? – tcpman Jun 05 '13 at 14:34
  • Without knowing exactly how the spinning progress indicator is produced, it's hard to given an exact explanation of what you see in the log file. But it looks like some (but not all) lines include enough of the spinner that `grep` matches it. – chepner Jun 05 '13 at 14:46
  • ... thing is I should be matching '<-?->', which doesn't appear in any of the lines with the spinner, hence my confusion. – tcpman Jun 05 '13 at 14:56
  • Take a look at your log file with `hexdump`. The non-printing characters that control cursor positioning should be apparent, and you might be able to track down exactly what is being output, which is not necessarily what is visible using `more`. – chepner Jun 05 '13 at 15:09