1

I am trying to parse the output on svn info without resorting to an external shell command like sed or awk. This is purely academic as I know I could do this in a heartbeat with those tools.

Output I am parsing is:

Path: .
URL: svn://[email protected]/handbrake_batch/trunk/handbrake
Repository Root: svn://[email protected]/handbrake_batch
Repository UUID: 99c2cca7-102b-e211-ab20-02060a000c0b
Revision: 6
Node Kind: directory
Schedule: normal
Last Changed Author: ilium007
Last Changed Rev: 6
Last Changed Date: 2012-11-10 19:00:35 +1000 (Sat, 10 Nov 2012)

Here is my code:

#!/bin/bash

#set -x

OLD_IFS="$IFS"
IFS=$'\r\n'

# Get the output from svn info into an array
SVN_INFO_ARR=(`svn info`)
COUNT=0
for i in ${SVN_INFO_ARR[@]}; do
    echo $COUNT
    echo "$i"
    (( COUNT++ ))
done

# Get the element that says "Revision: 6"
REV_ARR=${SVN_INFO_ARR[4]}

# Testing the loop over what should be a two element array
COUNT=0
for i in ${REV_ARR[@]}; do
    echo $COUNT
    echo "$i"
    (( COUNT++ ))
done

#This should give the number 6 (or string or something)
REV_NUMBER=${REV_ARR[1]}

echo ${REV_NUMBER}

### INCREMENT REVISION NUMBER FROM ARRAY ELEMENT ###

#NEW_REV_NUMBER= ????? + 1

IFS="$OLD_IFS"

I would like to be able to take the string:

Revision: 6

and pull out the 6 and increment by 1 so I can update a release txt file to be included in the SVN commit.

I have tried to make that 6 turn into a 7 for an hour now and feel like an idiot because I can't do it.

4 Answers 4

3

You need parenthesis: Change this:

# Get the element that says "Revision: 6"
REV_ARR=${SVN_INFO_ARR[4]}

to this:

# Get the element that says "Revision: 6"
REV_ARR=(${SVN_INFO_ARR[4]})

before

#This should give the number 6 (or string or something)
REV_NUMBER=${REV_ARR[1]}

so you'll be able to:

((REV_NUMBER++))

Edit:

As you wrote:

SVN_INFO_ARR=(`svn info`)

instead of just:

SVN_INFO_ARR=`svn info`

The parenthesis is used in bash to define an array. Have a look at:

man -Len -P'less +"/^ *Arrays"' bash
Sign up to request clarification or add additional context in comments.

3 Comments

It was the parentheses that I was looking for - thanks @f-hauri Can you please explain the parentheses ?
Actually it doesn't work !! Can you please explain the parentheses ? New code: #!/bin/bash set -x OLD_IFS="$IFS" IFS=$'\r\n' # Get the output from svn info into an array SVN_INFO_ARR=(`svn info`) IFS="$OLD_IFS" # Get the element that says "Revision: 6" REV_ARR=(${SVN_INFO_ARR[4]}) #This should give the number 6 (or string or something) REV_NUMBER=${REV_ARR[1]} echo $REV_NUMBER echo ((REV_NUMBER++)) Output error: + REV_ARR=(${SVN_INFO_ARR[4]}) + REV_NUMBER=6 + echo 6 6 ./test.sh: line 19: syntax error near unexpected token `(' ./test.sh: line 19: `echo ((REV_NUMBER++))'
Accepted answer after being advised further down the ++ syntax.
3

Instead of hardcoding the array indices, a better way would be to filter out the line you need and extract the number

Here's one way using regex (Bash 4)

while read -r line; do
    if [[ $line =~ Revision:\ ([0-9]+) ]]; then
         new_rev_num=$((BASH_REMATCH[1]+1))
         echo $new_rev_num
         break
    fi
done < $(svn info)

Comments

0

Use grep to only select the line you need. Then, use Parameter expansion to remove "Revision: ". Finally, use let to do the arithmetics:

REVISION=$(svn info | grep '^Revision:')
REVISION=${REVISION#* }
let REVISION++

1 Comment

Thanks @choroba but I wanted to tackle this without calling an external tool such as grep but your syntax is useful and I will keep in mind for other projects.
0

This code worked in the end:

#!/bin/bash

set -x

OLD_IFS="$IFS"
IFS=$'\r\n'

# Get the output from svn info into an array
SVN_INFO_ARR=(`svn info`)

IFS="$OLD_IFS"

# Get the element that says "Revision: 6"
REV_ARR=(${SVN_INFO_ARR[4]})

#This should give the number 6 (or string or something)
REV_NUMBER=${REV_ARR[1]}
echo $REV_NUMBER
echo $(( REV_NUMBER + 1 ))

The answer above had me stumped for a while because it was missing the $ in front of:

echo $(( REV_NUMBER + 1 ))

and the ((REV_NUMBER++)) notation did not work, I still got 6, not 7:

+ OLD_IFS='     
'
+ IFS='
'
+ SVN_INFO_ARR=(`svn info`)
++ svn info
+ IFS='     
'
+ REV_ARR=(${SVN_INFO_ARR[4]})
+ REV_NUMBER=6
+ echo 6
6
+ echo 6
6

2 Comments

((REV_NUMBER++)) only increments REV_NUMBER after you echo it, ((++REV_NUMBER)) would give you the right number.
Ok - that now makes 100% sense. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.