简体   繁体   中英

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 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 :

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. 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. So is answer is correct.

However, there is an error about my if statements (not related to the quotes):

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:

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.


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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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