Search code examples
bashnpmmintty

Shell command to open other shells and run commands


I'm attempting to script a process to open two additional shell windows and send commands to them to run some node modules I have installed. This is my first time doing bash scripting so if I'm screwing something up please feel free to let me know.

I have this script

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

I have it in a file called startdev in a folder that is on my PATH variable (I'm on Windows 10), so I can open up a shell from anywhere and type in startdev -g or startdev -g -h to run this.

This all works, wonderfully I might add, when it opens the shell and sends the gulp command, it detects my gulpfile and is able to run the default task on it like I want it to. However, the http-server isn't doing the same thing, it just tells me http-server ./ -p 8111: No such file or directory.


Solution

  • Mintty treats the first argument as command name, with all the options you pass because of qoutes. Arguments to program started by other program (i. e. with sudo, screen, etc) are usually passed as separate arguments to avoid parsing, so you should try start mintty http-server $run_http_location -p $run_http_port, without quotes.