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':

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

I run this script as :

/users/apps/vbolt/tmsmari/toneUserLoadUtility/runToneUserLoad.sh

I'm getting the following result for

echo "Script Dir:$script_dir"

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

but, for some reason

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

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:

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.

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:

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

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 (

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

) and I got this output:

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

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

gave this output:

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

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 .

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

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

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.

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:

Thanks, no disrespect to Sys Admins but I'm a J2EE developer :slight_smile:

Most people would use "vim" on the Linux box.

Btw. "Linux" is vague. The exact Operating System and version is more useful.

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.

Thanks for your valuable advice.

Regards
KT.