Bash:语法错误:重定向意外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2462317/
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
Bash: Syntax error: redirection unexpected
提问by Open the way
I do this in a script:
我在脚本中执行此操作:
read direc <<< $(basename `pwd`)
and I get:
我得到:
Syntax error: redirection unexpected
in an ubuntu machine
在 ubuntu 机器上
/bin/bash --version
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)
while I do not get this error in another suse machine:
虽然我在另一台 suse 机器上没有收到此错误:
/bin/bash --version
GNU bash, version 3.2.39(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
Why the error?
为什么会出错?
回答by John Kugelman
Does your script reference /bin/bash
or /bin/sh
in its hash bang line? The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh
then your script will be using a different shell than you expect. Dash does not have the <<<
redirection operator.
您的脚本是否引用/bin/bash
或/bin/sh
在其散列行中?Ubuntu 中的默认系统 shell 是dash,而不是bash,因此如果您有,#!/bin/sh
那么您的脚本将使用与您预期不同的 shell。Dash 没有<<<
重定向操作符。
回答by kemicofa ghost
Docker:
码头工人:
I was getting this problem from my Dockerfile as I had:
我从我的 Dockerfile 中遇到了这个问题:
RUN bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
However, according to this issue, it was solved:
但是,根据这个问题,它被解决了:
The execform makes it possible to avoid shell string munging, and to
RUN
commands using a base image that does not contain/bin/sh
.Note
To use a different shell, other than
/bin/sh
, use the execform passing in the desired shell. For example,RUN ["/bin/bash", "-c", "echo hello"]
在EXEC形式使得能够避免壳串改写(munging),并
RUN
使用不包含基础图像的命令/bin/sh
。笔记
要使用除 之外的其他外壳程序,请
/bin/sh
使用传入所需外壳程序的exec形式。例如,RUN ["/bin/bash", "-c", "echo hello"]
Solution:
解决方案:
RUN ["/bin/bash", "-c", "bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)"]
Notice the quotes around eachparameter.
注意每个参数周围的引号。
回答by Chris Pietschmann
If you're using the following to run your script:
如果您使用以下内容来运行您的脚本:
sudo sh ./script.sh
Then you'll want to use the following instead:
然后,您将需要使用以下内容:
sudo bash ./script.sh
The reason for this is that Bash is not the default shell for Ubuntu. So, if you use "sh" then it will just use the default shell; which is actually Dash. This will happen regardless if you have #!/bin/bash
at the top of your script. As a result, you will need to explicitly specify to use bash
as shown above, and your script should run at expected.
原因是 Bash 不是 Ubuntu 的默认 shell。所以,如果你使用“sh”,那么它只会使用默认的 shell;这实际上是 Dash。无论您是否#!/bin/bash
在脚本的顶部,这都会发生。因此,您需要明确指定使用bash
如上所示,并且您的脚本应按预期运行。
Dash doesn't support redirects the same as Bash.
Dash 不支持与 Bash 相同的重定向。
回答by AmirHossein
You can get the output of that command and put it in a variable. then use heredoc
. for example:
您可以获取该命令的输出并将其放入变量中。然后使用heredoc
. 例如:
nc -l -p 80 <<< "tested like a charm";
can be written like:
可以写成:
nc -l -p 80 <<EOF
tested like a charm
EOF
and like this (this is what you want):
像这样(这就是你想要的):
text="tested like a charm"
nc -l -p 80 <<EOF
$text
EOF
Practical example in busybox
under docker
container:
容器busybox
下的实际示例docker
:
kasra@ubuntu:~$ docker run --rm -it busybox
/ # nc -l -p 80 <<< "tested like a charm";
sh: syntax error: unexpected redirection
/ # nc -l -p 80 <<EOL
> tested like a charm
> EOL
^Cpunt! => socket listening, no errors. ^Cpunt! is result of CTRL+C signal.
/ # text="tested like a charm"
/ # nc -l -p 80 <<EOF
> $text
> EOF
^Cpunt!
回答by ghostdog74
do it the simpler way,
用更简单的方法做,
direc=$(basename `pwd`)
Or use the shell
或者使用外壳
$ direc=${PWD##*/}
回答by Motin
Another reason to the error may be if you are running a cron job that updates a subversion working copy and then has attempted to run a versioned script that was in a conflicted state after the update...
错误的另一个原因可能是,如果您正在运行更新 subversion 工作副本的 cron 作业,然后尝试运行更新后处于冲突状态的版本化脚本...
回答by Michael Mather
On my machine, if I run a script directly, the default is bash
.
在我的机器上,如果我直接运行脚本,默认是bash
.
If I run it with sudo
, the default is sh
.
如果我用 运行它sudo
,默认是sh
.
That's why I was hitting this problem when I used sudo
.
这就是为什么我在使用sudo
.
回答by Sijeesh
Before running the script, you should check first line of the shell script for the interpreter.
在运行脚本之前,您应该检查解释器的 shell 脚本的第一行。
Eg: if scripts starts with /bin/bash , run the script using the below command "bash script_name.sh"
例如:如果脚本以 /bin/bash 开头,请使用以下命令“bash script_name.sh”运行脚本
if script starts with /bin/sh, run the script using the below command "sh script_name.sh"
如果脚本以 /bin/sh 开头,请使用以下命令“sh script_name.sh”运行脚本
./sample.sh - This will detect the interpreter from the first line of the script and run.
./sample.sh - 这将从脚本的第一行检测解释器并运行。
Different Linux distributions having different shells as default.
不同的 Linux 发行版具有不同的默认 shell。
回答by Mansur Ali
In my case error is because i have put ">>" twice
在我的情况下,错误是因为我已经把“>>”两次
mongodump --db=$DB_NAME --collection=$col --out=$BACKUP_LOCATION/$DB_NAME-$BACKUP_DATE >> >> $LOG_PATH
i just correct it as
我只是将其更正为
mongodump --db=$DB_NAME --collection=$col --out=$BACKUP_LOCATION/$DB_NAME-$BACKUP_DATE >> $LOG_PATH