Bash 使用tee(或其他命令)将错误或消息记录到文件中,但仅当变量x==”时;是”;
我尝试使用if语句,但这不起作用,因为tee命令有两个括号,一个在开始处,一个在结束处 我试过这样的东西,但也没用Bash 使用tee(或其他命令)将错误或消息记录到文件中,但仅当变量x==”时;是”;,bash,shell,logging,tee,Bash,Shell,Logging,Tee,我尝试使用if语句,但这不起作用,因为tee命令有两个括号,一个在开始处,一个在结束处 我试过这样的东西,但也没用 if [[ "$logging" == "yes" ]]; then ftpt="2>&1 | tee $ftpLF" else ftpt="" fi } "$ftpt" 错误: ./ftp.sh: line 149: syntax error near unexpected token `"$ftpt"' ./ftp.sh: line 149:
if [[ "$logging" == "yes" ]]; then
ftpt="2>&1 | tee $ftpLF"
else
ftpt=""
fi
} "$ftpt"
错误:
./ftp.sh: line 149: syntax error near unexpected token `"$ftpt"'
./ftp.sh: line 149: `} "$ftpt"'
我现在用这个,但是我没有打开/关闭的选择,它总是开着的
{
....commands....
} 2>&1 | tee "$ftpLF"
如果您可以一致地引用内容,一个选项是使用
eval
强制Bash评估命令中添加的部分:
eval '{
command1 "foo bar" baz
command2
} "$ftpt"'
另一种选择是使用实际命名的函数:
ftpcommands() {
command1 "foo bar" baz
command2
}
if [[ "$logging" == "yes" ]]; then
ftpcommands 2>&1 | tee "$ftpLF"
else
ftpcommands
fi
后者可能是首选选项,因为您不必担心奇怪的引用问题或其他诸如此类的问题。您能给我们提供一点您尝试执行的命令的其他部分的上下文吗?tee看起来像什么?…命令…“只有一堆if语句和FTP命令(FTP上传脚本)…]2> &1 | tee/home/user/ftplog.txt实际上没有其他东西可以给予。。