LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

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


Hi,

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.

#!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"
 
Old 05-03-2015, 11:25 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,512

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

absolutePath=/abc/data/abc_unix/stg/decrypt/*.*
filepath=(${absolutePath.*})
echo "$filepath"
filenameext="${filepath#/abc*decrypt/}"
echo "$filenameext"
file="${filenameext#.*}"
echo "$file"
extract_physId="${file:1:9}"
physId="${extract_physId}"
echo "$physId"
key="$physId"HELO
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
bleedblue
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
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

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


Reply


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


All times are GMT -5. The time now is 04:07 AM.

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