LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do you export svn revision number to a variable within a script? (https://www.linuxquestions.org/questions/programming-9/how-do-you-export-svn-revision-number-to-a-variable-within-a-script-4175467039/)

Holering 06-22-2013 07:58 PM

How do you export svn revision number to a variable within a script?
 
Can't grep or awk revision info when running an svn checkout script. Tried this:
Code:

svn checkout name.of.place/trunk
NAME=(svn checkout name.of.place/trunk | grep '^r[0-9]\+')
cp -r trunk nameofplace-$NAME ;\
exit 1

Only get "nameofplace-" so $NAME variable doesn't seem to work...

Help would be great!

Regards

unSpawn 06-23-2013 03:25 AM

Quote:

Originally Posted by Holering (Post 4976828)
Only get "nameofplace-" so $NAME variable doesn't seem to work...

Next time try debugging your shell script by setting "set -vx" at the appropriate place (add to command line, begin of script, this specific code location). The reason "nameofplace-$NAME" doesn't work is because you don't execute the command with back ticks or preferably with "$()". You should also always watch for proper quoting: "nameofplace-$NAME" or "nameofplace-${NAME}". Third you're using the "checkout" command twice for no other reason than retrieving the revision number. That's highly inefficient. Use another command line "info" instead.


Code:

VERSION=$(svn info name.of.place/trunk 2>&1|awk '/^Revision:/ {print $2}')
[ ${#VERSION} -ne 0 -a "X${VERSION//[0-9]/}" != "X" ] && echo cp -r trunk "nameofplace-${VERSION}"


Holering 06-26-2013 10:35 PM

Thanks!

Ended up using:
Code:

VERSION=$(svn info http://place.net/trunk/ 2>&1|awk '/^Revision:/ {print $2}')
cp -r trunk "${PRGNAM}-r${VERSION}"

My only gripe is not being able to do this with a single variable.

If I use:
Code:

VERSION=$(svn checkout http://place.net/trunk | grep -o '[0-9]*')
cp -r trunk "${PRGNAM}-r${VERSION}"

it hangs and terminal has no output; when I ctrl-break and use svn manually it says it's locked and I have to use svn cleanup... Even if I do echo $VERSION prior to cp.

Would be nice to do this in one instance of svn rather than executing svn twice (svn checkout ; svn --info).

unSpawn 06-27-2013 01:42 AM

What you should understand firstly is that the 'checkout' command is the wrong tool for the job. Compared to 'info' it's an inefficient transaction because it provides more information than needed (it actually places code in the system while it's at all not necessary) and if you have a large code base it might take more time than strictly necessary. Secondly, and I don't know if this is a botched example or not, but your 'cp' command shows no destination. I don't know how a 'cp' should complete that way?.. Finally I pointed you to debugging your shell script by setting "set -vx". Using it (also in code you post?) helps you see and understand what goes on "under the hood". It comes in handy especially if you're not certain about the commands you use. Do try using it.

Holering 06-28-2013 12:47 AM

The cp command is run in the same working directory. Tried using set -vx but it shows nothing when running from script (didn't try outside the script). Thanks for your help.

Regards

David the H. 06-28-2013 11:43 AM

It also helps to post an example of the text that you're working with, BTW. Both input and desired output. I was wondering if something cleaner couldn't be written, but I don't have any way to produce the type of svn output you're working on, so I'm unable to test any possible solutions for you.


All times are GMT -5. The time now is 05:47 AM.