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.

usually I use XX 2>error.log , but for debug purposes , I prefer the following syntax:

function XX()
{
    redirect_io_to "error.log"
}

Is there such syntax available ?

It's convenient because I setup my VIM to comment / uncomment a line with some shortcut key , I could easily comment this out / uncomment it.

share|improve this question
    
I'm not quite clear on how you want to use this... command | XX instead of command 2>/dev/null? –  Kevin Jun 12 '12 at 0:53

2 Answers 2

up vote 4 down vote accepted

Yes, but its not really pretty.

You can do exec 2> error.log to send stderr to a file

The problem is that you now have to clean it up and restore stderr, so at the bottom of the function you have to do exec 2>&1, which makes stderr go back to the terminal again.

function XX()
{
    exec 2> error.log
    foo bar
    exec 2>&1
}

The exec 2>&1 doesnt hurt to leave in, so you can just comment and uncomment the first exec.


Alternatively you could make the whole function execute in a subshell so that redirections are cleaned up automatically (the parenthesis in the below example start a subshell).

function XX()
{ (
    exec 2> error.log
    foo bar
) }

Lastly, you could just create an alias definition that you can comment and uncomment easily, and just put it right before the function declaration.

alias XX='XX 2> error.log'
function XX()
{
    foo bar
}
share|improve this answer
    
exec is exactly what I want -;P –  warl0ck Jun 12 '12 at 2:37
    
+1 for the subshell solution! –  Herman Torjussen Jun 12 '12 at 5:39

Nope, but you can always stack everything you need in one block:

function XX() {
 (
  some
  commands
  here
 ) 2>error.log
}
share|improve this answer

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.