Question

I want to get the current svn revision using bash and set it as the environment variable SVN_REVISION. This environment variable may or may not be already set. If it's already set then I echo that, if not set then I compute it and then echo it. I DONT'T want to override if SVN_REVISION is already set. I am using the following script which is failing owing to my lack of understanding of parameter expansion.

#!/bin/bash

# If no directory provided as an argument, uses the current working directory as the    source directory.
RAW_SRC_DIR=${1:-`pwd`}
COMPUTE_REVISION=$(svn info ${RAW_SRC_DIR} | grep '^Revision' | cut -d ':' -f2 | tr -d ' ')
echo "${COMPUTE_REVISION}" ##Gets the revision successfully
${SVN_REVISION:="$COMPUTE_REVISION"} #Fails with message: <SVN_REVISION> command not found
export SVN_REVISION
echo $SVN_REVISION

How do I fix it?

Was it helpful?

Solution

One of the effects of the ${parameter:=word} syntax is that the value of parameter is substituted. That means your shell is going to try to execute whatever number you get as a command. Just do the assignment in the echo line and put the export afterwards.

echo ${SVN_REVISION:="$COMPUTE_REVISION"}
export SVN_REVISION

OTHER TIPS

Why not just do it the obvious way?

[[ -z $SVN_REVISION ]] && \
  SVN_REVISION=$(svn info ${1:-`pwd`} | grep '^Revision' | cut -d ':' -f2 | tr -d ' ')
echo $SVN_REVISION
export SVN_REVISION

or, if you insist

echo ${SVN_REVISION:=$(svn info ${1:-`pwd`} |
                       grep '^Revision' |
                       cut -d ':' -f2 |
                       tr -d ' ')}
export SVN_REVISION
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top