Problem in concatenating two Strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in concatenating two Strings
# 1  
Old 12-13-2011
Problem in concatenating two Strings

Hi Friends,

I'm new to shell scripting and trying to concatenate two Strings to create a filepath like string but I'm getting an unexpected result.
here is my code for 'runToneUserLoad.sh':
Code:
script_dir="$(dirname $0)"
echo "Script Dir:$script_dir"
dirtest1="/installedUtility"
cpvar="${script_dir} ${dirtest1}"
echo "cpvar: ${cpvar}"

I run this script as :
Code:
/users/apps/vbolt/tmsmari/toneUserLoadUtility/runToneUserLoad.sh

I'm getting the following result for
Code:
echo "Script Dir:$script_dir"

Script Dir:/users/apps/vbolt/tmsmari/toneUserLoadUtility

but, for some reason
Code:
echo "cpvar: ${cpvar}"

is resulting in :
/installedUtility/vbolt/tmsmari/toneUserLoadUtility

I was expecting this value to be:
/users/apps/vbolt/tmsmari/toneUserLoadUtility/installedUtility

Could you please tell why I am getting this result or I'm missing very basics of string concatenation in shell script?

Thanks
KT

Last edited by kuldeept; 12-13-2011 at 05:39 PM..
# 2  
Old 12-13-2011
Okay. The script itself looks sound in Posix Shell. Please mention what Shell you are using in the next post! Also, it always helps to know what Operating System and version you have.

I think your script contains some funny characters (like carriage-returns from a Microsoft Windows editor?).

Please post the output from the following command which is designed to make control characters visible:

Code:
sed -n l /users/apps/vbolt/tmsmari/toneUserLoadUtility/runToneUserLoad.sh

(Or whatever your script is called - the post is inconsistent). Disregard: O/P has fixed the post.


While I remember.
Quote:
script_dir="$(dirname $0)"
There will be no trailing solidus on this output. Bear this in mind when the main problem in the script is fixed.


You can prove my theory about bad characters in the script if you execute the script as:
Code:
/users/apps/vbolt/tmsmari/toneUserLoadUtility/runToneUserLoad.sh | sed -n l


Last edited by methyl; 12-13-2011 at 06:36 PM..
# 3  
Old 12-13-2011
Hi Methyl :
The machine I'm testing has linux and shell is KSH, but the machine where this code will ultimately run will have CSH.

I also executed the following command you sent (
Code:
sed -n l /users/apps/vbolt/tmsmari/toneUserLoadUtility/runToneUserLoad.sh

) and I got this output:

Code:
script_dir="$(dirname $0)"\r$
echo "Script Dir:$script_dir"\r$
dirtest1="/installedUtility"\r$
cpvar="${script_dir} ${dirtest1}"\r$
echo "cpvar: ${cpvar}"\r$

Sorry about the inconsistency in the script's name, it is actually runToneUserLoad.sh

Thanks
KT

Edited:
and the command
Code:
/users/apps/vbolt/tmsmari/toneUserLoadUtility/runToneUserLoad.sh | sed -n l

gave this output:

Code:
Script Dir:/users/apps/vbolt/tmsmari/toneUserLoadUtility\r\r$
cpvar: /users/apps/vbolt/tmsmari/toneUserLoadUtility\r /installedUtil\
ity\r\r\r$


Last edited by methyl; 12-13-2011 at 06:03 PM.. Reason: added more code tags because interested in exact output
# 4  
Old 12-13-2011
Good guess on my part!
The script is riddled with carriage-return characters (showing as \r). These usually come from using Microsoft editors (such as Notepad).
A unix text file has every line terminated with a line-feed character.
A MSDOS text file has every line terminated with two characters carriage-return then line-feed. This is a left-over from one of the greatest MSDOS mistakes.

To fix your script we can use the unix "tr" command to remove the MSDOS c**p .

Code:
cat oldscriptname | tr -d '\r' > newscriptname
chmod 755 newscriptname

When you are happy with "newscriptname", you can of course issue:
Code:
mv newscriptname oldscriptname

Don't forget to check your solidii in the final script!



Just in case you don't get what is happening here. The carriage return character moves the cursor on your screen to the far left. Thus your field you expected to be on the right hand side was jumped to the left hand side and overwrote part of what had been already displayed. It's an effect on the screen. The "sed -l" version of the output from your script showed that the script itself is basically sound but that you need to pay some attention to solidii and the extra space character. The carriage-return characters just messed-up the output.


Important: You are destined to become a great Systems Administrator. You can present a problem clearly and follow instructions. Good luck in your future career.

Btw: Post #3 mentions csh. This Shell has no place in unix Systems Administration. My advice, don't use it.

