制作一个脚本,该脚本打开3个cygwin终端并执行命令(Windows)

时间:2020-08-26 18:38:42

标签: bash shell batch-file cmd cygwin

  • 我要做什么:

制作一个脚本,该脚本在Windows 中打开3个 cygwin 终端,每个cygwin终端应导航到特定目录并运行import React, { Component } from "react"; import FlightService from "../services/flight.service"; export default class AddComponent extends Component { constructor(props) { super(props); this.onChangeName = this.onChangeName.bind(this); this.onChangeFrom = this.onChangeFrom.bind(this); this.onChangeTo = this.onChangeTo.bind(this); this.onChangeDate = this.onChangeDate.bind(this); this.onChangeDepartureTime = this.onChangeDepartureTime.bind(this); this.onChangeArrivalTime = this.onChangeArrivalTime.bind(this); this.onChangeFare = this.onChangeFare.bind(this); this.saveFlight = this.saveFlight.bind(this); this.newFlight = this.newFlight.bind(this); this.state = { id: null, name: "", from: "", to: "", date: "", departuretime:"", arrivaltime:"", fare: "", submitted: false }; } onChangeName(e) { this.setState({ name: e.target.value }); } onChangeFrom(e) { this.setState({ from: e.target.value }); } onChangeTo(e) { this.setState({ to: e.target.value }); } onChangeDate(e) { this.setState({ date: e.target.value }); } onChangeDepartureTime(e) { this.setState({ departuretime: e.target.value }); } onChangeArrivalTime(e) { this.setState({ arrivaltime: e.target.value }); } onChangeFare(e) { this.setState({ fare: e.target.value }); } saveFlight() { var data = { name: this.state.name, from: this.state.from, to:this.state.to, date:this.state.date, departuretime:this.state.departuretime, arrivaltime:this.state.arrivaltime, fare:this.state.fare }; FlightService.create(data) .then(response => { this.setState({ id: response.data.id, name: response.data.name, from: response.data.from, to: response.data.to, date:response.data.date, departuretime:response.data.departuretime, arrivaltime:response.data.arrivaltime, fare:response.data.fare, submitted: true }); console.log(response.data); }) .catch(e => { console.log(e); }); } newFlight() { this.setState({ id: null, name: "", from: "", to: "", date: "", departuretime:"", arrivaltime:"", fare:"", submitted: false }); } render() { return ( <div className="submit-form"> {this.state.submitted ? ( <div> <h4>Flight added successfully!</h4> <div> <button className="btn btn-info" onClick={this.newFlight}> Add </button> </div> </div> ) : ( <div> <div className="form-group"> <label htmlFor="title">Name</label> <input type="text" className="form-control" id="title" required value={this.state.name} placeholder="Name of the Flight" onChange={this.onChangeName} name="title" /> </div> <div className="form-group"> <label htmlFor="description">From</label> <input type="text" className="form-control" id="description" required value={this.state.from} placeholder="From where?" onChange={this.onChangeFrom} name="description" /> </div> <div className="form-group"> <label htmlFor="description">To</label> <input type="text" className="form-control" id="description" required value={this.state.to} placeholder="To where?" onChange={this.onChangeTo} name="description" /> </div> <div className="form-group"> <label htmlFor="description">Date</label> <input type="text" className="form-control" id="description" required value={this.state.date} placeholder="Date of Travel" onChange={this.onChangeDate} name="description" /> </div> <div className="form-group"> <label htmlFor="description">Departure Time</label> <input type="text" className="form-control" id="description" required value={this.state.departuretime} placeholder="Time of Departure" onChange={this.onChangeDepartureTime} name="description" /> </div> <div className="form-group"> <label htmlFor="description">Arrival Time</label> <input type="text" className="form-control" id="description" required value={this.state.arrivaltime} placeholder="Time of Arrival" onChange={this.onChangeArrivalTime} name="description" /> </div> <div className="form-group"> <label htmlFor="description">Fare</label> <input type="text" className="form-control" id="description" required value={this.state.fare} placeholder="Fare of Flight" onChange={this.onChangeFare} name="description" /> </div> <button onClick={this.saveFlight} className="btn btn-info"> Submit </button> </div> )} </div> ); } } 命令,然后运行{{ 1}}

