问题描述
我有一些bash脚本,我已经在Ubuntu 14.04和16.04上运行了一年多了. Ubuntu的一些最新更新破坏了bash,我无法弄清楚如何解决这个问题.
I have some bash scripts I have been running on Ubuntu 14.04 and 16.04 for well over a year now. Some recent Ubuntu update has broken bash and I cannot figure out how to sort this out.
示例:
#!/bin/bash
INPUT=myinput.txt
OUTPUT=myoutput.txt
ACTION1="0;"
cat $INPUT | while read LINE
do
printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT
done
然后,我的脚本按如下所示遍历文件,执行printf语句,但这是产生的输出
my script then loops through the file doing the printf statement as follows but this is the output produced
"~jetmo" 0;
"~spamme" 0;
"~baidu" 0;
myinput.txt的示例内容
example contents of myinput.txt
jetmon
spammen
baidu
包含小写n字符的输入行被剥去了?我是否错过了发生的一些重大bash变化,这种变化是在一周前开始的,现在让我发疯.
Input lines containing a lowercase n character gets stripped out ??Am I missing some major change in bash that occured, this started happening a week ago and is driving me insane now.
我尝试将#!/bin/bash更改为#!/bin/sh,结果相同.
I have tried changing #!/bin/bash to #!/bin/sh with same results.
还尝试了这种遍历输入文件的方法,但仍然得到相同的结果.
Also tried this method of looping through the input file and still get the same results.
#!/bin/bash
INPUT=myinput.txt
OUTPUT=myoutput.txt
ACTION1="0;"
while read LINE
do
printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT
done < $INPUT
使用回声尝试评论的建议
Tried suggestion from comments using echo
echo -e -n "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT
现在我得到了这个输出
-e -n "~jetmo" 0;
-e -n "~spamme" 0;
-e -n "~baidu" 0;
运行hexdump -C myinput.txt可以得到此输出
Running hexdump -C myinput.txt gives this output
00000000 6a 65 74 6d 6f 6e 0a 73 70 61 6d 6d 65 6e 0a 62 |jetmon.spammen.b|
00000010 61 69 64 75 0a 0a |aidu..|
00000016
还按照迈克尔的建议将所有变量名更改为小写,但仍然得到相同的结果.
Also changed all variable names to lowercase as suggested by Michael but still getting the same results.
#!/bin/bash
input=myinput.txt
output=myoutput.txt
action1="0;"
cat $input | while read line
do
printf "\t\"~${line}\"\t\t$action1\n" >> $output
done
解决方案神秘之谜
感谢所有人,但 https://stackoverflow.com/users/96588/l0b0 把它钉在了头上.我的脚本中早些时候隐藏了IFS=$'\n'
.
thanks to everyone, but https://stackoverflow.com/users/96588/l0b0 nailed it on the head. I had an IFS=$'\n'
hiding earlier on in my script.
由于Nahuel和Charles的建议,我现在有了printf的最终,更好,更安全的格式,并且该格式正在100%正常工作...
I now have this final, better and safer formatting of printf thanks to the recommendations of Nahuel and Charles and it is working 100% ... cannot thank you all enough.
#!/bin/bash
input=myinput.txt
output=myoutput.txt
action1="0;"
while IFS= read -r LINE
do
printf '\t"~%s"\t\t%s\n' "${LINE}" "$ACTION1" >> "$output"
done < $input1
推荐答案
如果内部字段分隔符($IFS
)包含字母n
:
This can happen if the internal field separator ($IFS
) contains the letter n
:
$ IFS=$' \t\nn' read line <<< foon
$ printf '%q\n' "$line"
foo
这是常见的错误.这是正确的值:
This is a fairly common mistake. Here is the correct value:
$ printf '%q\n' "$IFS"
$' \t\n'
示例:
$ IFS=$' \t\n' read line <<< foon
$ printf '%q\n' "$line"
foon
这篇关于Ubuntu(14& 16)来自包含小写字母"n"的输入的printf循环导致Bash错误人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!