如何通过批处理文件将命令作为命令行参数传递 [英] How to pass a command as a command line argument by Batch file

查看:53
本文介绍了如何通过批处理文件将命令作为命令行参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将命令作为命令行参数从一个批处理文件传递到另一个.

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

例如:

first.bat:

first.bat:

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

test.bat:

set initialcommand=%1

set maincommand=%2

%maincommand%

%initialcommand%

推荐答案

这是您需要的:

first.cmd:

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

test.cmd:

@echo off
%*

在这种情况下, first.cmd 传递实际命令(您的示例只是传递了常量字符串"maincommand" 而不是其值).

In this case first.cmd passes the actual command (your example just passed the constant string "maincommand" rather than its value).

此外, test.cmd 执行一个由每个参数组成的命令,而不仅仅是第一个.

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

创建这两个文件并执行 first.cmd 时,您将得到:

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

hello world!

符合预期.

这篇关于如何通过批处理文件将命令作为命令行参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