简体   繁体   中英

Open new tab in ITerm2 and run commands there with zsh script

I want make a shell script to startup my project env. I'm using ITerm2 with zsh and oh-my-zsh installed.

I want to:

  1. Open directory
  2. Activate python virtualenv
  3. Run django manage command
  4. Switch to the new tab
  5. change directory
  6. Run gulp command to watch for frontend changes

All I got is this:

#!/bin/zsh
cd ~/Projects/python/project_name
source ~/virtualenvs/project_name/bin/activate
python ./backend/manage.py runserver 
tab
cd front
gulp watch

And as you can expect this doesn't work. Can you point me direction where I should look or is this even possible to do with just shell script?

Completely possible.

I did pretty much the same thing you're trying (although it is a Rails project) using an NPM package called ttab.

  1. Install NPM .
  2. Install TTab .
  3. You can run the commands in the new tab like so:
# First switch to directory
cd front
# Open new tab in that directory and execute
ttab -G eval "gulp watch"

Note: You can execute several commands if needed, like gulp watch; rails s gulp watch; rails s .

  1. If you need to run a command on the original tab that is also on a different directory, you can create a procedure/function in your script file to do that:
# Define the function before it is called
gotofolder()
{
  cd ~/mydirectory
}

# start the other tabs (...)

# change the original tab directory
gotofolder
# Run Rails or whatever
./bin/rails s

If you want to take a look at how I did this, please checkout the confereai.sh script in my MDFR repo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM