bash 收到“不明确的重定向”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2462385/
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
Getting an "ambiguous redirect" error
提问by Open the way
The following line in my Bash script
我的 Bash 脚本中的以下行
echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
gives me this error:
给我这个错误:
line 46: ${OUPUT_RESULTS}: ambiguous redirect
Why?
为什么?
回答by nobar
Bash can be pretty obtuse sometimes.
Bash 有时可能非常迟钝。
The following commands all return different error messages for basically the same error:
以下命令都针对基本相同的错误返回不同的错误消息:
$ echo hello >
bash: syntax error near unexpected token `newline`
$ echo hello > ${NONEXISTENT}
bash: ${NONEXISTENT}: ambiguous redirect
$ echo hello > "${NONEXISTENT}"
bash: : No such file or directory
Adding quotes around the variable seems to be a good way to deal with the "ambiguous redirect" message: You tend to get a better message when you've made a typing mistake -- and when the error is due to spaces in the filename, using quotes is the fix.
在变量周围添加引号似乎是处理“模糊重定向”消息的好方法:当您输入错误时,您往往会得到更好的消息——并且当错误是由于文件名中的空格引起时,使用引号是解决办法。
回答by JUST MY correct OPINION
Do you have a variable named OUPUT_RESULTS
or is it the more likely OUTPUT_RESULTS
?
你有一个命名的变量OUPUT_RESULTS
还是更有可能OUTPUT_RESULTS
?
michael@isolde:~/junk$ ABC=junk.txt
michael@isolde:~/junk$ echo "Booger" > $ABC
michael@isolde:~/junk$ echo "Booger" >> $ABB
bash: $ABB: ambiguous redirect
michael@isolde:~/junk$
回答by ghostdog74
put quotes around your variable. If it happens to have spaces, it will give you "ambiguous redirect" as well. also check your spelling
在你的变量周围加上引号。如果它碰巧有空格,它也会给你“模糊重定向”。还要检查你的拼写
echo $AAAA" "$DDDD" "$MOL_TAG >> "${OUPUT_RESULTS}"
eg of ambiguous redirect
例如不明确的重定向
$ var="file with spaces"
$ echo $AAAA" "$DDDD" "$MOL_TAG >> ${var}
bash: ${var}: ambiguous redirect
$ echo $AAAA" "$DDDD" "$MOL_TAG >> "${var}"
$ cat file\ with\ spaces
aaaa dddd mol_tag
回答by Thomas
Does the path specified in ${OUPUT_RESULTS} contain any whitespace characters? If so, you may want to consider using ... >> "${OUPUT_RESULTS}"
(using quotes).
${OUTPUT_RESULTS} 中指定的路径是否包含任何空白字符?如果是这样,您可能需要考虑使用... >> "${OUPUT_RESULTS}"
(使用引号)。
(You may also want to consider renaming your variable to ${OUTPUT_RESULTS}
)
(您可能还想考虑将变量重命名为${OUTPUT_RESULTS}
)
回答by AixNPanes
I've recently found that blanks in the name of the redirect file will cause the "ambiguous redirect" message.
我最近发现重定向文件名称中的空格会导致“不明确的重定向”消息。
For example if you redirect to application$(date +%Y%m%d%k%M%S).log
and you specify the wrong formatting characters, the redirect will fail before 10 AM for example. If however, you used application$(date +%Y%m%d%H%M%S).log
it would succeed. This is because the %k
format yields ' 9'
for 9AM where %H
yields '09'
for 9AM.
例如,如果您重定向到application$(date +%Y%m%d%k%M%S).log
并指定了错误的格式字符,则重定向将在上午 10 点之前失败,例如。但是,如果你使用application$(date +%Y%m%d%H%M%S).log
它就会成功。这是因为%k
格式产量' 9'
为上午9时,其中%H
产量'09'
为上午9时。
echo $(date +%Y%m%d%k%M%S)
gives 20140626 95138
echo $(date +%Y%m%d%k%M%S)
给 20140626 95138
echo $(date +%Y%m%d%H%M%S)
gives 20140626095138
echo $(date +%Y%m%d%H%M%S)
给 20140626095138
The erroneous date might give something like:
错误的日期可能是这样的:
echo "a" > myapp20140626 95138.log
where the following is what would be desired:
以下是所需的内容:
echo "a" > myapp20140626095138.log
回答by Jim
One other thing that can cause "ambiguous redirect" is \t
\n
\r
in the variable name you are writing too
可能导致“模糊重定向”的另一件事是\t
\n
\r
您正在编写的变量名称
Maybe not \n\r
? But err on the side of caution
也许不是\n\r
?但请谨慎行事
Try this
尝试这个
echo "a" > ${output_name//[$'\t\n\r']}
I got hit with this one while parsing HTML, Tabs \t
at the beginning of the line.
我在解析 HTML 时遇到了这个问题,行首的 Tabs \t
。
回答by Wayne Workman
I just had this error in a bash script. The issue was an accidental \ at the end of the previous line that was giving an error.
我刚刚在 bash 脚本中遇到了这个错误。问题是在上一行的末尾意外出现了一个 \ ,它给出了错误。
回答by enharmonic
If your script's redirect contains a variable, and the script body defines that variable in a section enclosed by parenthesis, you will get the "ambiguous redirect" error. Here's a reproducible example:
如果您的脚本的重定向包含一个变量,并且脚本主体在括号括起来的部分中定义了该变量,您将收到“模糊重定向”错误。这是一个可重现的示例:
vim a.sh
to create the script- edit script to contain
(logit="/home/ubuntu/test.log" && echo "a") >> ${logit}
chmod +x a.sh
to make it executablea.sh
vim a.sh
创建脚本- 编辑脚本以包含
(logit="/home/ubuntu/test.log" && echo "a") >> ${logit}
chmod +x a.sh
使其可执行a.sh
If you do this, you will get "/home/ubuntu/a.sh: line 1: $logit: ambiguous redirect". This is because
如果你这样做,你会得到“/home/ubuntu/a.sh: line 1: $logit: ambiguous redirect”。这是因为
"Placing a list of commands between parentheses causes a subshell to be created, and each of the commands in list to be executed in that subshell, without removing non-exported variables. Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes."
“在括号之间放置一个命令列表会导致创建一个子shell,并且列表中的每个命令都将在该子shell中执行,而不删除未导出的变量。由于列表是在子shell中执行的,因此不会保留变量分配在子shell完成后生效。”
From Using parenthesis to group and expand expressions
To correct this, you can modify the script in step 2 to define the variable outside the parenthesis: logit="/home/ubuntu/test.log" && (echo "a") >> $logit
要更正此问题,您可以修改第 2 步中的脚本以定义括号外的变量: logit="/home/ubuntu/test.log" && (echo "a") >> $logit
回答by Anusree
if you are using a variable name in the shell command, you must concatenate it with +
sign.
如果在 shell 命令中使用变量名,则必须将其与+
符号连接。
for example :
例如 :
if you have two files, and you are not going to hard code the file name, instead you want to use the variable name "input.txt" = x
"output.txt" = y
如果您有两个文件,并且您不打算对文件名进行硬编码,而是想使用变量名 "input.txt" = x
"output.txt" = y
then ('shell command within quotes' + x > + y)
then ('引号内的 shell 命令' + x > + y)
it will work this way especially if you are using this inside a python program with os.system command probably
它会以这种方式工作,特别是如果您在带有 os.system 命令的 python 程序中使用它可能
回答by Rahul Rajput
This might be the case too.
这也可能是这种情况。
you have not specified the file in a variable and redirecting output to it, then bash will throw this error.
您没有在变量中指定文件并将输出重定向到它,那么 bash 将抛出此错误。
files=`ls`
out_file = /path/to/output_file.t
for i in `echo "$files"`;
do
content=`cat $i`
echo "${content} ${i}" >> ${out_file}
done
out_filevariable is not set up correctly so keep an eye on this too. BTW this code is printing all the content and its filename on the console.
out_file变量设置不正确,所以也要注意这一点。顺便说一句,此代码正在控制台上打印所有内容及其文件名。