问题
I am trying to run below git command using java program on windows.
git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java'
| grep e4ecfbe | awk '{print $7}'
it gives me error :
fatal: cannot stat path '$7}'': No such file or directory
While running this command from command prompt, it gives me results as desired.
Please help!
回答1:
On a CMD in Windows, I can make it work using double quotes for the awk parameters:
git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java'
| grep e4ecfbe | awk "{print $7}"
Note that my awk.exe comes from Gnu On Windows.
The OP mentions using that command in Java with:
String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'";
Process p = Runtime.getRuntime().exec(commandToBeExecuted);
But you never pass all parameters as a single string.
Use an array, as in "Using Quotes within getRuntime().exec" and in "How to make pipes work with Runtime.exec()?"
Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", commandToBeExecuted);
Escape the double quotes in commandToBeExecuted:
commandToBeExecuted = "git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java'
| grep e4ecfbe | awk \"{print $7}\""
来源:https://stackoverflow.com/questions/31199139/issue-with-git-blame-command-using-java-program