简体   繁体   English

bash将git命令结果添加到局部变量

[英]bash add git command result to local variable

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'` ./publish_to_artifactory.sh:第4行:本地:`rev-parse':不是有效的标识符'`

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? 我究竟做错了什么?

To assign the output of a command to a variable, you need to use command-substitution syntax in bash . 要将命令的输出分配给变量,需要在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! 当shell用令牌标记他们进行评估时,它已经将其理解为一个变量分配,这是完全错误的! 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 ) 它理解的是在currentBranch名称中设置为值gitlocal变量,并尝试将rev-list作为命令运行,但显然无法在其标准路径列表中(在$PATH )找到该命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM