1.

Solve : bash concatenate?

Answer»

Hi people,

I have a little problem with a bash script of mine.
Code: [Select]DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PATH="/System/java/"
echo $DIR                           #gives the working directory /media/*name of USB stick*
echo $PATH                          #gives /System/java/
echo $DIR$PATH                      #gives /System/java/, should give /media/*name of usb stick*/System/java/
FULL=$DIR$PATH                      #should concatenate
echo $FULL                          #gives /System/java/, should give /media/*name of usb stick*/System/java/
FULL=${DIR}/System/java/            #should concatenate
echo $FULL                          #gives /System/java/, should give /media/*name of usb stick*/System/java/

The script is stored on a USB stick
My GOAL is to get the current directory (where the script lives, not the working directory), which is stored in DIR. (This works)
Then I want to append the string "/System/java/" to it. Then I can use that string to do my stuff (not important for this question..)
I just can't figure out why it gives me this output...
Bash version 4.1.10, if anyone cares, since this stuff should work since version 3.1....

Can somebody help me with this?

EDIT: If I do this:
Code: [Select]echo ____________________$DIR$PATHI get this: /System/java/___________/media/*name of usb stick*
It's like it first prints the underscores, then the DIR and then puts PATH over all that..set "str1=Hello"

set "str3=%~dp0% %str1%"

echo.%str3%
pause


i dont know if this cna help...im new to it...im trying to get the value of str1 ...i mean hello Quote from: lina19 on January 10, 2012, 02:27:35 PM

set "str1=Hello"

set "str3=%~dp0% %str1%"

echo.%str3%
pause


i dont know if this cna help...im new to it...im trying to get the value of str1 ...i mean hello

What you want to get is e.g.: "I:\Hello"

Code: [Select]set str1=Hello
echo %str1%
set str3= %~dp0%str1%

echo %str3%
pause

But this has nothing to do with my question, it is the same problem in another scripting language...
The only difference is that here it works and in bash scripting it does weird stuff (at my pc at least)#!/bin/bash
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PATH="/System/java/"
FULL=$DIR$PATH
echo $DIR                         
echo $PATH                         
echo $DIR$PATH       
echo $FULL                         
FULL=${DIR}/System/java/
echo $FULL
echo "________________"$DIR$PATH


I had to adjust the amount of underscores to see the $DIR$PATH.
Hope is helps.

I used cygwin btw :-)You just adjusted the number of underscores to the number of signs in DIR/PATh to see them printed next to each other.
The point is that I shouldn't neet underscores or whatever "filling signs" to print them next to each other.
I have a variable FOO and I want to concatenate it to the variable BAR and store it into a new one FULL.
Then when I should print FULL I should see the CONTENTS of FOO at the time of the concatenation and then  the contents of BAR at the time of the concatenation.

Google says that it should work. All the sites say this:
FULL=$FOO$BAR
or
FULL=$FOO"contents of BAR"
or
FULL=${FOO}${BAR}

EDIT: I found something strange. When I output it to a text file (append > file.txt to the command) it writes the correct output to the file....I think I found it 

You need to do a dos2unix conversion first.

When you MADE your script in (most likely) a windows text editor (like notepad), the text editor puts a cariage return "\r" and another character at the end of each line.

You can see these characters in notepad++ by selecting "show all characters" from the "view > non-printable characters" menu.

The Bourne again shell does not like these characters. Therfore you need to convert the dos/windows file into a unix file. Cause Unix files have these characters in a different form.

What I did:

1. Open cygwin terminal.
2. Type: dos2unix yourfile.sh
3. Hit enter
4. Type: sh yourfile.sh
5. Hit enter

No text-overlap in my window 


Good luckthats nice learning_cmd...your learning very fast....yea i was guessing it was some kind of conversion issue...but didnt know what to do....
nice workThanks that's nice, I didn't think of that immediately!
Btw you don't need that dos2unix file, I just used Notepad++ (Edit menu, then Formatting/Format, Unix)
Thanks for your time and help you 2 =) 
If you have a problem, PM me if you want ^^What you said.

All CR's gone on the fly  Quote from: Bennieboj on January 11, 2012, 09:31:24 AM
Thanks that's nice, I didn't think of that immediately!
Btw you don't need that dos2unix file, I just used Notepad++ (Edit menu, then Formatting/Format, Unix)
Thanks for your time and help you 2 =) 
If you have a problem, PM me if you want ^^
Isn't the point of creating a script so that there is no user intervention?or use tr:

Code: [Select]tr -d '\015' < inputfile.txt > result.txt
Quote from: Squashman on January 11, 2012, 11:05:20 AM
Isn't the point of creating a script so that there is no user intervention?

Yes it is, there ain't any user intervention.
Why you're quoting my post?
I don't see the reference to anything I said and the question.

BC: thanks, but I'm not much of a command ninja =)


Discussion

No Comment Found