2

我尝试使用 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"
4

1 回答 1

4

如果您可以始终如一地引用事物,则一种选择是用于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

后者可能是首选选项,因为您不必担心奇怪的引用问题或其他类似问题。

于 2011-01-11T04:03:05.377 回答