views:

57

answers:

3
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.)

+1  A: 

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.

Kaleb Pederson
For clarification, `>>` appends to the file while `>` overwrites it
Michael Mrozek
+1  A: 

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
}
Josh
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.
Stefan Lasiewski
A: 

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
Stefan Lasiewski