制作一个打开 3 个 cygwin 终端并执行命令的脚本 (windows)

Making a script which opens 3 cygwin terminals and executes the command (windows)

制作一个脚本,在 中打开 3 个 cygwin 终端 in windows,每个 cygwin 终端应该导航到特定的目录和运行 source .env 命令,然后 运行 npm start

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

1 号航站楼) cd C:\Users\Username\Desktop\node-service-1 && echo "source .env" && npm run start

2 号航站楼) cd C:\Users\Username\Desktop\node-service-2 && echo "source .env" && npm run start

3 号航站楼)cd C:\Users\Username\Desktop\node-service-3 && echo "source .env" && npm run start

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


原因是当我不使用cygwin时,source .env不起作用。

您可以创建三 (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/
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
)

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

#!/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

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

cd C:/Users/Username/Desktop
cd node-service-1 && bash -c "source .env && npm run start" && cd -

您的基本问题是您想要创建一个新的 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

每个节点。我在这里以 mintty 为例,但您可以根据您使用的任何终端程序调整它。