1
function grabSourceFile
{
    cd /tmp/lmpsource
    wget $1 >  $LOG
    baseName=$(basename $1)
    tar -xvf $baseName > $LOG
    cd $baseName
}

When I call this function The captured output is not going to the log file. The output redirection works fine until I call the function. The $LOG variable is set at the top of the file. I tried echoing statements and they would not print. I am guessing the function captures the output itself? If so how do push the output to the file instead of the console. (The wget above prints to console, while an echo inside the function does nothing.)

2
  • I wouldn't deal with output redirection inside functions. Use exec to setup/restore output redirections on STDERR/STDOUT: exec 3>&2 2>/tmp/error.log; DOSOMETHING; exec 2>&3; Commented Jun 1, 2010 at 22:19
  • wget $1 > $LOG will redirect stdout into the file. If you're trying to capture stderr, use wget $1 2>$LOG, or to capture both, wget $1 > $LOG 2>&1. If utilized properly, stdout will contain "real" data (in wget case, the data downloaded), while stderr will contain errors and status messages. Commented Jan 1, 2019 at 19:05

3 Answers 3

3

As mentioned earlier, you are writing to the same logfile twice. Your function is logging the output of 'wget' and is then overwriting that output later on, with the tar command.

Myself, I like to log outside of functions. This will reduce the chance that your logfile gets clobbered. It also keeps the function code neat and tidy.

function grabSourceFile
{
        cd /tmp/lmpsource
        wget $1
        baseName=$(basename $1)
        tar -xvf $baseName
        cd $baseName
} >> $LOG

Or just do:

grabSourceFile >> $LOG
Sign up to request clarification or add additional context in comments.

Comments

1

Redirection works the same inside and outside of functions.

Your problem is likely that you want a double greater than sign rather than a single greater than sign. I.e. wget $1 >> $LOG. The redirection on your tar command truncates the output from wget.

1 Comment

For clarification, >> appends to the file while > overwrites it
1

I found the problem. It was with wget. wget has an option specifically for logging as I guess it can't have it's output redirected using the > (something with curses.) My working function ended up being:

function grabSourceFile
{
        cd /tmp/lmpsource
        wget -a /tmp/lamps_install.log $1
        baseName=$(basename $1)
        tar -xvf $baseName >> $LOG
        cd $baseName
}

1 Comment

I think you are correct about why wget http://www.example.org > tmp/wget.log doesn't work (Doesn't work for me either. I'm working on a similar issue). Also note that in your first command you use 'tar -xvf $baseName > $LOG, which will overwrite $LOG. Your second iteration does >> $LOG` which appends to $LOG.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.