使用 tee (或其他命令)将错误或消息记录到文件,但仅当变量 x==“yes” 时才有效。
我尝试使用 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"
I tried using an if statement but this doesn't work as the tee command has the two brackets, one at the start and one at the end.
I tried something like this, which didn't work either
if [[ "$logging" == "yes" ]]; then
ftpt="2>&1 | tee $ftpLF"
else
ftpt=""
fi
} "$ftpt"
Error:
./ftp.sh: line 149: syntax error near unexpected token `"$ftpt"'
./ftp.sh: line 149: `} "$ftpt"'
I use this at the moment but I have no option of turning it on/off, it's just always on
{
....commands....
} 2>&1 | tee "$ftpLF"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以一致地引用内容,一种选择是使用
eval
强制 Bash 计算命令的添加部分:另一种选择是使用实际的命名函数:
后者可能是首选选项,因为您不必担心奇怪的引用问题或其他类似问题。
One option, if you can consistently quote things, is to use
eval
to force Bash to evaluate the added portions of the command:Another option would be to use an actual named function:
The latter is probably the preferred option, since you don't have to worry about weird quoting issues or other such.