gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:43:54 25 4
gpt4 key购买 nike

我有一个 git 命令,我想在我的脚本中运行:

git filter-branch -f --env-filter "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

基本上,我从一个空字符串 ENVFILTER 开始,然后将它与其他字符串连接起来形成一堆 if 语句。我在最后一步遇到问题,即运行实际命令。我不知道如何使用预期引号 + 内插值来运行它。

===更新===

查看@chepner 的回答并使用"$ENVFILTER" 后,我现在可以成功运行该命令。所以答案是正确的。

但是,我的 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
[

有什么问题吗?没看懂

最佳答案

假设 ENVFILTER 包含正确的代码,这就是您所需要的:

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

ENVFILTERvalue 中的任何特殊字符都会按原样传递给 git,只要扩展被引用。


也就是说,我会稍微清理一下生成器:

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"

关于git - Bash:如何使用变量运行期望引号的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com