我想创建一个批处理文件,该文件回显它作为用户输入命令的一部分接收的文本。
@echo off
Echo execute a command
Set /p command=
if "%command%"=="echo 'some text'" goto echo
::my aim is to make "echo" ignored by System and Only read "'some text'"
:: like "set text_to_echo='some text'
:echo
echo %text_to_echo%
首先要求用户输入命令,然后询问用户回音的内容,如下所示,不是一个选项
@echo /p
set /p text=
if "%text%=="echo"
:echo
set /p tex1="Enter text to echo"
echo "%tex1%"
答案 0 :(得分:0)
你可以用这个:
@echo off
Echo execute a command
Set /p "command="
if NOT "%command%"=="%command:echo =%" goto echo
pause
:echo
SET "text_to_echo=%command:~5%"
echo.%text_to_echo%
pause
请注意,我还修复了您的if
,并在pause
之后添加if
,以便了解是否转到:echo
。
答案 1 :(得分:0)
您希望将输入分为第一个单词(标记)和其余部分。您可以使用for
:
@echo off
Echo execute a command
Set /p "commandstring=# "
for /f "tokens=1,* delims= " %%a in ("%commandstring%") do (
set "command=%%a"
set "params=%%b"
)
if /i "%command%"=="echo" goto :_echo
...
:_echo
echo %params%
REM also possible (at least for "echo", but I guess, that's just an example):
%command% %params%