gpt4 book ai didi

Git 钩子(Hook)预提交错误 : "command not found"

转载 作者:行者123 更新时间:2023-12-04 18:03:45 24 4
gpt4 key购买 nike

我有一个简单的 Git 钩子(Hook),可以调用其他一些 (VBScript) 脚本。当我从命令行调用脚本时,脚本运行正确。但是,当执行 Hook 时,会出现以下错误:

./RevisionDate.vbs: line 1: syntax error near unexpected token `('
./RevisionDate.vbs: line 1: `Set copyFSO = CreateObject ("Scripting.FileSystemObject")'
.git/hooks/pre-commit: line 8: Start-Sleep: command not found
test
./MovePDF.vbs: line 1: unexpected EOF while looking for matching `''
./MovePDF.vbs: line 6: syntax error: unexpected end of file

这是在 Hook 中调用的 VBScript。

Set copyFSO = CreateObject ("Scripting.FileSystemObject")
copyFSO.copyFile "C:\Users\Ian\Desktop\Test\*.pdf", "C:\Users\Ian\Desktop\Test2"
copyFSO.moveFile "C:\Users\Ian\Desktop\Test\*.pdf", "C:\Users\Ian\Desktop\Temp"

'------ Microsoft Excel -------

inputPrefix = cint(inputbox("Please enter two digit prefix for file you would like updated.", "File Update"))
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
sFolder = "C:\Users\Ian\Desktop\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files
fileName = oFile
Set objWorkbook = nothing
Set objSelection = nothing
Set objWorksheet = nothing

If UCase(oFSO.GetExtensionName(oFile.Name)) = "XLSX" Then
Prefix = left(oFile.Name, 2)
filePrefix = cint(Prefix)
Set objWorkbook = objExcel.Workbooks.Open(fileName)
Set objSelection = objExcel.Selection
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel.DisplayAlerts = False
myYear = Year(Now())
myMonth = Month(Now())
myDay = Day(Now())
myDate = myYear & "/" & myMonth & "/" & myDay
myDateFile = myYear & "-" & myMonth & "-" & myDay

If (filePrefix = inputPrefix) then
objWorksheet.PageSetup.RightFooter = "Revision Date: " & myDate & " C"
objWorkbook.Save
End If

fileName = Replace(oFile.Name, ".xlsx", "")
saveAndCloseXlsx objWorkbook
End if
Next

Function saveAndCloseXlsx(objWorkbook)
objWorkbook.ExportAsFixedFormat xiTypePDF, "C:\Users\Ian\Desktop\Test\" & fileName
objWorkbook.Close
end Function


'------ Microsoft Word -------

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
sFolder = "C:\Users\Ian\Desktop\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files

If UCase(oFSO.GetExtensionName(oFile.Name)) = "DOCX" Then
fileName = oFile
Set objDoc = objWord.Documents.Open(fileName)
Set objSelection = objWord.Selection

If objDoc.Bookmarks.Exists("RevisionDate") = True Then
Set objRange = objDoc.Bookmarks("RevisionDate").Range
myYear = Year(Now())
myMonth = Month(Now())
myDay = Day(Now())
myDate = myYear & "/" & myMonth & "/" & myDay
myDateFile = myYear & "-" & myMonth & "-" & myDay
Prefix = left(oFile.Name, 2)
filePrefix = cint(Prefix)

If (inputPrefix = filePrefix) then
objRange.text = "Revision Date: " & myDate & " C"
objDoc.Bookmarks.Add "RevisionDate", objRange
End If

wdFormatPDF = 17
SaveAndCloseDocx objDoc
End If
End if
Next

set oFSO = Nothing
objWord.Quit

Function SaveAndCloseDocx(objDoc)
fileName = Replace(oFile.Name, ".docx", "")
objDoc.SaveAs "C:\Users\Ian\Desktop\Test\" & fileName & ".pdf", wdFormatPDF
objDoc.Close
End Function

最后是钩子(Hook)本身:

#!/bin/sh
#
#
echo "Script Running"
cd 'C:\Users\Ian\desktop\QMS_Manual'
"./RevisionDate.vbs"
Start-Sleep -s 30
"./MovePDF.vbs"
cd 'C:\Users\Ian\desktop\Test2'
pdftk *.pdf cat output ECMWC.pdf
cd 'C:\Users\Ian\desktop\QMS_Manual'
DeleteAllButFinal.vbs

为什么会这样?我读过它可能与 PATH 有关环境变量。

最佳答案

您可能希望在对您的 .vbs 脚本的调用前加上:

cscript yourscript.vbs

您正在从 Git Hook 运行 Bash/sh 脚本。

您的 Bash/sh 实现不知道如何处理 Start-Sleep,因为这是一个 PowerShell 命令。您需要找到一些 Bash 等价物或 shell out 到 PowerShell 才能执行该语句。

脚本然后尝试将 RevisionDate.vbsMovePDF.vbs 的内容作为 Bash/sh 语句执行,而不是启动通常会在下面关联的 VBScript 解释器cmd.exe 命令外壳。因此,您需要告诉您的 shell 脚本如何执行此操作,即 cscript yourscript.vbs

路径:

还要注意那些路径。大多数 Windows Bash/sh shell 实现不知道 C:\blah\blah 的含义(\ 用作转义字符)。相反,您需要使用 Unix 风格的路径。例如,

'C:\Users\Ian\desktop\Test2'

将转化为:

/c/Users/Ian/desktop/Test2

关于Git 钩子(Hook)预提交错误 : "command not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com