Shell output redirection inside a function
function grabSourceFil开发者_运维问答e
{
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.)
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
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
.
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
}
精彩评论