首页 > 解决方案 > 批处理文件无法成功运行 bash 并打开 .sh 文件

问题描述

我正在尝试在 Windows 10 计算机上运行由 PyInstaller 创建的可执行文件。由于我的工作计算机上的某些安全性,我无法使用任何 Windows 终端 (TgApi) 运行 exe,但任何 bash 终端都可以出于我不理解的原因工作。为了解决这个问题,我正在尝试创建一个批处理文件,该文件将使用 bash 运行 exe。

这是 .bat 文件:

title Batch File
bash -x Shell_File.sh
pause

这是 .sh 文件:

#!/usr/bin/bash
chmod +x Executable.exe
bash ./Executable.exe

我不断收到的错误是Executable.exe: Executable.exe: cannot execute binary file.

标签: bashbatch-fileterminalshexe

解决方案


我找到的解决方案如下所示。

这是 .bat 文件:

REM Get the current directory and set it to CURRENT_DIR
for /f "tokens=* delims=/" %%A in ('cd') do set CURRENT_DIR=%%A

REM Set variables to forward to Bash Shell 
SET SHELLDIR=%CURRENT_DIR%
SET SHELL=%SHELLDIR%/bash.exe
SET BASH=%SHELL%

SET PATH=%SHELLDIR%;%PATH%

REM Pass variables and shell command file to Bash Shell
"%SHELL%" %* shell_commands.sh

这是 shell_commands.sh 文件:

cd (directory with executable)
./Executable.exe

推荐阅读