Ubuntu(14&16)Bash错误,输入包含小写字母“的printf循环;";人物

Ubuntu(14&16)Bash错误,输入包含小写字母“的printf循环;";人物,bash,printf,ubuntu-14.04,ubuntu-16.04,printf-debugging,Bash,Printf,Ubuntu 14.04,Ubuntu 16.04,Printf Debugging,我有一些bash脚本,我已经在Ubuntu 14.04和16.04上运行了一年多了。最近的一些Ubuntu更新破坏了bash,我不知道如何解决这个问题 例如: #!/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 $ IFS=$' \t\n

我有一些bash脚本,我已经在Ubuntu 14.04和16.04上运行了一年多了。最近的一些Ubuntu更新破坏了bash,我不知道如何解决这个问题

例如:

#!/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
$ IFS=$' \t\n' read line <<< foon
$ printf '%q\n' "$line"
foon
然后,我的脚本在文件中循环执行printf语句,如下所示,但这是生成的输出

"~jetmo" 0;
"~spamme" 0;
"~baidu" 0;
myinput.txt的示例内容

jetmon
spammen
baidu
包含小写n字符的输入行被删除?? 我是否错过了bash中发生的一些重大变化,这是一周前开始的,现在让我发疯了

我试过换衣服/垃圾桶/扑向#/具有相同结果的bin/sh

#!/bin/bash
INPUT=myinput.txt
OUTPUT=myoutput.txt
ACTION1="0;"
while read LINE
do
printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT
done < $INPUT
#!/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
还尝试了这种循环输入文件的方法,但仍然得到相同的结果

#!/bin/bash
INPUT=myinput.txt
OUTPUT=myoutput.txt
ACTION1="0;"
while read LINE
do
printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT
done < $INPUT
#!/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
现在我得到了这个输出

-e -n "~jetmo" 0;
-e -n "~spamme" 0;
-e -n "~baidu" 0;
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
运行hextump-cmyinport.txt将提供此输出

-e -n "~jetmo" 0;
-e -n "~spamme" 0;
-e -n "~baidu" 0;
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
还按照Michael的建议将所有变量名更改为小写,但仍然得到相同的结果

#!/bin/bash
INPUT=myinput.txt
OUTPUT=myoutput.txt
ACTION1="0;"
while read LINE
do
printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT
done < $INPUT
#!/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

解决方案 谜团解开了

谢谢大家,但是我把它钉在了头上。我的脚本前面隐藏了一个
IFS=$'\n'

由于Nahuel和Charles的建议,我现在有了这个最终的、更好、更安全的printf格式,它100%工作。。。非常感谢你们

#!/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
#!/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
#/bin/bash
input=myinput.txt
输出=myoutput.txt
action1=“0;”
而IFS=读取-r行
做
printf'\t“~%s”\t\t%s\n'${LINE}”“$ACTION1”>>“$output”
完成<$input1

使用
%s
插入带有printf的字符串更安全,例如,如果它可以包含
%

printf "\t\"~%s\"\t\t%s\n" "${LINE}" "$ACTION1" >> $OUTPUT
编辑以下注释,在第一个参数中使用单引号,因为并没有变量展开

printf '\t"~%s"\t\t%s\n' "${LINE}" "$ACTION1" >> "$OUTPUT"

使用
%s
插入带有printf的字符串更安全,例如,如果它可以包含
%

printf "\t\"~%s\"\t\t%s\n" "${LINE}" "$ACTION1" >> $OUTPUT
编辑以下注释,在第一个参数中使用单引号,因为并没有变量展开

printf '\t"~%s"\t\t%s\n' "${LINE}" "$ACTION1" >> "$OUTPUT"

如果内部字段分隔符(
$IFS
)包含字母
n
,则可能发生这种情况:

$ IFS=$' \t\nn' read line <<< foon
$ printf '%q\n' "$line"
foo
例如:

#!/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
$ IFS=$' \t\n' read line <<< foon
$ printf '%q\n' "$line"
foon

$IFS=$”\t\n“读取行如果内部字段分隔符(
$IFS
)包含字母
n
,则可能发生这种情况:

$ IFS=$' \t\nn' read line <<< foon
$ printf '%q\n' "$line"
foo
例如:

#!/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
$ IFS=$' \t\n' read line <<< foon
$ printf '%q\n' "$line"
foon

$IFS=$”\t\n“读取行解决方案
谜团解开了

谢谢大家,但是我把它钉在了头上。我的脚本前面隐藏了一个
IFS=$'\n'

由于Nahuel和Charles的建议,我现在有了这个最终的、更好、更安全的printf格式,它100%工作。。。非常感谢你们

#!/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
#!/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
#/bin/bash
input=myinput.txt
输出=myoutput.txt
action1=“0;”
而IFS=读取-r行
做
printf'\t“~%s”\t\t%s\n'${LINE}”“$ACTION1”>>“$output”
完成<$input1

解决方案 谜团解开了

谢谢大家,但是我把它钉在了头上。我的脚本前面隐藏了一个
IFS=$'\n'

由于Nahuel和Charles的建议,我现在有了这个最终的、更好、更安全的printf格式,它100%工作。。。非常感谢你们

#!/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
#!/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
#/bin/bash
input=myinput.txt
输出=myoutput.txt
action1=“0;”
而IFS=读取-r行
做
printf'\t“~%s”\t\t%s\n'${LINE}”“$ACTION1”>>“$output”
完成<$input1

尝试检查输入文件中是否有非UNIX行结尾。它在Ubuntu 14.04上对我来说很好。您还可以用
echo-e-n
替换
printf
。在ubuntu 16.04上对我很好用。。。这行吗<代码>printf'\t“~%s”\t\t%s\n'${LINE}”“$ACTION1'
。。。也有一些建议-使用小写变量名以避免与Env变量冲突,请参阅正确的方式读取文件,并考虑使用文本处理工具,如“代码> SED000000006A65746D6F6E0A7370616D6D656E0A62 | jetmon.spammen.b | 0000001069646475A0A | aidu..| 00000016
用hexdump输出更新了我的原始问题。请尝试检查输入文件中的非UNIX行结尾。它在Ubuntu 14.04上对我来说很好。您还可以用
echo-e-n
替换
printf
。在ubuntu 16.04上对我很好用。。。这行吗<代码>printf'\t“~%s”\t\t%s\n'${LINE}”“$ACTION1'。。。也有一些建议-使用小写变量名以避免与Env变量冲突,请参阅正确的方式读取文件,并考虑使用文本处理工具,如“代码> SEDhextump-C myinput.txt的输出再次感谢Michae