简体   繁体   English

从同一个批处理文件中读取批处理文件的第一行?

[英]Read the first line of batch file from the same batch file?

I have a batch file that tries to run the program specified in its first line. 我有一个批处理文件,尝试运行第一行中指定的程序。 Similar to Unix's shebang : 与Unix的shebang类似:

C:\> more foo.bat
#!C:\Python27\python.exe
%PYTHON% foo-script.py
C:\>

What I want to know is: is there a way to automatically set %PYTHON% to C:\\Python27\\python.exe which is specified in the first line of the script following the shebang ( #! )? 我想知道的是:有没有办法自动%PYTHON%设置为C:\\Python27\\python.exe ,这是在shebang( #! )之后的脚本的第一行中指定的?

Background : I am trying to do this so as to explicitly specify the Python interpreter to invoke (as there are multiple Python interpreters installed on the system) in the wrapper script. 背景 :我正在尝试这样做,以便在包装器脚本中显式指定要调用的Python解释器(因为系统上安装了多个Python解释器)。

Assumption : You can assume that script already knows it's own filename ( foo ) and %~dp0 is the directory of this script. 假设 :您可以假设脚本已经知道它自己的文件名( foo ), %~dp0是此脚本的目录。 How do we read the first line excluding the shebang? 我们如何阅读不包括shebang的第一行?

Clarification : Adding C:\\PythonXY to %PATH% is not a solution. 澄清 :将C:\\ PythonXY添加到%PATH%不是解决方案。 The shebang line is supposed to be modified during install time (the original script is generated on the build machine only) on the user's machine .. which may have multiple Python installations of the same version. shebang行应该在用户机器的安装时间(原始脚本仅在构建机器上生成)中进行修改..这可能有多个相同版本的Python安装。 And only the shebang line is modifyable (that is how the program works). 只有shebang线是可修改的(这是程序的工作方式)。

Firstly , on windows, there is no need for shebang. 首先,在窗户上,没有必要。 Its best to include the path of the Python interpreter to the PATH environment variable of the user who is running the script. 最好将Python解释器的路径包含在运行脚本的用户的PATH环境变量中。 That said, to get the first line in batch, you can use set 也就是说,要批量获取第一行,您可以使用set

set /p var=<file

since you have multiple version of interpreter, why not just use the correct one when you invoke the script? 因为你有多个版本的解释器,为什么不在调用脚本时使用正确的解释器呢?

c:\python27\bin\python.exe myscript.py

Edit: 编辑:

@echo off
set /p var=<test.py
call %var:~2% test.py

output 产量

C:\test>more test1.py
#!c:\Python26\python.exe

print "hello"


C:\test>more test.bat
@echo off
set /p var=<test1.py
call %var:~2% test1.py

C:\test>test.bat
hello

Shebangs are only native to the filesystems of Unix/Linux/BSD variants, due to their design and layout, the filesystem knows that when a file begins with a shebang, the filesystem triggers the user's terminal process to invoke the shell based on the shebang, usually /bin/sh . Shebangs只是Unix / Linux / BSD变种文件系统的原生,由于它们的设计和布局,文件系统知道当文件以shebang开头时,文件系统触发用户的终端进程根据shebang调用shell,通常/bin/sh This can be confirmed by browsing through the filesystem routines here . 这可以通过文件系统程序浏览确认这里

Edit: Have edited my answer to make it fully clear in the context of the Windows environment. 编辑:编辑了我的答案,使其在Windows环境的上下文中完全清楚。

In short this is not possible under Windows (2000 upwards to Windows 7) as the NTFS lacks such a capability to interpret the contents of the data (NTFS does not care what is in the file), and therefore lack the means of having an exec call to load the shebang script, only .EXE, .DLL, .SYS,.DRV's are catered for, but for a script.... 简而言之,这在Windows(2000向上到Windows 7)下是不可能的,因为NTFS缺乏解释数据内容的这种能力(NTFS不关心文件中的内容),因此缺乏拥有exec调用加载shebang脚本,只提供.EXE,.DLL,.SYS,.DRV,但对于脚本....

Hope this helps, Best regards, Tom. 希望这会有所帮助,最好的问候,汤姆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM