繁体   English   中英

UNIX / shell脚本中的字符串串联

[英]String concatenation in UNIX / shell script

下面的代码可以正常工作,直到进行串联(第二步)-我需要将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覆盖,因为字符串以回车结束。 在输入文件上运行dos2unixsed '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

#!/bin/sh更改为#!/bin/bash或:

更改

extract_physId=$(echo ${file:1:9}) 

extract_physId=$( echo "${file}" | cut -c1-10)

sh无法识别${file:1:9}

暂无
暂无

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

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