Find the answer to your Linux question:
Results 1 to 8 of 8
ok, still learning but I've been trying to get this right for hours. I'm trying to get multiple numeric valued version information into a variable, all on the same line. ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    5

    bash; echo multiple numeric values on one line

    ok, still learning but I've been trying to get this right for hours. I'm trying to get multiple numeric valued version information into a variable, all on the same line.

    I want for example $VERSION=3.1.0.01.002. I'm trying to pull seperate values from the file named version.properties, wherein the file contains;

    patch.rel.num=0
    sqa.num=01
    major.rel.num=3
    build.num=002
    minor.rel.num=1


    so I have the following script;

    ###########################
    #!/bin/bash

    BUILDVERFILE="version.properties"

    PATCH=`grep "patch.rel.num" ${BUILDVERFILE} | awk {'print $1'}`
    SQA=`grep "sqa.num" ${BUILDVERFILE} | awk {'print $1'}`
    MAJOR=`grep "major.rel.num" ${BUILDVERFILE} | awk {'print $1'}`
    BUILD=`grep "build.num" ${BUILDVERFILE} | awk {'print $1'}`
    MINOR=`grep "minor.rel.num" ${BUILDVERFILE} | awk {'print $1'}`

    P=`echo $PATCH | tr -d .=[:alpha:]`
    S=`echo $SQA | tr -d .=[:alpha:]`
    MA=`echo $MAJOR | tr -d .=[:alpha:]`
    B=`echo $BUILD | tr -d .=[:alpha:]`
    MI=`echo $MINOR | tr -d .=[:alpha:]`

    VERSION=$MA.$MI.$P.$S.$B
    echo $VERSION
    ###########################

    I end up with the value of .002 ?? Seems like it's only getting the last portion because if I run the script thru "bash -x" I get;

    ...
    + VERSION=$'3\r.1\r.0\r.01\r.002\r'
    + echo $'3\r.1\r.0\r.01\r.002\r'
    .002


    ...it's killin me! where have I gone wrong?

    Thanks!

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'BEGIN{ FS="[.=]"}
    { data[$1]=$NF}
    END{ print data["major"]"."data["minor"]"."data["patch"]"."data["sqa"]}' file

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    5
    Quote Originally Posted by ghostdog74 View Post
    Code:
    awk 'BEGIN{ FS="[.=]"}
    { data[$1]=$NF}
    END{ print data["major"]"."data["minor"]"."data["patch"]"."data["sqa"]}' file
    thanks ghostdog74,

    Problem is, you are assuming I'm as brilliant in interpreting what you are telling me to do, as you are in writing it!

    can you dumb it down for me a bit, what do I do with this?

  4. #4
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    FS="[.=]" means "the field separator is the equals sign"
    data[$1]=$NF means "put each row on an associative array using the field as the key and the last column as the value"... $1 is the first word on each row and $NF means the value of last column.
    EOF

  5. #5
    Just Joined!
    Join Date
    Aug 2009
    Posts
    5
    so this short script in and of itself can retrieve the data but how do I then retrieve it by calling a variable? How do I get, for example;

    This is build; $VERSION

  6. #6
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    You can save the script, let say 'foo.sh' and use the output to set the variable:

    VERSION=`foo.sh`
    echo $VERSION
    EOF

  7. #7
    Just Joined!
    Join Date
    Aug 2009
    Posts
    5
    Then, I have two files, the first file "HECMS_version.sh";

    [root]# more HECMS_version.sh
    #!/bin/bash
    awk 'BEGIN{ FS="[.=]"}
    { data[$1]=$NF}
    END{ print data["major"]"."data["minor"]"."data["patch"]"."data["sqa"]"."data["b
    uild"]}' ./version.properties

    the second file named "getbldversion4";

    [root]# more getbldversion4
    VERSION=`./HECMS_version.sh`
    echo $VERSION


    but I get the same result I started with, when trying to echo this thing ...the just the last portion of the version number. (build.num=002)

    [root]# ./getbldversion4
    .002

  8. #8
    Just Joined!
    Join Date
    Aug 2009
    Posts
    5
    ok, I've solved it ...was all my problem,the file I was retrieving the values from, had Windows style carriage returns in it! Sorry, I never noticed that...

    Thanks both for all your assistance!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...