如何通过 Autohotkey 读取 Git Bash 中命令的输出
How to read output of a command in Git Bash through Autohotkey
我正在尝试通过 Autohotkey 向 Git Bash 终端发送命令,但无法找到读取其输出的方法。有一个简单的方法吗?我正在发送这个
运行, C:\Users\Unknown\AppData\Local\Programs\Git\git-bash.exe
睡觉,2000
发送 cd /c/Users/Unknown/Desktop/git/fw{输入}
睡觉,1000
发送git日志--pretty=format:'`%h' -n 1{Enter}
答案是终端上显示的提交编号
如何阅读?
谢谢
捕获输出
要捕获命令的输出,您可以使用以下 RunWaitOne()
函数之一。
选项 1: 不使用临时文件且无法隐藏命令 window。 (Source)
; From https://www.autohotkey.com/docs/commands/Run.htm#Examples
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C """ command """") ; change: added '"' around command
return exec.StdOut.ReadAll()
}
选项 2: 使用临时文件并隐藏命令 window。 (在 WTFPL 下发布)
RunWaitOne(command) {
tmpFile := A_Temp . "\" . A_ScriptName . "_RunWaitOne.tmp"
RunWait, % ComSpec . " /c """ . command . " > """ . tmpFile . """""",, Hide
FileRead, result, %tmpFile%
FileDelete, %tmpFile%
return result
}
工作示例
我个人不喜欢在路径中硬编码,所以设置下面的答案来做以下步骤:
- 在系统 PATH 环境变量上寻找 Git 安装并将其存储为
GIT_BIN_DIR
- 如果尚未找到安装,请在默认安装目录中查找一个并将其存储为
GIT_BIN_DIR
- 如果找不到 Git 并退出,则显示错误消息。
- 排队要调用的命令。
- 执行命令并将结果存储在变量
result
。
- 显示结果
代码
代码在WTFPL
下发布
; STEP 1: Try to find Git on PATH
EnvGet, E_PATH, PATH
GIT_BIN_DIR := ""
for i, path in StrSplit(E_PATH, ";")
{
if (RegExMatch(path, "i)Git\cmd$")) {
SplitPath, path, , parent
GIT_BIN_DIR := parent . "\bin"
break
}
}
; STEP 2: Fallback to default install directories.
if (GIT_BIN_DIR == "") {
allUsersPath := A_ProgramFiles . "\Git\bin"
currentUserPath := A_AppData . "\Programs\Git\bin"
if (InStr(FileExist(currentUserPath), "D"))
GIT_BIN_DIR := currentUserPath
else if (InStr(FileExist(allUsersPath), "D"))
GIT_BIN_DIR := allUsersPath
}
; STEP 3: Show error Git couldn't be found.
if (GIT_BIN_DIR == "") {
MsgBox 0x1010,, Could not find Git's 'bin' directory
ExitApp
}
; STEP 4 - Queue any commands.
; commands becomes "line1 & line2 & ..." thanks to the Join continuation section
commands := "
(Join`s&`s
cd /c/Users/Unknown/Desktop/git/fw
git log --pretty=format:'`%h' -n 1
)"
; STEP 5 - Execute the commands (Uses "Git\bin\sh.exe" so we can capture output)
result := RunWaitOne("""" . GIT_BIN_DIR . "\sh.exe"" --login -i -c """ . commands . """")
; STEP 6 - Show the result
MsgBox 0x1040,, % result
我正在尝试通过 Autohotkey 向 Git Bash 终端发送命令,但无法找到读取其输出的方法。有一个简单的方法吗?我正在发送这个
运行, C:\Users\Unknown\AppData\Local\Programs\Git\git-bash.exe
睡觉,2000
发送 cd /c/Users/Unknown/Desktop/git/fw{输入}
睡觉,1000
发送git日志--pretty=format:'`%h' -n 1{Enter}
答案是终端上显示的提交编号
如何阅读?
谢谢
捕获输出
要捕获命令的输出,您可以使用以下 RunWaitOne()
函数之一。
选项 1: 不使用临时文件且无法隐藏命令 window。 (Source)
; From https://www.autohotkey.com/docs/commands/Run.htm#Examples
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C """ command """") ; change: added '"' around command
return exec.StdOut.ReadAll()
}
选项 2: 使用临时文件并隐藏命令 window。 (在 WTFPL 下发布)
RunWaitOne(command) {
tmpFile := A_Temp . "\" . A_ScriptName . "_RunWaitOne.tmp"
RunWait, % ComSpec . " /c """ . command . " > """ . tmpFile . """""",, Hide
FileRead, result, %tmpFile%
FileDelete, %tmpFile%
return result
}
工作示例
我个人不喜欢在路径中硬编码,所以设置下面的答案来做以下步骤:
- 在系统 PATH 环境变量上寻找 Git 安装并将其存储为
GIT_BIN_DIR
- 如果尚未找到安装,请在默认安装目录中查找一个并将其存储为
GIT_BIN_DIR
- 如果找不到 Git 并退出,则显示错误消息。
- 排队要调用的命令。
- 执行命令并将结果存储在变量
result
。 - 显示结果
代码
代码在WTFPL
下发布; STEP 1: Try to find Git on PATH
EnvGet, E_PATH, PATH
GIT_BIN_DIR := ""
for i, path in StrSplit(E_PATH, ";")
{
if (RegExMatch(path, "i)Git\cmd$")) {
SplitPath, path, , parent
GIT_BIN_DIR := parent . "\bin"
break
}
}
; STEP 2: Fallback to default install directories.
if (GIT_BIN_DIR == "") {
allUsersPath := A_ProgramFiles . "\Git\bin"
currentUserPath := A_AppData . "\Programs\Git\bin"
if (InStr(FileExist(currentUserPath), "D"))
GIT_BIN_DIR := currentUserPath
else if (InStr(FileExist(allUsersPath), "D"))
GIT_BIN_DIR := allUsersPath
}
; STEP 3: Show error Git couldn't be found.
if (GIT_BIN_DIR == "") {
MsgBox 0x1010,, Could not find Git's 'bin' directory
ExitApp
}
; STEP 4 - Queue any commands.
; commands becomes "line1 & line2 & ..." thanks to the Join continuation section
commands := "
(Join`s&`s
cd /c/Users/Unknown/Desktop/git/fw
git log --pretty=format:'`%h' -n 1
)"
; STEP 5 - Execute the commands (Uses "Git\bin\sh.exe" so we can capture output)
result := RunWaitOne("""" . GIT_BIN_DIR . "\sh.exe"" --login -i -c """ . commands . """")
; STEP 6 - Show the result
MsgBox 0x1040,, % result