相当于 Windows 中的 Unix eval
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1195494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent to Unix eval in Windows
提问by johannix
Was wondering how you'd do the following in Windows:
想知道您将如何在 Windows 中执行以下操作:
From a c shell script (extension csh), I'm running a Python script within an 'eval' method so that the output from the script affects the shell environment. Looks like this:
从 ac shell 脚本(扩展名 csh),我在 'eval' 方法中运行 Python 脚本,以便脚本的输出影响 shell 环境。看起来像这样:
eval `python -c "import sys; run_my_code_here(); "`
Was wondering how I would do something like the eval statement in Windows using Windows' built in CMD shell. I want to run a Python script within a Windows script and have the script run what the Python script prints out.
想知道我将如何使用 Windows 的内置 CMD shell 在 Windows 中执行类似于 eval 语句的操作。我想在 Windows 脚本中运行 Python 脚本,并让脚本运行 Python 脚本打印出来的内容。
** update: specified interest in running from CMD shell.
** 更新:指定从 CMD shell 运行的兴趣。
回答by user1686
If it's in cmd.exe
, using a temporary file is the only option [that I know of]:
如果它在cmd.exe
,使用临时文件是唯一的选择[我知道]:
python -c "print(\"Hi\")" > temp.cmd
call temp.cmd
del temp.cmd
回答by Jay Bazuzi
(Making some guesses where details are missing from your question)
(猜测您的问题中缺少细节的地方)
In CMD, when a batch script modifies the environment, the default behavior is that it modifies the environment of the CMD process that is executing it.
在 CMD 中,当批处理脚本修改环境时,默认行为是修改正在执行它的 CMD 进程的环境。
Now, if you have a batch script that calls another batch script, there are 3 ways to do it.
现在,如果您有一个调用另一个批处理脚本的批处理脚本,有 3 种方法可以做到。
execute the batch file directly:
REM call q.bat q.bat REM this line never runs
Usually you don't want this, because it won't return to the calling batch script. This is more likegoto
thangosub
. The CMD process just switches from one script to another.execute with
call
:REM call q.bat CALL q.bat REM changes that q.bat affects will appear here.
This is the most common way for one batch file to call another. Whenq.bat
exits, control will return to the caller. Since this is the same CMD process, changes to the environment will still be there.
Note: Ifq.bat
uses theEXIT
statement, it can cause the CMD process to terminate, without returning control to the calling script.
Note 2: Ifq.bat
usesEXIT /B
, then the CMD process will not exit. This is useful for settingERRORLEVEL
.Execute in a new CMD process:
REM call q.bat CMD /C q.bat REM environment changes in q.bat don't affect me
Since q.bat run ins a new CMD process, it affects the environment of that process, and not the CMD that the caller is running in.
Note: Ifq.bat
usesEXIT
, it won't terminate the process of the caller.
直接执行批处理文件:
REM call q.bat q.bat REM this line never runs
通常你不想要这个,因为它不会返回到调用批处理脚本。这goto
比gosub
. CMD 进程只是从一个脚本切换到另一个脚本。执行
call
:REM call q.bat CALL q.bat REM changes that q.bat affects will appear here.
这是一个批处理文件调用另一个文件的最常见方式。当q.bat
退出时,控制权将返回给调用者。由于这是相同的 CMD 过程,因此环境的变化仍然存在。
注意:如果q.bat
使用该EXIT
语句,它会导致 CMD 进程终止,而不会将控制权返回给调用脚本。
注2:如果q.bat
使用EXIT /B
,则CMD进程不会退出。这对于设置ERRORLEVEL
.在新的 CMD 进程中执行:
REM call q.bat CMD /C q.bat REM environment changes in q.bat don't affect me
由于 q.bat 运行在一个新的 CMD 进程中,它会影响该进程的环境,而不是调用者正在运行的 CMD。
注意:如果q.bat
使用EXIT
,它不会终止调用者的进程。
The SETLOCAL
CMD command will create a new environment for the current script. Changes in that environment won't affect the caller. In general, SETLOCAL
is a good practice, to avoid leaking environment changes by accident.
在SETLOCAL
CMD命令将为当前脚本的新环境。该环境的变化不会影响调用者。一般来说,这SETLOCAL
是一个很好的做法,避免意外泄漏环境变化。
To use SETLOCAL
and still push environment changes to the calling script, end the script with:
要使用SETLOCAL
并仍然将环境更改推送到调用脚本,请使用以下命令结束脚本:
ENDLOCAL && SET X=%X% && SET Y=%Y%
This will push the values of X and Y to the parent environment.
这会将 X 和 Y 的值推送到父环境。
If on the other hand you want to run another process (not a CMD script) and have it affect the current script's environment, than have the tool generate a batch file that makes the changes you want, then execute that batch file.
另一方面,如果您想运行另一个进程(不是 CMD 脚本)并让它影响当前脚本的环境,那么让该工具生成一个批处理文件来进行您想要的更改,然后执行该批处理文件。
REM q.exe will write %TEMP%\runme.cmd, which looks like: REM set X=Y q.exe call "%TEMP%\runme.cmd"