Unable to get "Execute Shell Script / Task" to work

Love BTT. Hoping to get some assistance with the "Execute Shell Script / Task" action.

I have written a shell script that toggles my VPN on/off and verified that it works as expected using the terminal. My intention is to execute this script via a BTT keyboard shortcut. Unfortunately, I have been unsuccessful copy/pasting the code below into the "Execute Shell Script / Task" configuration as well as trying to execute the script as a fie using "Execute Terminal Command".

#!/bin/bash

piactl=`which piactl`
status=`$piactl get connectionstate`

if [[ $status == "Disconnect"* ]]; then
    $piactl connect
else
    $piactl disconnect
fi

status=`$piactl get connectionstate`

while [[ $status != *"onnected" ]]; do
    sleep 1

    progress=`$piactl get connectionstate`

    if [[ $progress != $status ]]; then
        echo $progress
    fi

    status=$progress
done

Any idea what I might be doing wrong? I have other BTT keyboard shortcuts that execute AppleScript without issue, but for some reason the above shell script just isn't work (i.e., nothing happens when I use the keyboard shortcut or click the "Run Script Now" button).

Thanks in advance for any help you can provide.

Figured it out.

I was unaware that the $PATH typically associated with a terminal session is not available to BTT when executing shell scripts. As a result, the which piactl line was resolving to a full path when run from the terminal ("/usr/local/bin/piactl"), but to "" (empty string) when run from BTT. The easy fix was to just hard code the path into the script instead of relying on which to determine the path.

Anyway, hope this helps someone in the future. :slight_smile:

2 Likes

This is the same problem when you use "do shell script" in AppleScript. Alternatively you can put this code:

export PATH=$PATH:/usr/local/bin/; 

at the beginn or in the first line of the script.

1 Like