1

I am trying to change the name of my file, adding the number of commits and the current branch name on my file before sending it to the repository.

But I get the following error:

./publish_to_artifactory.sh: line 4: local: `rev-parse': not a valid identifier'`

upload() {
  local currentBranch=git rev-parse --abbrev-ref HEAD;
  local gitNumberOfCommits=git rev-list --count HEAD;

  for file in ./../*.ipa; do
    mv $file mobile-${currentBranch}-${gitNumberOfCommits};
    echo "uploading zeos-mobile-$currentBranch-$gitNumberOfCommits";
    curl -X PUT -u $PUBLISH_USER:$PUBLISH_PASS -T mobile-$currentBranch-$gitNumberOfCommits; http://example.com/artifactory/ios-dev-local/ --fail
  done
}

upload;

What am I doing wrong?

4
  • 1
    BTW, you shouldn't put command substitutions on the same line as local at all -- doing so shadows the exit status of those substitutions with the exit status of the local command. Commented Jan 3, 2018 at 17:05
  • 1
    Instead, run local currentBranch gitNumberOfCommits as one line, and then put currentBranch=$(...) and gitNumberOfCommits=$(...) each on a separate line. See Why does local sweep the return code of a command? Commented Jan 3, 2018 at 17:06
  • 1
    Consider running your code through shellcheck.net and also fixing the various quoting bugs it identifies. Commented Jan 3, 2018 at 17:07
  • Very interesting. Thank you! Commented Jan 3, 2018 at 17:08

1 Answer 1

4

To assign the output of a command to a variable, you need to use command-substitution syntax in bash.

local currentBranch="$(git rev-parse --abbrev-ref HEAD)"
local gitNumberOfCommits="$(git rev-list --count HEAD)"

What you have done is stored the literal string in those local variables defined. When shell was tokenizing them to evaluate it has understood it as a variable assignment gone horribly wrong! What it understood is a local variable in the name of currentBranch set to a value git and tries to run rev-list as a command which it cannot obviously find in its list of standard paths (under $PATH)

Sign up to request clarification or add additional context in comments.

1 Comment

Also, local a=b c means, in effect, local a=b; local c. Note that the result of expansion ($(git rev-parse ...)) won't re-expand here, so you could write local currentBranch=$(git rev-parse --abbrev-rev HEAD) even if git rev-parse might produce more than one word; but the double quotes are useful for our own sanity. :-) Finally, as Charles Duffy noted, if you want to check the exit status of a substitution, you have to separate it out from the local declaration.

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.