1

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?

4

2 回答 2

2

语法的效果之一${parameter:=word}是 的值parameter被替换。这意味着您的 shell 将尝试执行您作为命令获得的任何数字。只需echo在行中完成分配,然后将其放在export后面。

echo ${SVN_REVISION:="$COMPUTE_REVISION"}
export SVN_REVISION
于 2013-09-23T23:20:36.940 回答
1

为什么不以明显的方式进行呢?

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

或者,如果你坚持

echo ${SVN_REVISION:=$(svn info ${1:-`pwd`} |
                       grep '^Revision' |
                       cut -d ':' -f2 |
                       tr -d ' ')}
export SVN_REVISION
于 2013-09-23T23:29:48.800 回答