shell:奇怪的字符串连接行为 [英] shell: strange string concatenation behavior

查看:87
本文介绍了shell:奇怪的字符串连接行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样写:

#!/bin/bash
while read line
do
  echo line ${line}
  pdbfile=${line}.pdb  
  echo pdbfile ${pdbfile}
done < myfile

结果是:

line pdb8mhtA
.pdbfile pdb8mhtA

应该是

line pdb8mhtA
pdbfile pdb8mhtA.pdb

这是怎么了?为什么字符串连接不起作用?以及为什么在行首出现奇怪的点?
我替换为pdbfile=${line}'.pdb'.那不会改变结果.

What's wrong with this? Why the string concatenation does not work? And why the strange dot at the beginning of the line?
I replace with pdbfile=${line}'.pdb'. That does not change the result.

推荐答案

字符串转到行首"是$line中回车符的征兆,您可以通过许多其他方式使用tr管道到您的文件:

The "string goes to the beginning of the line" is symptomatic of a carriage return in your $line that you can among many other ways remove with a tr pipe to your file:

while IFS= read -r line
do
  echo "line ${line}"
  pdbfile=${line}.pdb  
  echo "pdbfile ${pdbfile}"
done < <(tr -d '\r' <file)

这篇关于shell:奇怪的字符串连接行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