下面的代码工作正常,直到连接(最后第二步) - 我需要将 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/