Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: bash concatenate  (Read 6259 times)

0 Members and 1 Guest are viewing this topic.

Bennieboj

    Topic Starter


    Rookie

  • time flies, ur the pilot
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 7
bash concatenate
« on: January 10, 2012, 10:12:32 AM »
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..
« Last Edit: January 10, 2012, 10:52:36 AM by Bennieboj »

lina19



    Rookie

    • Experience: Beginner
    • OS: Unknown
    Re: bash concatenate
    « Reply #1 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

    Bennieboj

      Topic Starter


      Rookie

    • time flies, ur the pilot
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 7
    Re: bash concatenate
    « Reply #2 on: January 10, 2012, 02:32:16 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)

    learning_cmd



      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: bash concatenate
      « Reply #3 on: January 10, 2012, 05:35:33 PM »
      #!/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 :-)
      « Last Edit: January 10, 2012, 05:49:39 PM by learning_cmd »
      “you don’t need a beret to be creative.”

      Creativity is a dirty job.

      It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

      Merlin Mann

      Bennieboj

        Topic Starter


        Rookie

      • time flies, ur the pilot
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 7
      Re: bash concatenate
      « Reply #4 on: January 11, 2012, 12:45:43 AM »
      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....

      learning_cmd



        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: bash concatenate
        « Reply #5 on: January 11, 2012, 05:44:57 AM »
        I think I found it  ;D

        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  8)


        Good luck
        « Last Edit: January 11, 2012, 05:57:20 AM by learning_cmd »
        “you don’t need a beret to be creative.”

        Creativity is a dirty job.

        It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

        Merlin Mann

        lina19



          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: bash concatenate
          « Reply #6 on: January 11, 2012, 08:47:26 AM »
          thats 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 work

          Bennieboj

            Topic Starter


            Rookie

          • time flies, ur the pilot
            • Yes
          • Computer: Specs
          • Experience: Familiar
          • OS: Windows 7
          Re: bash concatenate
          « Reply #7 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 ^^

          learning_cmd



            Rookie

            • Experience: Beginner
            • OS: Unknown
            Re: bash concatenate
            « Reply #8 on: January 11, 2012, 10:01:07 AM »
            What you said.

            All CR's gone on the fly  :)
            “you don’t need a beret to be creative.”

            Creativity is a dirty job.

            It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

            Merlin Mann

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: bash concatenate
            « Reply #9 on: January 11, 2012, 11:05:20 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?

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: bash concatenate
            « Reply #10 on: January 11, 2012, 04:53:53 PM »
            or use tr:

            Code: [Select]
            tr -d '\015' < inputfile.txt > result.txt
            I was trying to dereference Null Pointers before it was cool.

            Bennieboj

              Topic Starter


              Rookie

            • time flies, ur the pilot
              • Yes
            • Computer: Specs
            • Experience: Familiar
            • OS: Windows 7
            Re: bash concatenate
            « Reply #11 on: January 15, 2012, 09:18:37 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 =)