函数内部的Shell输出重定向 [英] Shell output redirection inside a function

查看:48
本文介绍了函数内部的Shell输出重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我调用此函数时,捕获的输出不会进入日志文件.在我调用该函数之前,输出重定向工作正常.$ LOG变量设置在文件的顶部.我尝试回显语句,但它们无法打印.我猜函数会捕获输出本身吗?如果是这样,如何将输出推送到文件而不是控制台.(上面的wget打印到控制台,而函数内部的回显则不起作用.)

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

推荐答案

我发现了问题.是wget.wget有一个专门用于日志记录的选项,因为我猜它无法使用>(带有curses的东西)将其输出重定向.我的工作职能最终是:

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
}

这篇关于函数内部的Shell输出重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