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.

Consider the following bash constructs:

ls /usr/include/asm > list-redir.txt
ls /usr/include/asm | tee list-tee.txt

In this case, list-redir.txt and list-tee.txt will be identical, and will contain the listing of files as expected; e.g.

$ head -5 list-redir.txt
a.out.h
auxvec.h
bitsperlong.h
boot.h
bootparam.h [...]

My question is - how could I write such a command, and have the command line text inserted as the first thing in the standard output - such that the file eventually starts with the command line as first? As an example, the file list-redir.txt would in that case look like:

$ head -5 list-redir.txt
# ls /usr/include/asm
a.out.h
auxvec.h
bitsperlong.h
boot.h [...]

... which also implies that the character # can be prepended to the inserted command line.

Is there anything that I could use for this - but with the minimum of change in typing out in respect to the original command lines (ls /usr/include/asm > list-redir.txt ...)?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

A simple (and ugly) hack would be to add this to your ~/.bashrc:

echorun(){
    echo "# $@";
    "$@"
}

You'd then run your command as

echorun ls /usr > list-redir.txt

That will not let you differentiate between ls /usr >foo and ls /usr | tee foo but it will append # ls /usr to the beginning of foo.

share|improve this answer
    
Many thanks @terdon - I'll be accepting this, as it looks like it minimizes the amount of typing; I didn't need to differentiate between redirection and piping, so it looks good. Cheers! –  sdaau Jun 23 at 17:15
add comment

You could just do this:

{   cmd="ls /usr/include/asm"
    echo "$cmd" ; $cmd
} >./list-redir.txt

At least I think this is what you want done. This would yield results like:

 $ cat <./list-redir.txt

 ###OUTPUT###

   ls /usr/include/asm
 #output of above command#
   ...
share|improve this answer
    
Thanks for that @mikeserv - the other answer has also a note about a shorter usage on command line, so I accepted that one. Cheers! –  sdaau Jun 23 at 17:16
1  
Cool by me, @sddau - it's a good answer. –  mikeserv Jun 23 at 17:18
add comment

Actually, just realized that the answer from @terdon may not work with a more complicated command pipeline; so I came up with the following alias (named er as short for @tendon's echorun):

#alias er=' cat <(echo "# cmd: $(history 1)") - | tee' # last tee is not needed, so:
alias er=' cat <(echo "# cmd: $(history 1)") -'

The idea is, basically, that |er should be inserted before the last pipe or redirect in a command line; then, it is a lucky coincidence indeed, that at that point, history 1 exactly refers to the current command line! Thus, it can be echoed first, before the rest of (what is at that point) standard input, by cat. And so, we can now do things like:

$ ls /usr/include/asm | grep 'p*.h' | grep 'osix' |er | tee mylist.txt
# cmd:   125  ls /usr/include/asm | grep 'p*.h' | grep 'osix' |er | tee mylist.txt
posix_types_32.h
posix_types_64.h
posix_types.h
$ ls /usr/include/asm | grep 's*.h' | grep 'ig' |er >> mylist.txt
$ cat mylist.txt 
# cmd:   125  ls /usr/include/asm | grep 'p*.h' | grep 'osix' |er | tee mylist.txt
posix_types_32.h
posix_types_64.h
posix_types.h
# cmd:   126  ls /usr/include/asm | grep 's*.h' | grep 'ig' |er >> mylist.txt
sigcontext32.h
sigcontext.h
siginfo.h
signal.h

Thus we have full command line of multiple pipes - and we don't have to worry about escaping anything - basically, just add er to the last pipe. The only slight nag is the history number (it doesn't bother me that much, otherwise I'd add an additional awk in the alias).

Well, hope this helps someone,
Cheers!

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.