String/Variable Concatenation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String/Variable Concatenation
# 1  
Old 03-07-2010
String/Variable Concatenation

Hello,

Trying to concatenate the following using bourne shell:

Code:
# !/bin/bash
# this works in bash shell e.g. get the results I am expecting
fnTmp=C$cindex.$station_0.$station_1.$station_3.$ts.tmp
#
# under !/bin/sh
# the results are not the same

Any assistance would be appreciated...
# 2  
Old 03-07-2010
Quote:
Originally Posted by LAVco
Hello,

Trying to concatenate the following using bourne shell:

Code:
# !/bin/bash
# this works in bash shell e.g. get the results I am expecting
fnTmp=C$cindex.$station_0.$station_1.$station_3.$ts.tmp
#
# under !/bin/sh
# the results are not the same


What are the results? How do they differ?

I see nothing there that will vary from one shell to another.

Unless the problem is exported variables. In a Bourne shell, values may not be exported unless you do it explicitly at each level. In bash, once a variable is exported, it doesn't need to be exported again in its children.
# 3  
Old 03-07-2010
thanks for the response..

in bash the result is C1.NL.NLDOT.NLPCP.20100307T052700.tmp

In bourne the result is .20100307T052700.tmp

---------- Post updated at 01:42 AM ---------- Previous update was at 01:40 AM ----------

additional info..

Code:
#
cfgFilePath="../etc/app.conf"
#
# configuration section constants
cfgSec0="STATION"
cfgSec1="NETWORK-CAMERA"
cfgSecCnt1=`grep -c $cfgSec1 $cfgFilePath`
cfgSec2="DATA-CENTER"
cfgSecCnt2=`grep -c $cfgSec2 $cfgFilePath`
#
# timestamp
ts=`date --utc +%Y%m%dT%H%M00`
#
# parse station parameters
set -- $(awk '/\['$cfgSec0']/{f=1;next}/\[/{f=0}f{print $NF}' $cfgFilePath)
#
sta_region=$1
sta_org=$2
sta_name=$3
sta_clientid=$4
sta_netid=$5
#
for (( i = 0 ; i < cfgSecCnt1 ; i++ )); do
	# set camera index value
	cindex=$[$i + 1]
	echo $cindex
	echo $sta_region 
	echo $sta_org 
	echo $sta_name 
	echo $sta_clientid
	echo $sta_netid
	echo ""
	# create temp and actual file name
	fnTmp=("C"$cindex"."$sta_region"."$sta_org"."$sta_clientid"."$ts".tmp")
	#
	#echo "C $cindex . $sta_region . $sta_org"
	echo $fnTmp
	#
	# parse network-camera parameters
	set -- $(awk '/\['$cfgSec1'.C'$cindex']/{f=1;next}/\[/{f=0}f{print $NF}' $cfgFilePath)
	#
	cam_uid=$1
	cam_pwd=$2
	cam_host=$3
	cam_cmd=$4
	echo $4
done

# 4  
Old 03-07-2010

Have the variables been exported in the calling script?


---------- Post updated at 12:48 AM ---------- Previous update was at 12:43 AM ----------

Quote:
Originally Posted by LAVco
thanks for the response..

in bash the result is C1.NL.NLDOT.NLPCP.20100307T052700.tmp

In bourne the result is .20100307T052700.tmp

---------- Post updated at 01:42 AM ---------- Previous update was at 01:40 AM ----------

additional info..

Code:
for (( i = 0 ; i < cfgSecCnt1 ; i++ )); do


That is not Bourne shell syntax, so it cannot work in a Bourne shell.
Quote:
Code:
	fnTmp=("C"$cindex"."$sta_region"."$sta_org"."$sta_clientid"."$ts".tmp")


That is not Bourne shell syntax.
# 5  
Old 03-07-2010
no (not that I am aware of), single script being called within an ARM SBC.

---------- Post updated at 01:52 AM ---------- Previous update was at 01:48 AM ----------

oddly the loop works before I tried concatenating the string. variables are being read until I try to build the filename.

I have #!/bin/sh declared and no errors are being generated.

---------- Post updated at 02:07 AM ---------- Previous update was at 01:52 AM ----------

I've removed the the 'for' loop and getting the same result.

Code:
#
cfgFilePath="../etc/app.conf"
#
# configuration section constants
cfgSec0="STATION"
cfgSec1="NETWORK-CAMERA"
cfgSecCnt1=`grep -c $cfgSec1 $cfgFilePath`
cfgSec2="DATA-CENTER"
cfgSecCnt2=`grep -c $cfgSec2 $cfgFilePath`
#
# timestamp
ts=`date --utc +%Y%m%dT%H%M00`
#
# parse station parameters
set -- $(awk '/\['$cfgSec0']/{f=1;next}/\[/{f=0}f{print $NF}' $cfgFilePath)
#
staRegion=$1
staOrg=$2
staName=$3
staClientId=$4
staNetId=$5
#
echo $staRegion 
echo $staOrg 
echo $staName 
echo $staClientId
echo $staNetId
echo ""
# create temp and actual file name
fnTmp=C1.$staRegion.$staOrg.$staClientId.$ts.tmp
#
echo $fnTmp

