开发者

How to pass a command as a command line argument by Batch file

开发者 https://www.devze.com 2022-12-31 18:23 出处:网络
I want to pass a command as a command line argument from one batch file to another. e.g. : first.bat: call test.bat \"echo hello world\" \"ech开发者_运维问答o welcome \"

I want to pass a command as a command line argument from one batch file to another.

e.g. :

first.bat:

call test.bat "echo hello world" "ech开发者_运维问答o welcome "

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).

In addition, test.cmd executes a command made up of every parameter, not just the first.

When you create those two files and execute first.cmd, you get:

hello world!

as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消