Linux 语法错误:重定向意外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20179531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error: redirection unexpected
提问by Jimmy
I ran a deployment script to setup my server as root. Then I tried to run another script called test.sh which had the following lines in it:
我运行了一个部署脚本来以 root 身份设置我的服务器。然后我尝试运行另一个名为 test.sh 的脚本,其中包含以下几行:
# Logging
exec > >(tee -a /var/log/test_full.log)
exec 2> >(tee -a /var/log/test_error.log)
However when I try this I get the following error:
但是,当我尝试此操作时,出现以下错误:
test.sh: 19: test.sh: Syntax error: redirection unexpected
What might be causing this issue do you think? I've not heard of this error before.
您认为是什么原因导致了这个问题?我以前没有听说过这个错误。
采纳答案by Louis
This answersolves your problem, assuming that your script snippet is complete.
这个答案解决了你的问题,假设你的脚本片段是完整的。
In brief, you are running your script through dash
, not bash
. The solution is as simple as adding the necessary #!/bin/bash
简而言之,您是通过dash
而不是bash
. 解决方案就像添加必要的一样简单#!/bin/bash
What a system runs by default if the #!
is missing varies from system to system. On my system, I don't get your error because a shell that understands your redirections is run by default. I've had to simulate the case where dash
would be the default shell to reproduce your error.
如果#!
缺失,系统默认运行的内容因系统而异。在我的系统上,我没有收到您的错误消息,因为默认情况下会运行能够理解您的重定向的 shell。我不得不模拟dash
将默认 shell 重现您的错误的情况。
回答by damienfrancois
Assuming you run your script with ./myscript
, make sure your scripts starts with
假设您使用 运行脚本./myscript
,请确保您的脚本以
#!/bin/bash
and not #!/bin/sh
or anything else. The error suggests that another shell than Bash is used.
而不是#!/bin/sh
或其他任何东西。该错误表明使用了除 Bash 之外的另一个 shell。
If your script indeed do, check that /bin/bash
is not a symbolic link and that it indeed is Bash with /bin/bash --version
.
如果您的脚本确实如此,请检查它/bin/bash
是否不是符号链接,并且它确实是带有/bin/bash --version
.