# 6  
Old 03-07-2010
Quote:
Originally Posted by LAVco
Code:
fnTmp=("C"$cindex"."$sta_region"."$sta_org"."$sta_clientid"."$ts".tmp")

You are declaring an array variable, and you are quoting all the wrong parts. If anything, you need to quote the variables, not the constants. But you don't need to do that in a scalar assignment. You would have to in an array assignment using var=(...).
Quote:
oddly the loop works before I tried concatenating the string. variables are being read until I try to build the filename.

I have #!/bin/sh declared and no errors are being generated.

Then /bin/sh is not a Bourne shell.

The correct way to concatenate the variables is:
Code:
fnTmp=C$cindex.$sta_region.$sta_org.$sta_clientid.$ts.tmp

# 7  
Old 03-07-2010
thanks for the clarification. The () was me playing around trying to get it to output and I've tried the way you suggested previously with no success.

---------- Post updated at 02:22 AM ---------- Previous update was at 02:16 AM ----------

ahh i think I found the problem but not sure how to fix it...it's reading a confg file as per awk cmd but I think it's including the crlf.

---------- Post updated at 02:42 AM ---------- Previous update was at 02:22 AM ----------

humm adding the following fixed the problem:

Code:
# parse station parameters
set -- $(awk '/\['$cfgSec0']/{f=1;next}/\[/{f=0}f{print $NF}' $cfgFilePath)
#
staRegion=`echo $1 | sed 's/.$//'`
staOrg=`echo $2 | sed 's/.$//'`
staName=`echo $3 | sed 's/.$//'`
staClientId=`echo $4 | sed 's/.$//'`
staNetId=`echo $5 | sed 's/.$//'`

just not very clean per say
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with String concatenation

I have a script which is migrated from AIX to Linux & now while running it is no able to concatenate string values The string concatenation step under while loop is not displaying desired result Please find below the piece of code: while read EXT_FILE ; do EXT_FILE=$EXT_FILE.ext.sent echo... (7 Replies)
Discussion started by: PreetArul
7 Replies

2. Shell Programming and Scripting

Group by and string concatenation

Hi, I was trying to work on a file which had the following data format 1 hi 1 this 1 is 1 john 2 hello 3 test 3 case the expected output file is the below 1 hi, this, is, john 2 hello 3 test, case I tried using awk or while read, but I couldnt... (13 Replies)
Discussion started by: karthikbhuvana
13 Replies

3. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

4. Shell Programming and Scripting

String concatenation

Hi, I have two files. cat file.txt a b c d cat file1.txt j k l m I need the output as a:j (12 Replies)
Discussion started by: nareshkumar522
12 Replies

5. Shell Programming and Scripting

String / Variable Concatenation

Hi all, I'm trying to build a variable name automatically through a for loop for a script I'm working on, basically I want to build the variables named: $JVM_HOME0 or $JVM_HOME1 so that I can loop through some file copy/deletes and a server restart once completed. With the code below, I get this... (3 Replies)
Discussion started by: hydroponx
3 Replies

6. Shell Programming and Scripting

String concatenation problems

#! /bin/csh set tt=12345_UMR_BH452_3_2.txt set rr=`echo $tt | cut -d_ -f1` set rr1=welcome set ff=$rr $rr1 echo $ff why $ff returned only 12345 and not 12345welcome? thanks (2 Replies)
Discussion started by: jdsignature88
2 Replies

7. Shell Programming and Scripting

String Concatenation

Hi All, I need to concatenate the values in the array into a variable. Currently the code is : for (( i=1 ; i <= $minCount ; i++ )) do var="${var}""${sample_file}" done The output is : /tmp/1/tmp/2/tmp/3/tmp/4/tmp/5/tmp/6/tmp/7/tmp/8/tmp/9/tmp/10 I need a space between... (1 Reply)
Discussion started by: sh_kk
1 Replies

8. Shell Programming and Scripting

String concatenation with spaces

Hi, I have a variable $ID=40 and I need to build a string like 40 40 40 40 40 40 so repeating ID 'n' times separated by spaces. Any help? Thanks Sarah (2 Replies)
Discussion started by: f_o_555
2 Replies

9. Shell Programming and Scripting

Help concatenation string and variable

Hello, in my script i have this lines of code in a while cycle: .. let j=i+1 t_prod_$i = `cat myfile.csv | grep world | cut -d ";" -f$j` let i+=1 ... So if i try an echo $t_prod_$i at the end of the cycle i cannot see the right value obtained by `cat myfile.csv | grep world |... (5 Replies)
Discussion started by: drain
5 Replies

10. UNIX for Dummies Questions & Answers

string concatenation

my input file contains thousands of lines like below 234A dept of education 9788 dept of commerce 8677 dept of engineering How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line 234A:dept of education 9788:dept of commerce 8677:dept of engineering (7 Replies)
Discussion started by: systemsb
7 Replies
Login or Register to Ask a Question