简体   繁体   English

如何通过批处理文件将命令作为命令行参数传递

[英]How to pass a command as a command line argument by Batch file

I want to pass a command as a command line argument from one batch file to another. 我想将命令作为命令行参数从一个批处理文件传递到另一个批处理文件。

eg : 例如:

first.bat: first.bat:

call test.bat "echo hello world" "echo welcome "

test.bat: test.bat的:

set initialcommand=%1

set maincommand=%2

%maincommand%

%initialcommand%

Here's what you need: 这就是你需要的:

first.cmd:

@echo off
set maincommand=echo hello world!
call test.cmd %maincommand%

test.cmd:

@echo off
%*

In this case first.cmd passes the actual command (your example just passed the constant string "maincommand" rather than its value). 在这种情况下, first.cmd传递实际命令(您的示例只传递常量字符串"maincommand"而不是其值)。

In addition, test.cmd executes a command made up of every parameter, not just the first. 另外, test.cmd执行由每个参数组成的命令,而不仅仅是第一个参数。

When you create those two files and execute first.cmd , you get: 当您创建这两个文件并执行first.cmd ,您会得到:

hello world!

as expected. 正如所料。

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

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