【问题标题】:Shell command to open other shells and run commandsShell 命令打开其他 shell 并运行命令
【发布时间】:2024-11-01 21:05:02
【问题描述】:

我正在尝试编写一个进程来打开另外两个 shell 窗口并向它们发送命令以运行我已安装的一些节点模块。这是我第一次编写 bash 脚本,所以如果我搞砸了,请随时告诉我。

我有这个脚本

#!/bin/bash

# [-g]
# [-h]
# [-l <location to start the http-server on --default ./>]
# [-p <port to start the http-server on --default "8111">]

run_gulp=false
run_http=false
run_http_port=8111
run_http_location=./

while getopts ghl:p: opt; do
    case $opt in
        g)
            run_gulp=true
            ;;
        h)
            run_http=true
            ;;
        l)
            run_http_location=$OPTARG
            ;;
        p)
            run_http_port=$OPTARG
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            ;;
    esac
done

if [ $run_gulp == true ]
then
    start mintty "gulp" # this works
fi

if [ $run_http == true ]
then    
    start mintty "http-server $run_http_location -p $run_http_port"
fi

我将它保存在我的 PATH 变量(我在 Windows 10 上)的文件夹中的一个名为 startdev 的文件中,因此我可以从任何地方打开一个 shell 并输入 startdev -gstartdev -g -h 以运行这个。

这一切都有效,我可以补充一下,当它打开 shell 并发送 gulp 命令时,它会检测到我的 gulpfile 并能够像我想要的那样在其上运行默认任务。然而,http-server 并没有做同样的事情,它只是告诉我http-server ./ -p 8111: No such file or directory

【问题讨论】:

    标签: bash npm mintty


    【解决方案1】:

    Mintty 将第一个参数视为命令名称,以及由于 qoutes 而传递的所有选项。由其他程序启动的程序的参数(即使用 sudo、screen 等)通常作为单独的参数传递以避免解析,因此您应该尝试start mintty http-server $run_http_location -p $run_http_port,不带引号。

    【讨论】:

    • 嗯,当你这样说的时候,它是完全有道理的!这解决了问题,非常感谢!