1

I'm trying to do an alfred workflow that toggles my bluetooth connection.

STATUS=$(/usr/local/bin/blueutil status)

if [ $STATUS == "on" ]
then
/usr/local/bin/blueutil off
echo "off"
else
/usr/local/bin/blueutil on
echo "on"
fi

what am I doing wrong here?

It doesn't do anything.

2
  • What output does /usr/local/bin/blueutil status produce?
    – devnull
    Commented Mar 6, 2014 at 9:02
  • "on" – cl.ly/UGor
    – Matt
    Commented Mar 6, 2014 at 9:08

2 Answers 2

1

I'm guessing that blueutil writes to STDERR and not STDOUT. In that case, merge the former into the latter while capturing the output of blueutil.

STATUS=$(/usr/local/bin/blueutil status 2>&1)

Moreover, you want to see whether the output contains on, so instead of:

if [ $STATUS == "on" ]

say:

if [[ $STATUS == *on ]]

instead in order to match the desired string.

0

blueutil now works a bit differently, with the status returned as '1' or '0' rather than words. Here is a script that toggles the bluetooth state, may it help others who ended up on this page via google:

#!/bin/bash
STATE=$(/usr/local/bin/blueutil -p)
((STATE ^= 1))
/usr/local/bin/blueutil -p $STATE

The ^= is the bitwise XOR-equal operator, which flips a variable between 0 and 1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .