Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Some commands, such as rsync, take in lists as an argument. Can these files contain unix comments like #?

share|improve this question
add comment

4 Answers

Unless the documentation for the command explicitly mentions that it can contain comments, assume that it cannot.

share|improve this answer
    
Concur. Generally, the respective manpage of the command will detail whether you can add comments to a read file, and if so how it identifies a line as a comment. –  Shadur Oct 13 '11 at 9:58
add comment

What I usually do in cases like this, is build an arguments array (which can take comments) and then pass those along to rsync, for instance.

#!/bin/bash
rsync_args=(
    # Show me what you're doing
    --itemize-changes
    # All HTML and resources
    *.html *.css *.js
    # All PHP source code
    *.php
    # To the live server
    live:
)
rsync "${rsync_args[@]}";
share|improve this answer
add comment

If I understand you correctly, you want to add comments about files in the file list. This is not possible, because when shell encounters the # character, it will ignore all remaining character on the command line. One way to achieve this is to use rsync with --files-from=filelist argument. The filelist file can contain comments.

On the other hand, if you want to embed # characters in the filename, then this will work. For example, with bash:

touch a#b 
touch 'a #b' 
touch "a #b"
share|improve this answer
    
See e.g. the --include-from argument of rsync. –  Ignacio Vazquez-Abrams Oct 13 '11 at 12:12
add comment

Ignacio Vazquez-Abrams answer is the safest, but if you really want to do it (as I do in some instances), a simple workaround can easily get at least part-way there by dealing with # comments which take up the entire line.

Things can get a bit gnarly if you want to handle comments which follow the real list data-lines, unless you are certain that your data-lines contain no embedded # chars.

You can use process substitution and a simple helper script.

Here are some examples. The grc before diff generates coloured output.
I've used bash functions, but you can, of course, save them to a script file.

# nocom handles comment-only lines (ie. no dat)
#       it ignores any # chars embedded in your data lines.
#
  nocom() { sed -e '/^[[:space:]]*#/d' "$1"; }

# NOCOM handles handles comments which occurr after your data   
#       as well as comment-only lines.  
#       Do not use NOCM unless you are CERTAIN that your data
#       lines contain no  # chars.
#
  NOCOM() { sed -n '/^[[:space:]]*#/d; s/^\([^#][^#]*[^[:space:]]\)[[:space:]]*#.*/\1/; p' "$1"; }

printf '# a bit of hokus-pokus
        # you see the coments here, 
        # but the calling program will not.
$HOME/bin/abra
$HOME/bin/kadabra  # comment after data
$HOME/bin/sim# another comment after data
' >file

echo == nocom ==
cat <(nocom "file")
echo == NOCOM ==
cat <(NOCOM "file")
echo == diff ==
grc diff "file" <(NOCOM "file")

Here is the output:

== nocom ==
$HOME/bin/abra
$HOME/bin/kadabra  # comment after data
$HOME/bin/sim# another comment after data
== NOCOM ==
$HOME/bin/abra
$HOME/bin/kadabra
$HOME/bin/sim
== diff ==
1,3d0
< # a bit of hokus-pokus
<         # you see the coments here, 
<         # but the calling program won't.
5,6c2,3
< $HOME/bin/kadabra  # comment after data
< $HOME/bin/sim# another comment after data
---
> $HOME/bin/kadabra
> $HOME/bin/sim
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.