到目前为止,我能做的是在bat脚本中执行命令:

端子1)source .env

2号接线端子)npm start

3号终端)cd C:\Users\Username\Desktop\node-service-1 && echo "source .env" && npm run start

相反,我想在cygwin中做同样的事情,并使cygwin保持打开状态。


原因是因为当我不使用cygwin时,源.env无法正常工作。

3 个答案:

答案 0 :(得分:1)

您可以创建三(3)个文件。前两个(2)属于用户的Cygwin主目录。第三个脚本是.bat脚本,可以在任何地方。

===〜/ .env === C:\ cygwin64 \ home \ username \ .env

export AVAR=something

===〜/ doit.bash === C:\ cygwin64 \ home \ username \ doit.bash

cd $(cygpath -u $USERPROFILE)/Desktop/$1
echo source ~/.env
echo npm run start

=== trycyg.bat

FOR %%A IN (node-service-1 node-service-2 node-service-3) DO (
    START "node1" "C:\cygwin64\bin\bash.exe" --login -i ~/doit.bash %%~A
)

答案 1 :(得分:1)

您可以将以下脚本保存在test.sh中并运行它:

DECLARE @Test VARCHAR(MAX) = '\\asda.xxv.ww\data\tas\Test\Clients\asdas\TestFolder\QA'
DECLARE @Test2 VARCHAR(MAX) = '\\uxcvs.xcxcv.xcv\data\tas\Test\Clients\xcvcx\xcvxcv\QA\'

SELECT RIGHT(SUBSTRING(@Test, 2, LEN(@Test)-2), CHARINDEX('\', SUBSTRING(REVERSE(@Test), 2, LEN(REVERSE(@Test))-2), (CHARINDEX('\', SUBSTRING(REVERSE(@Test), 2, LEN(REVERSE(@Test))-2))))) AS DiconFilePath , 
RIGHT(SUBSTRING(@Test2, 2, LEN(@Test2)-2), CHARINDEX('\', SUBSTRING(REVERSE(@Test2), 2, LEN(REVERSE(@Test2))-2), (CHARINDEX('\', SUBSTRING(REVERSE(@Test2), 2, LEN(REVERSE(@Test2))-2))))) AS DiconFilePath2

要进行故障排除,请运行以下命令:

#!/usr/bin/env bash

cd C:/Users/Username/Desktop
for service in node-service-1 node-service-2 node-service-3; do
    cd $service && cygstart bash -c "source .env && npm run start" && cd -
done

答案 2 :(得分:1)

您的基本问题是,您想创建一个新的bash会话并首先运行一些安装命令,然后保持bash会话打开。您在这里的新终端中启动它并不重要。

我会(错误地)使用您的~/.bashrc和一个环境变量来传达是否启动服务,如果是,则表明是哪个:在.bashrc末尾,执行

if [[ -n $node_service ]]
then
  ns_dir=/cygdrive/c/Users/Username/Desktop/node-service-$node_service 
  if [[ -d $ns_dir ]]
  then
    cd $ns_dir
    echo source .env # What's the purpose of this?
    npm run start
  else
    echo "ERROR: Directory '$ns_dir' does not exist"
  fi
  # Make sure that npm is not run again in a bash subshell
  node_service=
fi

使用此设置,您可以编写Windows批处理脚本,执行以下操作:

set node_service=1
start c:\cygwin64\bin\mintty.exe /usr/bin/bash -l -i
每个节点的

。我以此处的薄荷糖为例,但是您可以根据所使用的任何终端程序对其进行调整。