Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

javascript - How to concat multiple bash commands?

Having this bash_profile commands, trying to run backend and frontend server in one single alias command i.e ins

alias is='ivui && npm run start:backend'
alias ib='ivbe && npm run start:dev'
alias ins='ib && is'

is referring to a different project folder and it starts the server and ib is also referring to a different folder and server. Trying to concat both, but the first one only triggers and concat of && is not executed.

Concat npm helps in combining both servers from single project folder, but wondering how we can get this done using bash_profile? so that by executing just ins, it must start backend server first and frontend also.

question from:https://stackoverflow.com/questions/65645771/how-to-concat-multiple-bash-commands

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason your double alias command (ib && is) does not work is because, as per man bash:

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.

Stands to reason that if you run ib && is where both ib and is are aliases, it will only run ib.

With that out of the way, there is have a different and, I believe, better solution to your problem. You can use screen to run those 2 commands in the background and also, as a bonus, have the ability to watch their terminal output any time you want.

The idea is this: 1. Start a screen session; 2. Open two windows inside that session; 3. Run npm run start:backend in 1st window and run npm run start:dev in the second window.

Here is what you need:

screen -S Servers -t backend_window -A -d -m
screen -S Servers -X screen -t dev_window

This will start a screen session with backend_window and dev_window inside it. Now you just need to run your 2 commands inside those windows:

screen -S Servers -p backend_window -X stuff $'npm run start:backend
'
screen -S Servers -p dev_window -X stuff $'npm run start:dev
'

Now both your npm commands are running in the background at the same time. You can just put these 4 lines into your .bashrc file and it will launch on log in.

But the best part about this approach is you can visually see what's going on with those npm commands by attaching the screen and looking inside those windows. Just run:

screen -rx Servers

This will show you your first window by default. Let's split the screen and show both windows side by side:

Ctr-A + | <- will split the screen into 2 sections
Ctr-A + Tab <- will shit cursor to the new section
Ctr-A + " <- will show you your 2 windows, just pick the dev

All this will give you this view enter image description here

Keep in mind that both your npm commands will continue to run even after you log out. To kill them, either attach the screen as described above and then Ctrl-C both processes. Or just run killall screen and they will just die.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...