Can I run a Shell Script via Terminal window?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Can I run a Shell Script via Terminal window? Thu Dec 22, 2022 6:10 am • by bonn
I currently have a rule that performs "Run Shell Script" via an Embedded Script on matching files.

The embedded script executes some Python code, and it prints some results, etc, based on the matching file as an input. Via the Run Shell Script method, the textual output of the embedded shell script and the resulting Python code is "invisible" insofar as I can't see the Python print outputs (perhaps they are accessible via a Hazel log or something).

However, I'd like effectively run the script via Terminal (launch a new window/tab), so I can see the outputs there. Is this possible?

Here is the embedded script:

Code: Select all

export PATH="/opt/homebrew/bin:$PATH"   #Needed to call Homebrew, etc
. /opt/homebrew/anaconda3/etc/profile.d/conda.sh
conda activate my_conda_env

cd /Users/my_user/my_python_project/

if [ -n "$1" ]
then
   python my_python_file.py $1
fi
bonn
 
Posts: 12
Joined: Sun Oct 16, 2016 5:18 am

Turn on debug mode as described here: https://www.noodlesoft.com/kb/hazel-debug-mode/

After that, script outputs will appear in Hazel's log.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Turn on debug mode as described here: https://www.noodlesoft.com/kb/hazel-debug-mode/

After that, script outputs will appear in Hazel's log.


Thanks, but I specifically want to have the script run & output to Terminal. (This is not a debugging issue, I want to be able to monitor & manually use the program when it runs).

Is there a way to run the script in Terminal?
bonn
 
Posts: 12
Joined: Sun Oct 16, 2016 5:18 am

It might be better then to have a script open another script in Terminal. You can use the "open" command to do that (check the man page).
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:It might be better then to have a script open another script in Terminal. You can use the "open" command to do that (check the man page).


If the Hazel rule runs an Embedded Shell Script as below, for Shell: /bin/zsh:

Code: Select all
open -a Terminal path/to/my_external_script.sh $1


And my_external_script.sh contains the code:

Code: Select all
#!/bin/zsh
echo "$1"


Do you have any idea why the Terminal prints an empty string?

NB: Hazel Renames the file in the action step just before attempting to Run Shell Script on it.
bonn
 
Posts: 12
Joined: Sun Oct 16, 2016 5:18 am

What happens if you enter that command in Terminal itself? Does it work there?
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can I run a Shell Script via Terminal window? Wed Dec 28, 2022 10:21 am • by bonn
Mr_Noodle wrote:What happens if you enter that command in Terminal itself? Does it work there?


I was able to find a solution on the link below, though convoluted. It seems like open whilst using open -a Terminal path/to/my_external_script.sh doesn't want to jive with a path-like argument via $1.

https://stackoverflow.com/questions/295 ... en-command

Based on the answers there, I set Hazel's Run Embedded Shell Script to run the code below, which creates and runs a temporary shell script, which in turn successfully opens a new Terminal window, which then runs the actual shell script together with the matching file path argument for $1.

Code: Select all
echo "path/to/my_external_script.sh '$1'; rm /tmp/temp_script.sh" > /tmp/temp_script.sh; chmod +x /tmp/temp_script.sh; open -a Terminal /tmp/temp_script.sh


After execution, this will delete the temporary shell script via rm /tmp/temp_script.sh

I'm not sure if all this manoeuvring is anecessary to get Hazel to run a shell command with a path argument in a new Terminal window, or if it can be done more easily, but this seems to work at least.

The Terminal window will successfully print the path of the matching file $1, instead of an empty string.
bonn
 
Posts: 12
Joined: Sun Oct 16, 2016 5:18 am

Maybe you can set an environment variable instead? Not sure how that carries over if you launch Terminal though.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support

cron