简体   繁体   English

回声没有打印某些变量

[英]Echo is not printing certain variables

I'm having some problem with my code not printing certain variables even if the variables has a string in it. 我的代码有一些问题,即使变量中包含字符串,也无法打印某些变量。

$ dmisysname=$(sed -ne 148p ./3A47083/S09850724414683/002590F3851A.txt | awk -F '=' '{ print $2 }' | sed 's/ //')
$ dmibrdname=$(sed -ne 157p ./3A47083/S09850724414683/002590F3851A.txt | awk -F '=' '{ print $2 }' | sed 's/ //')
$ echo $dmisysname $dmibrdname

This should give me whatever inside $dmisysname and $dmibrdname, but the output displays this: 这应该给我$ dmisysname和$ dmibrdname内的任何内容,但是输出显示如下:

X9DRi-LN4+/X9DR3-LN4+ X9DRi-LN4 + / X9DR3-LN4 +

To make sure that the 2 variables have a string in it, I echo each variable individually. 为了确保2个变量中都有一个字符串,我分别回显了每个变量。

$ echo $dmisysname

PIO-647R-6RLN4F+-ST031 PIO-647R-6RLN4F + -ST031

$ echo $dmibrdname

X9DRi-LN4+/X9DR3-LN4+ X9DRi-LN4 + / X9DR3-LN4 +

Am I doing something wrong that has to do something with the print buffer or is there a bigger problem that I don't see? 我是在做一些必须使用打印缓冲区的错误操作吗?还是我没有看到更大的问题?

Your input file was created under windows, and as such uses CRLF line terminations. 您的输入文件是在Windows下创建的,因此使用CRLF行终止。 This causes the output from under to be confused, relative to what is expected, as per @Lurker's useful explanation . 根据@Lurker的有用解释 ,这导致的输出相对于预期的是混乱

Use the dos2unix utility to fix your file: 使用dos2unix实用工具修复文件:

$ dmisysname=$(sed -ne 148p ./3A47083/S09850724414683/002590F3851A.txt | awk -F '=' '{ print $2 }' | sed 's/ //')
$ dmibrdname=$(sed -ne 157p ./3A47083/S09850724414683/002590F3851A.txt | awk -F '=' '{ print $2 }' | sed 's/ //')
$ echo $dmisysname $dmibrdname
 X9DRi-LN4+/X9DR3-LN4+
$ dos2unix 3A47083/S09850724414683/002590F3851A.txtdos2unix: converting file 3A47083/S09850724414683/002590F3851A.txt to Unix format ...
$ dmisysname=$(sed -ne 148p ./3A47083/S09850724414683/002590F3851A.txt | awk -F '=' '{ print $2 }' | sed 's/ //')
$ dmibrdname=$(sed -ne 157p ./3A47083/S09850724414683/002590F3851A.txt | awk -F '=' '{ print $2 }' | sed 's/ //')
$ echo $dmisysname $dmibrdname
PIO-647R-6RLN4F+-ST031 X9DRi-LN4+/X9DR3-LN4+
$ 

I recreated a workable version of your file under Linux, then used unix2dos to format it as per windows. 我在Linux下重新创建了文件的可行版本,然后使用unix2dos按照Windows unix2dos进行了格式化。 Then I am able to recreate your problem. 然后,我可以重新创建您的问题。 Reverting it back using dos2unix fixes it. 使用dos2unix将其还原可修复该问题。

Each of your variables ends in a carriage return ( \\r ). 您的每个变量都以回车符( \\r )结尾。 So what happens is both are echoed, but the second one overwrites the first. 因此,发生的事情是两者都得到了回应,但是第二个覆盖了第一个。

I'll illustrate with the -e option: 我将使用-e选项进行说明:

$ echo -e "FOO\rBAR\r"
BAR

But both are being echoed. 但是两者都得到了回应。 It's just that BAR is overwriting FOO : 只是BAR正在覆盖FOO

$ echo -e "FOO\rBAR\r" | od -c
0000000   F   O   O  \r   B   A   R  \r  \n
0000011

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM