【问题标题】:Remove quotation marks and add file extension to filename in a shell script删除引号并将文件扩展名添加到 shell 脚本中的文件名
【发布时间】:2014-10-29 11:41:09
【问题描述】:

在我正在编写的 shell 脚本中,我想从另一个文件中读取文件名,删除引号并为其添加文件扩展名。

tmp=$(awk '/output_onefile/ {getline;print}' $cmds_file)
tmp=$(echo "$tmp" | tr -d '"')
uh3dsr_file="${tmp}.sr_uh3d"

在$cmds_file 后面的文件中,“output_onefile”后面的行中有一个文件名。文件名在引号中,我在第二行中删除。至此,一切正常。 然后我只想将 .sr_uh3d 添加到文件名中。

当我运行这样的代码并假设文件名为“Blabla_123456”时,我最终得到以下结果:

echo "$uh3dsr_file"
.sr_uh3d23456
echo "Text $uh3dsr_file"
.sr_uh3dbla_123456

它将 .sr_uh3d 放在其余部分的前面并覆盖其余部分的开头。

谁能向我解释这种行为?清楚我想要做什么吗?

【问题讨论】:

  • 您能否添加输入文件的示例?
  • 也许这样做会更简单:`uh3dsr_file = echo $tmp".sr_uh3d"

标签: shell filenames


【解决方案1】:

您的文本文件有 CRLF 行结尾,但您的工具需要 LF 行结尾。预先使用dos2unix 或使用tr 或参数替换从文本中去除CR。

【讨论】:

  • 谢谢,你是对的。从来没想过,虽然我稍后在同一个脚本中使用了 dos2unix。
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 2012-06-29
相关资源
最近更新 更多