我正在尝试在 Windows 上使用 java 程序在 git 命令下运行。
git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java'
| grep e4ecfbe | awk '{print $7}'
它给我错误:
fatal: cannot stat path '$7}'': No such file or directory
从命令提示符运行此命令时,它会根据需要提供结果。
请帮忙!
最佳答案
在 Windows 的 CMD 上,我可以为 awk
参数使用双引号使其工作:
git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java'
| grep e4ecfbe | awk "{print $7}"
请注意,我的 awk.exe
来自 Gnu On Windows .
OP 提到在 Java 中使用该命令:
String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'";
Process p = Runtime.getRuntime().exec(commandToBeExecuted);
但是您永远不会将所有参数作为单个字符串传递。
使用数组,如“Using Quotes within getRuntime().exec”和“How to make pipes work with Runtime.exec()?”
Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", commandToBeExecuted);
转义commandToBeExecuted
中的双引号:
commandToBeExecuted = "git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java'
| grep e4ecfbe | awk \"{print $7}\""
关于java - 使用java程序的git blame命令问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199139/