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:
Open directory
Activate python virtualenv
Run django manage command
Switch to the new tab
change directory
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.
Install NPM.
Install TTab.
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.
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.
Related
after installing the bun framework I'm prompted to run the following two commands.
Manually add the directory to your $HOME/.zshrc (or similar)
export BUN_INSTALL="/Users/foo/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
after I run these in my terminal bun is working. If I am to close my terminal, reopen and run bun --help I see zsh: command not found: bun
until I run the two following commands from above again. How can I resolve this to not have to repeat those commands?
If you running those export statements from your current shell / cli than those are available only in the current shell environment. It's not available in any other shell you launch.
You need to put them in your ~/.zshrc so they are exported in every shell environment you launch.
Or you can just put them in your ~/.profile ( or ~/.zprofile , whichever you are using) and do source ~/.[z]profile and it would be available in every subsequent shell you launch.
I've set up Ubuntu in Windows 10 and am in my home directory:
/mnt/c/Users/Me$
I want to end up in my environment directory and have it activated.
I created a file called goenv.sh in this directory and it contains:
#!/bin/bash
source environments/my_env/bin/activate
I changed the permissions of goenv.sh then tried to run it with:
./goenv.sh
...but nothing happens. What am I missing?
The script ./goenv.sh runs in a sub shell. That means that all environment changes are local to that sub shell but have no impact on the calling shell (the shell where you started it).
You need to define a function in your .bashrc to achieve that:
function goenv() {
cd environments
source my_env/bin/activate
}
Anyhow, that looks like a Python virtualenv. I recommend to install virtualenvwrapper which comes with handy shell functions to work with virtualenvs. Having it installed, you just run:
workon my_env
It works now. I changed the script shown in my question to:
#!/bin/bash
cd environments
source ./my_env/bin/activate
I have a simple Gruntfile that I want to be able to run from an icon in my OSX dock.
I have created a very simple shell script (launcher.sh) that I will save as an application so I can add it to my dock. It works great when I run it directly in my Terminal:
#!/usr/bin/env bash
$(grunt serve --open)
It also works fine with this shebang: #!/bin/bash
However when I call the shell script from an Automator workflow I get the following error:
launcher.sh: line 2: grunt: command not found
This is my Automator set up:
What am I doing wrong?
Update
If I put this in my launcher.sh file:
#!/bin/bash
`/usr/local/bin/grunt serve --open`
I get a different error in Automator: env: node: No such file or directory
But, as before, if I run the script directly in Terminal its fine - so I think #mklement0 is right about the PATH
Update 2
launcher.sh
#!/bin/bash
grunt serve --open
Automator
PATH="/usr/local/bin:$PATH"
~/Public/Sites/launcher.sh
Now I'm still getting an error popup when I run it in Automator, but it has no details - it just says:
The action "Run Shell Script" encountered an error.
The Log panel shows a blank entry. Is there a way to get more info? A verbose mode perhaps?
Update 3
So this is weird... if I use &> ~/log it works. Without it it fails.
But this is working, thanks #mklement0, it'll do for now
PATH="/usr/local/bin:$PATH"
cd ~/Public/Sites && ./launcher.sh &> ~/log
The problem is that the $PATH variable when running from Automator doesn't have the same entries as when running from Terminal.
Notably, /usr/local/bin is missing, which is where grunt is typically installed (if installed globally).
A simple workaround is to add the folder in which grunt is installed to your $PATH at the top of the Automator shell script:
PATH="/usr/local/bin:$PATH"
~/Public/Sites/Launcher.sh
Aside from that:
Your shell command, $(grunt serve --open), should be just grunt serve --open - no need for a command substitution ($(...) or `...`), as that would actually first execute the command and then try to execute the output from that command.
The default working dir. when running a shell script from Automator is ~ (your home folder), which may not be what your script expects; it looks like your script expects its own dir. to be the working dir., so use cd ~/Public/Sites && ./launcher.sh to invoke it.
Automator will report an error in case the shell script exits with a nonzero exit code; the error message will include the shell script's stderr output (and nothing else) - sounds like no stderr output is being produced in your case.
To capture all output for diagnostic purposes, use something like ./launcher.sh &> ~/log
On macOS 10.11 through at least 10.15 (as of this update), $PATH has the following value inside a shell script run from an Automator workflow: /usr/bin:/bin:/usr/sbin:/sbin
I'm trying to write a script shell (Mac OS X) for deploying my Awestruct site within a launch agent.
So, I have to generate site before deploying it. The command to generate site is :
awestruct -g
My script is myscript.sh :
cd /my/site/structure/base/directory
awestruct -g
This script is launched by the system when an event occurs.
But the problem is it doesn't know about awestruct...
My awestruct program is a part of my ruby installation and is added to my PATH variable.
When I run the script manually, it works (because it's my user who is lauynching it)
When the system detect the event and runs the script, it results as :
awestruct: command not found
The problem is the PATH...
If it works manually, then in the same prompt where it works, run command:
which awestruct
That will print the program file with full path, let's assume /usr/local/bin/awestruct, but use whatever it really is. Then use that in your script, for example:
cd /my/site/structure/base/directory
/usr/local/bin/awestruct -g
This assumes that there are no other environment variables, only defined for your account, which awestruct needs. If there are, then find out what they are, and add them to your script before running awestruct, for example with line:
export AWESTRUCT_ENVIRONMENT_VARIABLE=foobar
(Note: When you run the script normally like any program, that will not change the parent shell environment.)
You can also add the path to the executable in the PATH of the user that run the script and gets the error.
You could try something like :
$ su - <user_that_run_the_script>
$ echo "export PATH=$PATH:$(which awestruct)" >> ~/.bash_profile
$ source ~/.bash_profile
(For Linux users, use ~/.bashrc instead of ~/.bash_profile)
I'm trying to write (what I thought would be) a simple zsh script to set up my development environment. The problem I'm running into is that once I get to part of my process, that tab in the shell is devoted only to the last process when I need it to continue running other commands.
More explicitly, I need to run:
lein ring server-headless
then open a new tab, and run
cd ../
cd my_directory/
nodemon
I'm sure this has a simple solution/I should have been a better-Googler, I'm just new to this and didn't know how to word my problem.
Instead of trying this with bash/zsh/etc. I would recommend using Tmux instead :)
Take a look at Tmuxinator for this stuff: https://github.com/aziz/tmuxinator
If you don't want to use tmuxinator, with a few lines of shell code you can still make tmux do this stuff.