Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用tee(或其他命令)将错误或消息记录到文件中,但仅当变量x==”时;是”;_Bash_Shell_Logging_Tee - Fatal编程技术网

Bash 使用tee(或其他命令)将错误或消息记录到文件中,但仅当变量x==”时;是”;

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语句,但这不起作用,因为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: `} "$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实际上没有其他东西可以给予。。