简体   繁体   English

Bash:如何使用变量运行命令期望引号?

[英]Bash: How to run command expecting quotes using a variable?

I have a git command that I would like to run in my script: 我有一个要在脚本中运行的git命令:

git filter-branch -f --env-filter "STUFF HERE IN QUOTES"

The thing is, in my script I am dynamically creating the string that is represented by STUFF HERE IN QUOTES : 问题是,在我的脚本中,我正在动态创建由STUFF HERE IN QUOTES表示的字符串:

ENVFILTER=""
while read commit; do
    IFS="|" read date hash message <<< "$commit"
    DATE_NO_SPACE="$(echo "${date}" | tr -d '[[:space:]]')"
    COMMIT_ENV=$(cat <<-END
         if [ \$GIT_COMMIT = $hash ]
         then
         export GIT_AUTHOR_DATE="$DATE_NO_SPACE"
         export GIT_COMMITTER_DATE="$DATE_NO_SPACE"
         fi
    END
    )
    ENVFILTER="$ENVFILTER$COMMIT_ENV"
done < $tmpfile
git filter-branch -f --env-filter \'"$ENVFILTER"\' // <---------- does not work

Basically I start with an empty string ENVFILTER and I concat it with other strings to form a bunch of if statements. 基本上,我从一个空字符串ENVFILTER开始,并与其他字符串合并以形成一堆if语句。 I'm having trouble with the last step, which is running the actual command. 我在执行实际命令的最后一步遇到了麻烦。 I don't know how to run it with the expected quotes + the interpolated value. 我不知道如何使用期望的报价+插值来运行它。

=== UPDATE === ===更新===

After looking at @chepner's answer and using "$ENVFILTER" , I can now successfully run the command. 在查看@chepner的答案并使用"$ENVFILTER" ,我现在可以成功运行该命令。 So is answer is correct. 所以答案是正确的。

However, there is an error about my if statements (not related to the quotes): 但是,关于我的if语句(与引号无关)存在错误:

Rewrite eeb8860c13179c931a513a9ada76dc109324d790 (1/17)/opt/boxen/homebrew/Cellar/git/2.6.2/libexec/git-core/git-filter-branch: eval: line 313: syntax error near unexpected token `if'
/opt/boxen/homebrew/Cellar/git/2.6.2/libexec/git-core/git-filter-branch: eval: line 313: `     fi     if [ $GIT_COMMIT =  08a2d85f9412c67435929d80ccc5b914f3ad8547  ]'
env filter failed:      if [ $GIT_COMMIT =  59d13c174c34b6540f61585ef507abcef29ab22e  ]
     then
 export GIT_AUTHOR_DATE="2016-10-02T21:16:37-04:00"
 export GIT_COMMITTER_DATE="2016-10-02T21:16:37-04:00"
     fi     if [ $GIT_COMMIT =  08a2d85f9412c67435929d80ccc5b914f3ad8547  ]
     then
 export GIT_AUTHOR_DATE="2016-08-31T17:54:28-04:00"
 export GIT_COMMITTER_DATE="2016-08-31T17:54:28-04:00"
     fi     if [ $GIT_COMMIT =  d7801396f5fb1092beec3aa484361cd50ab35e9e  ]
     then
 export GIT_AUTHOR_DATE="2016-08-31T17:53:07-04:00"
 export GIT_COMMITTER_DATE="2016-08-31T17:53:07-04:00"
     fi     if [ $GIT_COMMIT =  7930e9be149a57229c9fc26ccffe1c6453317d70  ]
     then
 export GIT_AUTHOR_DATE="2016-08-31T17:48:03-04:00"
 export GIT_COMMITTER_DATE="2016-08-31T17:48:03-04:00"
     fi     if [ $GIT_COMMIT =  f0849b2de99ac37790eaf6e40c8980988e865c7c  ]
     then
 export GIT_AUTHOR_DATE="2016-08-31T17:46:33-04:00"
 export GIT_COMMITTER_DATE="2016-08-31T17:46:33-04:00"
     fi
[

Is there something wrong with it? 这有什么问题吗? I don't get it 我不明白

Assuming that ENVFILTER contains the correct code, this is all you need: 假设ENVFILTER包含正确的代码,这就是您所需要的:

git filter-branch -f --env-filter "$ENVFILTER"

Any special characters in the value of ENVFILTER are passed as-is to git as long as the expansion is quoted. 只要引用了扩展名, ENVFILTER 值中的任何特殊字符都ENVFILTER传递给git


That said, I would clean up the generator a little: 也就是说,我将清理一下发电机:

envfilter=
while IFS="|" read -r date hash message; do
    envfilter+="if [ \$GIT_COMMIT = \"$hash\" ]; then echo \"here\"; fi;"
done < "$tmpfile"
git filter-branch -f --env-filter "$envfilter"

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

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