簡體   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