bash - UNIX/shell 脚本中的字符串连接

标签 bash shell unix shebang

下面的代码工作正常,直到连接(最后第二步) - 我需要将 Hello 连接到“physId” 例如- 文件名是 UM123456789.20150503 - 我正在提取 M123456789,我需要在末尾附加 “HELO”。但根据下面的脚本 - 当我使用串联时,它会覆盖 M123456789,输出因此变为 HELO456789。我试图将输出设为 - M123456789HELO - 我哪里出错了?

#!bin/sh  
absolutePath=/abc/data/abc_unix/stg/decrypt/*.*  
filepath=$(echo ${absolutePath%.*})  
echo "$filepath"  
filenameext=$(echo ${filepath#/abc*decrypt/})  
echo "$filenameext"  
file=$(echo ${filenameext#.*})  
echo "$file"  
extract_physId=$(echo ${file:1:9})  
physId=$(echo ${extract_physId})  
echo "$physId"  
key="$physId"HELO  
echo "$key"  

最佳答案

字符串的开头被 HELO 覆盖,因为字符串以回车符结尾。在输入文件上运行 dos2unix,或 sed 's/\r$//'


顺便说一句,里面有很多不必要的echo。我会提供重写:

#!bin/sh  
for file in /abc/data/abc_unix/stg/decrypt/*.*; do
    filename=$(basename "$file")   # remove the directory
    filename=${filename%.*}        # remove the extension
    physId=${filename#?}           # remove the first char
    key="${physId}HELO"
    echo "$key"
done

关于bash - UNIX/shell 脚本中的字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30011932/

相关文章:

macos - 从文件尾部修剪 N 行的简单方法是什么(不使用 'head' )?

bash - 使用shell脚本登录监控并发送邮件

linux - 如何使用 shell 脚本验证用户密码?

BASH - 将 CURL 命令拆分为多行

windows - 使用 SSH 在 Windows 中远程调用显示桌面

bash - sed 打印一行,其中至少有 3 位数字在第 4 位数字后重复数字

linux - 什么是零信号?

需要 UNIX 服务器上保存的 CSV 文件的 Java 前端

linux - AWK 将列的总和除以文件中的行数

linux - 在 bash 脚本中使用 for 循环在多行命令中插入行