Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-03-2015, 10:28 AM   #1
LQ Newbie
Registered: May 2015
Posts: 2

Rep: Reputation: Disabled
Angry String concatenation in UNIX / shell script


I am new to UNIX - i wrote this below script based on the requirement. But i am stuck at the concatenation (at the second last step of the code)

The below code is working fine till the concatenation(second last step) - I need to concatenate Hello to the "physId" e.g. - The filename is UM123456789.20150503 - i am extracting M123456789 and i need to append "HELO" to it at the end. But as per the below script - when i am using the concatenation, it is overwriting the M123456789 and the output thus becomes HELO456789. I am trying to get the output as - M123456789HELO - where am i going wrong?

on the terminal i checked - echo $0 and it gave -> /bin/sh. Hence i wrote the below code.

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"
echo "$key"
Old 05-03-2015, 11:25 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,489

Rep: Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310Reputation: 4310
please use [code]here comes your script[/code] to keep formatting.
# in general do not use
var1=$(echo ${var2})
# but simply
so your script should look like:
set -xv

echo "$filepath"
echo "$filenameext"
echo "$file"
echo "$physId"
echo "$key"
but obviously this is not what you want, because it will not fix the error you mentioned.

And also there are other problems in that script, for example:
1. it works only if there was only one single file in that directory
2. the line filepath=$(echo ${absolutePath%.*}) will first remove .* from the end of your absolutePath and than evaluate /abc/data/abc_unix/stg/decrypt/* - as the only single file in that dir. Is this what you really want? (That's why I used () in that line)

Actually I suggest you to put
set -xv
at the beginning of the script (and it still won't fix your problem) but you will see what's happening.\
Old 05-03-2015, 12:06 PM   #3
LQ Newbie
Registered: May 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
String concatenation in UNIX / shell script

not working - tried with the code it says - command not found.
Old 05-03-2015, 12:57 PM   #4
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,844

Rep: Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477
there is a typeo: "#!bin/sh" should be "#!/bin/sh"


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script: confusing concatenation of explicit string and variable itayshom Linux - Software 4 11-21-2014 05:28 PM
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 08:51 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-21-2007 12:14 AM
Shell string concatenation Quakeboy02 Programming 4 04-26-2007 12:11 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration