【问题标题】:bat command goes to next line after using the variable imbedded [closed]bat 命令在使用变量嵌入后转到下一行 [关闭]
【发布时间】:2022-10-20 22:02:11
【问题描述】:

您好,我正在尝试将此代码传递给命令行,当我手动将其复制并粘贴到我的 cmd shell 时,一切正常,但是当我在脚本中使用确切的命令时,我的命令似乎中断了多个部分,我不知道发生了什么任何想法?

python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452/spec --distpath ./artifacts-repo/2022-10-09-174452/dist --workpath ./artifacts-repo/2022-10-09-174452/build --onefile ./codes/SayHello.py

当我将其复制并粘贴到我的 CMD 中时,上面的工作正常

bat "python -m PyInstaller --specpath ./artifacts-repo/${directoryName}/spec --distpath ./artifacts-repo/${directoryName}/dist --workpath ./artifacts-repo/${directoryName}/build --onefile ./codes/SayHello.py"

但是当我尝试通过我的管道脚本传递它时,它似乎缩小了!!!结果如下:

C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452 

usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]

                   [--add-data <SRC;DEST or SRC:DEST>]

                   [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]

                   [--hidden-import MODULENAME]

                   [--collect-submodules MODULENAME]

                   [--collect-data MODULENAME] [--collect-binaries MODULENAME]

                   [--collect-all MODULENAME] [--copy-metadata PACKAGENAME]

                   [--recursive-copy-metadata PACKAGENAME]

                   [--additional-hooks-dir HOOKSPATH]

                   [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]

                   [--key KEY] [--splash IMAGE_FILE]

                   [-d {all,imports,bootloader,noarchive}]

                   [--python-option PYTHON_OPTION] [-s] [--noupx]

                   [--upx-exclude FILE] [-c] [-w]

                   [-i <FILE.ico or FILE.exe,ID or FILE.icns or Image or "NONE">]

                   [--disable-windowed-traceback] [--version-file FILE]

                   [-m <FILE or XML>] [--no-embed-manifest] [-r RESOURCE]

                   [--uac-admin] [--uac-uiaccess] [--win-private-assemblies]

                   [--win-no-prefer-redirects] [--argv-emulation]

                   [--osx-bundle-identifier BUNDLE_IDENTIFIER]

                   [--target-architecture ARCH] [--codesign-identity IDENTITY]

                   [--osx-entitlements-file FILENAME] [--runtime-tmpdir PATH]

                   [--bootloader-ignore-signals] [--distpath DIR]

                   [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a]

                   [--clean] [--log-level LEVEL]

                   scriptname [scriptname ...]

pyinstaller: error: the following arguments are required: scriptname



C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>/spec --distpath ./artifacts-repo/2022-10-09-174452 

'/spec' is not recognized as an internal or external command,

operable program or batch file.



C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>/dist --workpath ./artifacts-repo/2022-10-09-174452 

'/dist' is not recognized as an internal or external command,

operable program or batch file.



C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>/build --onefile ./codes/SayHello.py 

'/build' is not recognized as an internal or external command,

operable program or batch file.

script returned exit code 1

查看它缩小为 4 个命令的命令:

> python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452
> /spec --distpath ./artifacts-repo/2022-10-09-174452  /dist --workpath
> ./artifacts-repo/2022-10-09-174452  /build --onefile
> ./codes/SayHello.py

【问题讨论】:

  • 关于Naming Files, Paths, and Namespaces 的Microsoft 文档页面解释说Windows 上的目录分隔符是\,而不是Linux/Mac 上的/。所以应该在所有带有/ 的参数字符串中使用两个反斜杠。第一个反斜杠被 Jenkins 解释为转义字符,以解释第二个反斜杠字面上写入由 Jenkins 创建的批处理文件以供执行。将文件扩展名.exe 附加到python 也很好,即使用python.exe
  • ${directoryName} 是 Linux/Mac shell 解释器语法,用于引用名称为 direcoryName 的变量。处理批处理文件的 Windows 命令处理器 cmd.exe 不支持此语法。 cmd.exe 支持引用环境如果在命令行引用带有感叹号的环境变量之前显式启用延迟扩展,则使用语法 %directoryName%!directoryName! 的变量。
  • 看起来${directoryName} 在创建批处理文件和运行cmd.exe 选项/c 和临时创建的批处理文件之前已经被Java 扩展为解释管道脚本的行。问题显然是分配给变量directoryName 的字符串值在末尾包含一个行结束字符,如回车和/或换行符,因此批处理文件中的命令行只是python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452。用于定义变量directoryName 值的代码是主要原因。
  • 是的@Mofi那是确切的问题

标签: batch-file jenkins pipeline declarative


【解决方案1】:

管道脚本命令和变量不是问题(对于詹金斯)

但问题是:(哦,我的上帝,我现在填得太傻了-_-) 上面几行我使用了一个命令来获取创建目录的日期和时间前缀,我像下面这样回应它:

directoryName = bat(returnStdout: true,script: '@echo %date:~-4,4%-%date:~-10,2%%date:~7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%')

echo 命令添加一个 在命令之后,这就是我的命令分裂的原因! 它可以通过在命令之后使用 .trim() 来修复,如下所示

directoryName = bat(returnStdout: true,script: '@echo %date:~-4,4%-%date:~-10,2%%date:~7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%').trim()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
相关资源
最近更新 更多