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?

Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90
  • 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. – Charles Duffy Jan 03 '18 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?](https://stackoverflow.com/questions/4421257/why-does-local-sweep-the-return-code-of-a-command) – Charles Duffy Jan 03 '18 at 17:06
  • 1
    Consider running your code through http://shellcheck.net/ and also fixing the various quoting bugs it identifies. – Charles Duffy Jan 03 '18 at 17:07
  • Very interesting. Thank you! – Kevin Amiranoff Jan 03 '18 at 17:08

1 Answers1

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)

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    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. – torek Jan 03 '18 at 17:45