Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new 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 Nov 16, 2017 12:56 AM

Reply
Question marked as Best reply

Posted on Dec 3, 2017 7:42 AM

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
6 replies

There are no replies.

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

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.