0

I'm a complete noob with bash scripting so maybe a stupid question: I have 3 scripts to auto run a react web app along with firebase cloud functions, the project structure is something like this:

-general:
  |_start.sh
  |_start-frontend.sh
  |_start-backend.sh
-backend
  |_functions
-frontend
  |_my_app

please note that general is an npm project with a package.json file:

    {
    "name":"my_app",
    "version":"0.1.0",
    "workspaces":[
       "backend/functions",
       "frontend/my_app"
    ],
    "author":"Giulio Serra",
    "license":"ISC",
    "scripts": {
      "start": "sh ./start.sh"
    }
 }

so when I run npm start start.sh is executed:

#!/bin/bash
open -a Terminal "`sh start-backend.sh`"
open -a Terminal "`sh start-frontend.sh`"

here are the content of start-frontend:

#!/bin/bash
cd ../frontend/my_app
npm start

and start-backend:

  #!/bin/bash
    cd ../backend/functions
    kill $( lsof -i:8085,9000 -t )
    firebase emulators:start --import /Users/giulioserra/Documents/Applicazioni/my_app/backend/dev_res

I just want to open 2 terminals: one with react and another with the emulators suite with the functions, but all I managed to do is to open one terminal with just the emulator suite (basically start-frontend.sh gets ignored) without any logs.

if I switch the instructions on the file: start-backend it's ignored and start-fronted.sh is executed again without any logs (so I don't have any clues about the port used and compiled warnings).

Any hints on how to fix the scripts such as both start-frontend.sh and start-backend.sh are executed on two different terminal instances with the proper logs?I'm on Mac Monterey btw Thanks.

Giulio Serra
  • 253
  • 1
  • 6
  • 15
  • Wishing to run scripts in separate terminals is often a sign that you are doing something wrong. In many situations, a better solution is to run the commands in the background with output to log files, which you can then inspect from any terminal at any time. Also, `open -a` is quite specific to MacOS, and not at all portable. (Using Freedesktop's facilities like `xdg-open` is nominally somewhat more portable, but does not work on MacOS, alas.) – tripleee Aug 15 '22 at 07:11
  • The `cd` commands are weird, too. They require the script to be started in a particular directory. Perhaps see [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) and perhaps refactor to something like `cd "${0%/*}/../..."` – tripleee Aug 15 '22 at 07:13
  • Also, the backticks in the argument to `open -a` looks really weird. You don't want to use a command substitution here (that would require each script to _output_ the commands you eventually want to run). – tripleee Aug 15 '22 at 07:15
  • In general, probably try http://shellcheck.net/ before asking for human assistance. – tripleee Aug 15 '22 at 07:15
  • @tripleee First of all thanks for the answer and thanks for the tool suggested. As I said I need two terminals to see the output of the two different processes: the firebase cloud functions emulator suite and the react web app, these two need to run concurrently and not sequentially and I also need to see irt the output of the functions to check on what port they are running and I'm still not able to do that using open -a Terminal, do you have any suggestions for another command? Do i need to create two separate processes? Thanks – Giulio Serra Aug 15 '22 at 07:49

1 Answers1

0

Turns out that it was simple as:

sh start-backend.sh & sh start-frontend.sh

thanks to triplee for the help.

Giulio Serra
  • 253
  • 1
  • 6
  • 15
  • `&` runs the command in the background; you probably want `;`or perhaps `&&`which only runs the second command if the first succeeds. – tripleee Aug 15 '22 at 08:09