简体   繁体   English

批处理文件提示使用管道中的空间

[英]batch file prompt using space in pipe

I am using firebase as my host. 我正在使用Firebase作为主机。 When I upload my webpage I need to call two cmd instructions: 当我上传网页时,我需要调用两个cmd指令:

  1. firebase init Firebase初始化
  2. firebase deploy 火力基地部署

When I run firebase init I must answer some questions to go on. 当我运行firebase init时,我必须回答一些问题才能继续。 To the first question I enter y for yes and the second I need to enter space to deselect one item and then enter to go on. 对于第一个问题,我输入y表示是,第二个问题我需要输入空格以取消选择一项,然后继续输入。

I am trying to use a batch file for this instructions. 我正在尝试将批处理文件用于此说明。

I found out that I can use a pipe with following content in a batch file, the file looks like this: 我发现可以在批处理文件中使用包含以下内容的管道,该文件如下所示:

echo y | firebase init

but this "echo y" is only the first instruction for firebase init. 但是此“ echo y”只是firebase init的第一条指令。 After that I need to deselect one item with "space" and than "enter". 之后,我需要取消选择“空格”而不是“输入”的一项。

How can I write: 我该怎么写:

(echo y
echo "space"
echo "enter") | firebase init

First the firebase.json file must be created with echo in the right directory. 首先,必须在正确的目录中创建带有echo的firebase.json文件。 This replaces the firebase init command. 这将替换firebase init命令。

Then run: firebase deploy -P project alias. 然后运行:firebase deploy -P项目别名。

In bash it looks like: 在bash中,它看起来像:

echo $'{ "database": { "rules": "database.rules.json" }, "hosting": { "public": "public", "rewrites": [ { "source": "**", "destination": "/index.html" } ] } }\\n' >firebase.json echo $'{“ database”:{“ rules”:“ database.rules.json”},“ hosting”:{“ public”:“ public”,“ rewrites”:[{“ source”:“ **”, “目标”:“ / index.html”}]}} \\ n'> firebase.json

firebase deploy -P Test_Google_Analytics firebase deploy -P Test_Google_Analytics

Try it with 尝试一下

(
 (echo(y) 
 (echo( )
 (echo()
) | firebase init

This sends through the pipe 这通过管道发送

y<Linefeed>
<space><Linefeed>
<Linefeed>

The parenthesis seems to be unbalanced, but it's correct this way as echo( ensures that the content will be printed as is. 括号似乎是不平衡的,但是这种方式是正确的,如echo(确保内容按原样打印。

Should this work for you? 这应该为您工作吗?

@echo off

:: start command
firebase init

:: wait command to load. IP must be unreachable for -w to work as expected
ping 1.1.1.1 -n 1 -w 500 >NUL

call:sendKeys

exit/B

:: spawn a cscript to echo desired keys to the same cmd window
:: sleep time may need to be changed
:sendKeys
SetLocal 
set "_vbs_file_=%TEMP%\keys.vbs"
(
  echo(set oWS ^= CreateObject^("wScript.Shell"^)
  echo(wScript.Sleep 100
  echo(oWS.SendKeys "y"
  echo(wScript.Sleep 100
  echo(oWS.SendKeys " "
  echo(wScript.Sleep 100
  echo(oWS.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\keys.vbs" (set "_spawn_=%TEMP%\keys.vbs") else (set "_spawn_=keys.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
exit/B 0

Edit: as echo sends an explicit carriage return, the sendkeys may be (also ping delay is now 1500 miliseconds) 编辑:随着回显发送明确的回车,sendkeys可能是(也ping延迟现在是1500毫秒)

@echo off

:: start command
firebase init

:: wait command to load. IP must be unreachable for -w to work as expected
ping 1.1.1.1 -n 1 -w 1500 >NUL

call:sendKeys

exit/B

:: spawn a cscript to echo desired keys to the same cmd window
:: sleep time may need to be changed
:sendKeys
SetLocal 
set "_vbs_file_=%TEMP%\keys.vbs"
(
  echo(set oWS ^= CreateObject^("wScript.Shell"^)
  echo(wScript.Sleep 100
  echo(oWS.SendKeys "y"
  echo(wScript.Sleep 100
  echo(oWS.SendKeys "{ENTER}"
  echo(oWS.SendKeys " "
  echo(wScript.Sleep 100
  echo(oWS.SendKeys "{ENTER}"
  echo(wScript.Sleep 100
  echo(oWS.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\keys.vbs" (set "_spawn_=%TEMP%\keys.vbs") else (set "_spawn_=keys.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
exit/B 0

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

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