我的Bash脚本中的以下行
echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
给我这个错误:
line 46: ${OUPUT_RESULTS}: ambiguous redirect
为什么?
OUPUT_RESULTS = "filename.log"一个,将会得到歧义的重定向。因为bash希望=操作员周围没有空格。
我的Bash脚本中的以下行
echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
给我这个错误:
line 46: ${OUPUT_RESULTS}: ambiguous redirect
为什么?
OUPUT_RESULTS = "filename.log"一个,将会得到歧义的重定向。因为bash希望=操作员周围没有空格。
Answers:
有时候Bash可能很钝。
以下命令均针对基本相同的错误返回不同的错误消息:
$ 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
在变量周围添加引号似乎是处理“歧义重定向”消息的好方法:输入错误时,以及文件名中的空格引起错误时,您往往会得到更好的消息,解决方法是使用引号。
sh -c 'echo hello >/tmp/hello' >/tmp/world works properly.
Do you have a variable named OUPUT_RESULTS or is it the more likely 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$
ABC="junk file.txt", this would still have the problem.
在变量周围加上引号。如果碰巧有空格,它也会给您“歧义重定向”。还要检查你的拼写
echo $AAAA" "$DDDD" "$MOL_TAG >> "${OUPUT_RESULTS}"
例如歧义重定向
$ 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
我最近发现重定向文件名称中的空格将导致“歧义重定向”消息。
例如,如果您重定向到application$(date +%Y%m%d%k%M%S).log并指定了错误的格式字符,则重定向将在10 AM之前失败。但是,如果您使用application$(date +%Y%m%d%H%M%S).log它会成功。这是因为%k格式会产生' 9'9AM,其中%H产生'09' 9AM。
echo $(date +%Y%m%d%k%M%S) 给 20140626 95138
echo $(date +%Y%m%d%H%M%S) 给 20140626095138
错误的日期可能会显示以下内容:
echo "a" > myapp20140626 95138.log
以下是所希望的:
echo "a" > myapp20140626095138.log
One other thing that can cause "ambiguous redirect" is \t \n \r in the variable name you are writing too
Maybe not \n\r? But err on the side of caution
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.
如果脚本的重定向包含一个变量,并且脚本主体在括号中的部分中定义了该变量,则将出现“歧义重定向”错误。这是一个可重现的示例:
vim a.sh 创建脚本(logit="/home/ubuntu/test.log" && echo "a") >> ${logit}chmod +x a.sh 使它可执行a.sh如果这样做,您将得到“ /home/ubuntu/a.sh:第1行:$ logit:歧义重定向”。这是因为
“在括号之间放置命令列表会导致创建一个子shell,并且列表中的每个命令都将在该子shell中执行,而不会删除未导出的变量。由于该列表是在子shell中执行的,因此不会保留变量分配在子shell完成后生效。”
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
if you are using a variable name in the shell command, you must concatenate it with + sign.
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
然后(“引号内的shell命令” + x> + y)
它将以这种方式工作,特别是如果您正在os.system命令中的python程序中使用它的话
也可能是这种情况。
您尚未在变量中指定文件并将其重定向到该文件,则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_file变量未正确设置,因此也请注意这一点。顺便说一句,此代码正在控制台上打印所有内容及其文件名。
${OUPUT_RESULTS}。如果您是$OUTPUT_RESULTS}