Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过自动热键读取Git Bash中的命令输出_Git_Autohotkey - Fatal编程技术网

如何通过自动热键读取Git Bash中的命令输出

如何通过自动热键读取Git Bash中的命令输出,git,autohotkey,Git,Autohotkey,我试图通过自动热键向Git Bash终端发送命令,但找不到读取其输出的方法。有简单的方法吗?我要寄这个 运行,C:\Users\Unknown\AppData\Local\Programs\Git\Git bash.exe 睡眠,2000年 发送cd/c/Users/Unknown/Desktop/git/fw{Enter} 睡吧,1000 发送git日志--pretty=格式:`%h'-n1{Enter} 答案是终端上显示的提交编号 我怎么读呢 谢谢捕获输出 要捕获命令的输出,可以使用以下函数

我试图通过自动热键向Git Bash终端发送命令,但找不到读取其输出的方法。有简单的方法吗?我要寄这个

运行,C:\Users\Unknown\AppData\Local\Programs\Git\Git bash.exe
睡眠,2000年

发送cd/c/Users/Unknown/Desktop/git/fw{Enter}

睡吧,1000

发送git日志--pretty=格式:`%h'-n1{Enter}

答案是终端上显示的提交编号

我怎么读呢

谢谢

捕获输出 要捕获命令的输出,可以使用以下函数之一
RunWaitOne()

选项1:不使用临时文件,无法隐藏命令窗口。()

选项2:使用临时文件,命令窗口隐藏。(根据"基本法"公布)

工作示例 我个人不喜欢路径中的硬编码,因此设置以下答案以执行以下步骤:

  • 在系统路径环境变量上查找Git安装,并将其存储为
    Git\u BIN\u DIR
  • 如果尚未找到安装,请在默认安装目录中查找一个,并将其存储为
    GIT\u BIN\u DIR
  • 如果找不到Git,则显示错误消息并退出
  • 将要调用的命令排队
  • 执行命令并将结果存储在变量
    result
  • 显示结果
  • 代码 代码是根据

    尝试
    ; 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()
    }
    
    RunWaitOne(command) {
      tmpFile := A_Temp . "\" . A_ScriptName . "_RunWaitOne.tmp"
      RunWait, % ComSpec . " /c """ . command . " > """ . tmpFile . """""",, Hide
      FileRead, result, %tmpFile%
      FileDelete, %tmpFile%
      return result
    }
    
    ; 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