Last edited by methyl; 12-13-2011 at 06:22 PM.. Reason: schoolboy error in mv ! assorted other typos
This User Gave Thanks to methyl For This Post:
# 5  
Old 12-13-2011
Thanks a lot Methyl .
It worked!!!

You are right, I initially created the script in my windows machine using notepad.
Could you please suggest any unix style editor that I can use in windows machine to create shell script?

Thanks Again.
KT.

PS:
Quote:
Important: You are destined to become a great Systems Administrator. You can present a problem clearly and follow instructions. Good luck in your future career.
Thanks, no disrespect to Sys Admins but I'm a J2EE developer Smilie

Last edited by kuldeept; 12-13-2011 at 06:29 PM..
# 6  
Old 12-13-2011
Most people would use "vim" on the Linux box.

Btw. "Linux" is vague. The exact Operating System and version is more useful.
Code:
uname -a

(blotting anything confidential like machine names with X's).

If you do edit on a Microsoft platform be careful how you transfer the file to the Linux/unix server. If you use ftp make sure that you use "text" mode because this will fix most of the usual issues. Temporarily renaming your file with a ".TXT" extension often helps with ftp.


Why not learn "vim" or one of the umpteen popular editors for Linux?


Btw. Whenever you mention an Operating System (e.g. "Windows") always state the manufacturer and version. e.g. Microsoft Windows 7 service pack 4,300. There are other more important uses of the word "Windows" in the unix world which precede Microsoft and are still in use today.

Last edited by methyl; 12-13-2011 at 06:48 PM..
This User Gave Thanks to methyl For This Post:
# 7  
Old 12-13-2011
Thanks for your valuable advice.

Regards
KT.
This User Gave Thanks to kuldeept For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

2. Shell Programming and Scripting

Lex: concatenating word into array of c-strings

Alright, so I'm writing a file for the lexical analyzer (lex). It will be used to check C code (collecting the identifiers and storing those names along with the line numbers the identifier was found on). I'm not used to 'C' so I'm having some difficulty. I am using a function (insertId()) to... (4 Replies)
Discussion started by: D2K
4 Replies

3. Shell Programming and Scripting

Concatenating strings and run it in bash

Hi, all, I tried to write a simple shell script as follow: #!/bin/bash # What want to do in bash is following # : pcd_viewer cloud_cluster_0.pcd cloud_cluster_1.pcd cloud_cluster_2.pcd cloud_cluster_3.pcd cloud_cluster_4.pcd STR = "pcd_viewer" for i in `seq 0 4` do STR... (1 Reply)
Discussion started by: bedeK
1 Replies

4. Shell Programming and Scripting

How to preserve space while concatenating strings? (KSH)

I have these str1=$(echo "This is string one with spaces \n This is also my sentence 1") When I echo $str1, it displays the new line character properly. Now I have another new variable say str2. I want to concatenate in this way.. str1 + newline character + and then str2. That's I... (3 Replies)
Discussion started by: dahlia84
3 Replies

5. Shell Programming and Scripting

CSH: Concatenating Strings, how to add new line character and functions?

Hello, I'm trying to run a program on a directory (traverse sub dirs too) through my csh script. Arrays support in CSH is appalling, something like associative arrays would have helped me do this so much easier. Anyway, I want to hold some details extracted from the program and then at the... (0 Replies)
Discussion started by: ragabonds
0 Replies

6. Shell Programming and Scripting

concatenating strings

I m new to shell scripting and what i want is take as an i/p from command line the name of the file and inside my script i should redirect the o/p of my few commands to this file concatenated with .txt for example if i give ./linux filename i should get the o/p in filename.txt i need to... (2 Replies)
Discussion started by: tulip
2 Replies

7. Programming

Concatenating array of strings into one string separated by spaces

Hi, Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces. For example is an array is such that array... (12 Replies)
Discussion started by: newhere
12 Replies

8. Shell Programming and Scripting

mailx: concatenating strings for message body (KSH)

Hi all, Think this is a pretty simple problem, but I've been thinking about it for a few days. Let's say that I'm going to have to output the contents of a file as the body of a mailx message. I'll probably do this: cat <filename> | mailx <extra commands> However, how do I go about doing... (1 Reply)
Discussion started by: rockysfr
1 Replies

9. Shell Programming and Scripting

concatenating strings..

hey guys.. probably a simple question but i cant seem to find any info on it. i have a small array of strings, and i want to concatenate the contents of the array into one big string. any ideas on how i can do this? cheers. (2 Replies)
Discussion started by: jt_csv
2 Replies

10. Shell Programming and Scripting

Concatenating Strings

Is there any function to concatenate strings in shell script (2 Replies)
Discussion started by: radhika03
2 Replies
Login or Register to Ask a Question