Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: Shell-Script 'if on==on' 'on==on': command not found

Hello,


I was just trying to expand my script to enable/disable my AirPort card with launch/quit an application and for some reason, the syntax in if is recognized as unknown command.


The script I'm trying to expand (found somewhere on the internet some time ago) is as follows:


device="$(networksetup -listallhardwareports |

grep -E '(AirPort|Wi-Fi)' -A 1 | grep -o "en.")"

[[ "$(networksetup -getairportpower $device)" == *On ]] && val=off || val=on

networksetup -setairportpower $device $val


after multiple tries of shorter forms I finally tried expanding with:


if [["$val"=="on"]]

then osascript -e 'quit app "AdGuard"'

else

open /Applications/AdGuard.app

fi


I only get the error "on==on: command not found" or "off==on: command not found"


I hope someone is able to help me out on this.

MacBook Pro (13-inch, 2017, 4 TBT3), macOS High Sierra (10.13), null

Posted on

Reply
Question marked as Solved
Answer:
Answer:

Bash expects white-space around its operators unless it is a variable or constant assignment. You have multiple syntax issues that can be improved:

  1. see the test(1) man page
    1. The string equality symbol is '=', not '=='.
    2. There is no splat '*' wildcard syntax in ordinary Bash string comparisons as you have shown.
  2. You can use egrep instead of grep -E.
  3. [[ $(networksetup -getairportpower $devices) =~ "On" ]] && val="off" || val="on"
  4. Google Bash compound if

Posted on

Question marked as Helpful

Dec 3, 2017 8:02 AM in response to VikingOSX In response to VikingOSX

There is no splat '*' wildcard syntax in ordinary Bash string comparisons as you have shown.

Actually there is. From 'man bash'

When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching, as if the extglob shell option were enabled.

I use this all the time in shell scripts.

There’s more to the conversation

Read all replies

Page content loaded

Nov 16, 2017 3:58 AM in response to Free 4 Live In response to Free 4 Live

Solved it with the almost same argument as above:


device="$(networksetup -listallhardwareports |

grep -E '(AirPort|Wi-Fi)' -A 1 | grep -o "en.")"

[[ "$(networksetup -getairportpower $device)" == *On ]] && val=off || val=on

networksetup -setairportpower $device $val

[[ "$(networksetup -getairportpower $device)" == *Off ]] && osascript -e 'quit app "AdGuard"' || open /Applications/AdGuard.app


Works now as expected.

Nov 16, 2017 3:58 AM

Reply Helpful
Question marked as Solved

Dec 3, 2017 7:42 AM in response to Free 4 Live In response to Free 4 Live

Bash expects white-space around its operators unless it is a variable or constant assignment. You have multiple syntax issues that can be improved:

  1. see the test(1) man page
    1. The string equality symbol is '=', not '=='.
    2. There is no splat '*' wildcard syntax in ordinary Bash string comparisons as you have shown.
  2. You can use egrep instead of grep -E.
  3. [[ $(networksetup -getairportpower $devices) =~ "On" ]] && val="off" || val="on"
  4. Google Bash compound if

Dec 3, 2017 7:42 AM

Reply Helpful (1)

Dec 3, 2017 7:42 AM in response to VikingOSX In response to VikingOSX

Thanks for the explanation, I can't remember all I tried back then, but it seems like I missed trying it like this!


Since the part for Wifi and launching/quitting applications with it is working fine now, is there any chance you could help me with creating a script for doing the same for bluetooth? It doesn't seem to work with just changing the network card in the lines from wifi and the script I found on the internet is rather.. inefficient (slow, and uses quite some code). So, the only reason I'm using this, is because I couldn't make it work otherwise and also couldn't find any better hints anywhere. Again, I don't remember where I found this code. Just some copy&paste work I did a while ago.


The script I'm currently using (python):


import sys, objc

import time

from CoreFoundation import *



IOBT_BRIDGESUPPORT = '''<?xml version="1.0" standalone="yes"?>

<!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd">

<signatures version="0.9">

<function name="IOBluetoothPreferenceGetControllerPowerState">

<retval type="i"></retval>

</function>

<function name="IOBluetoothPreferenceSetControllerPowerState">

<arg type="i"></arg>

<retval type="i"></retval>

</function>

</signatures>'''



objc.initFrameworkWrapper(

frameworkName="IOBluetooth",

frameworkIdentifier="com.apple.Bluetooth",

frameworkPath=objc.pathForFramework('/System/Library/Frameworks/IOBluetooth.fra mework'),

globals=globals()

)

objc.parseBridgeSupport(

IOBT_BRIDGESUPPORT,

globals(),

objc.pathForFramework('/System/Library/Frameworks/IOBluetooth.framework')

)



def set_ioblpstate(s):

# int s : 0 = off, 1 = on

IOBluetoothPreferenceSetControllerPowerState(s)

s1 = -1

for i in range(50):

s1 = get_ioblpstate()

if s1 == s:

break

time.sleep(0.1)

if s1 != s:

sys.stderr.write('Unable to set bluetooth power state to %s\n' % ('off' if s == 0 else 'on').encode('utf-8'))

sys.exit(1)

return s1



def get_ioblpstate():

return IOBluetoothPreferenceGetControllerPowerState()



def main():

print '%d' % set_ioblpstate(1 if get_ioblpstate() == 0 else 0)

sys.exit(0)



main()

Dec 3, 2017 7:42 AM

Reply Helpful
Question marked as Helpful

Dec 3, 2017 8:02 AM in response to VikingOSX In response to VikingOSX

There is no splat '*' wildcard syntax in ordinary Bash string comparisons as you have shown.

Actually there is. From 'man bash'

When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching, as if the extglob shell option were enabled.

I use this all the time in shell scripts.

Dec 3, 2017 8:02 AM

Reply Helpful (1)

Dec 3, 2017 8:14 AM in response to Free 4 Live In response to Free 4 Live

I was just trying to expand my script to enable/disable my AirPort card with launch/quit an application and for some reason, the syntax in if is recognized as unknown command.

You may be interested in ControlPlane

<http://www.controlplaneapp.com>

It can do things like turn on or off your Wi-Fi. based on whether a program is running or not. While I do not mess with my WiFi, I do change my network "Location" to own with Proxies when I launch the Cisco AnyConnect VPN app I used when connected to work, and when I quick AnyConnect, ControlPlane switches my network location back to a Location without proxies setup.


Then again, it is fun to write your own. I even wrote my own script years ago and posted it in MacOSXHints. But then someone actually spent the time to create ControlPlane, and I found it easier to just use the app.

Dec 3, 2017 8:14 AM

Reply Helpful
User profile for user: Free 4 Live

Question: Shell-Script 'if on==on' 'on==on': command not found