Jump to content

[Solved] Terminal command workflow


Recommended Posts

Hopefully there are some Bash scripting gurus out there who might be able to help. I'm trying to create a "Terminal Command" workflow that does the same as Alfred's built-in terminal shortcut without having to open an actual Terminal window. I want it to have access to my usual interactive, login shell environment. (Startup files: ~/.bash_profile, ~/.bash_aliases) 

 

I've linked a 'run script' action with Bash as the interpreter.

 

I tried a basic script '{query}' which works but has no environment loaded.

I tried: 'bash -l -c "{query}"' which, for reasons I can't fathom, doesn't load my environment in Alfred but does on the command line.

I tried:

 

export BASH_ENV='/Users/Nick/.bash_profile'

bash -l -c "{query}"

 

and seem to have created a self-replicating monster that is screwing up the Terminal and my system. >.<

 

Any advice much appreciated! Thanks.

Link to comment

This is working intermittently for me:

 

source /Users/danielsh/.bash_profile

result=$(eval '{query}' 2>&1)
echo $result

But only intermittently. Functions that I've defined seem to work, but aliases do not, which seems strange. So for example, my .bash_profile contains this, which works:

# Uninstall an app with CleanMyMac
clean () { open -a "CleanMyMac" /Applications/"${1}".app; }

 

...and this, which returns "command not found":

alias myip="dig +short myip.opendns.com @resolver1.opendns.com"

 

 

But maybe that'll give you something to go on.

Link to comment

Cool thanks for the input. I found the cause of our issues. Non-interactive shells don't expand aliases by default. I'm now using the following:


shopt -s expand_aliases
source /Users/Nick/.bash_profile
{query} 2>&1 &

I needed to append the ampersand to the final line to get Sublime Text 2 to play nicely when called from the workflow though I don't understand why.

Link to comment
Cool thanks for the input. I found the cause of our issues. Non-interactive shells don't expand aliases by default. I'm now using the following:

 

shopt -s expand_aliases
source /Users/Nick/.bash_profile
{query} 2>&1 &

I needed to append the ampersand to the final line to get Sublime Text 2 to play nicely when called from the workflow though I don't understand why.

 

That's great, thanks! I've made a slight change that doesn't require the user to manually enter his or her own home path and updated the "Background Command" script I'd placed in AlfPT (which was just `eval "{query}"` before seeing this). Here's the script it's using now:

shopt -s expand_aliases

if [ -f "${HOME}/.bash_profile" ] ; then
	source "${HOME}/.bash_profile"
fi

{query} 2>&1 &

 

This, incidentally, seems to work fine with the `subl` command, though `subl` is known for occasionally not working. Here's a link to my version.

 

Edit: Whoops, that was a foolishness; I'd had `subl` aliased in my profile so that it opened a new window, and that was what solved the problem! You're right about your syntax working better than `eval`, so I changed it again and re-uploaded. It also has the benefit of not spitting out a Notification if the command has no output now. Cool!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...