使用<重定向命令输入. [英] Redirecting command input using <

查看:73
本文介绍了使用<重定向命令输入.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入重定向适用于.exe文件或内部Windows命令.

Input redirection is working for .exe files or internal windows commands.

    app.exe < ListOfNames.txt
    sort < input.txt

但是,当我尝试将其重定向到批处理脚本时,它不起作用.

However it isn't working when I try to redirect it into a batch script.

test.bat:-

test.bat :-

 @echo off
 echo %1 %2

使用:-

test.bat<input.txt

其中input.txt具有两个字符串.

where input.txt has two strings.

但是,即使在批处理脚本的情况下,它也可以很好地用于重定向输出.

However, it is working fine for redirecting output even in case of batch scripts.

这是预期的行为,还是我在语法上犯了错误?还有什么其他方法可以从文件中读取参数,而不是手动对其进行解析?

Is this the expected behavior or I am making some syntax mistake? Is there any other way to read arguments from a file instead of manually parsing it?

推荐答案

命令行上提供的参数与stdin(重定向输入所在的位置)完全不同.对于批处理脚本和.exe程序都是如此.

Parameters that are provided on the command line are completely different than stdin ( where your redirected input goes). This is true for both batch scripts as well as .exe programs.

某些程序被设计为通过命令行参数或stdin接受相同的值.但这不是常态.这是程序开发人员提供的功能.

Some programs are designed to accept the same values via command line arguments or stdin. But that is not the norm. That is a feature that is provided by the developer of the program.

如果要在批处理脚本中读取重定向的输入,则必须执行以下操作之一.

If you want to read redirected input within a batch script, then you must do one of the following.

要阅读一行:

set /p "ln="
echo %ln%

要读取循环中的所有行,请执行以下操作:

To read all lines in a loop:

for /f "delims=" %%A in ('findstr "^"') do (
  echo %%A
)

这篇关于使用&lt;重定向命令输入.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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