BASH Help

A place where you can chat about anything that isn't to do with games!
Post Reply
User avatar
njiska
Posts: 2412
Joined: Sun Jan 10, 2010 8:36 am
Location: Waterloo, On, Canada

BASH Help

Post by njiska »

Hey, it's been about 8 years since I last wrote a BASH script and I could use some quick help. Specifically I've forgotten how to write proper if comparison statements for program output. Essentially this is psuedocode of what I need:

Code: Select all

if <output of 'networksetup -getdnsservers Wi-Fi'> = '208.122.23.22\n 208.122.23.23'
   networksetup -setdnsservers Wi-Fi empty
else
   networksetup -setdnsservers Wi-Fi 208.122.23.22 208.122.23.23
I can't for the life of me remember how to correctly script that comparison in bash. It should be a stupid simple line so any help that can be offered is greatly appreciated.

I'm trying to quickly toggle between automatic and two set DNS servers in OS X and it seems to me a script is the easiest way.
Look at our friendly members:
MX7 wrote:I'm not a fan of a racist, gun nut brony puking his odious and uninformed arguments over every thread that comes up.
Drum wrote:He's also a pederast. Presumably.
Barrakketh
Posts: 317
Joined: Sat Jan 22, 2011 10:44 pm

Re: BASH Help

Post by Barrakketh »

-eq

Code: Select all

[ `networksetup -getdnsservers Wi-Fi` -eq "208.122.23.22\n 208.122.23.23" ]
Or something along those lines. See this (the whole ABS guide is great).
User avatar
njiska
Posts: 2412
Joined: Sun Jan 10, 2010 8:36 am
Location: Waterloo, On, Canada

Re: BASH Help

Post by njiska »

Cool. That line is pretty much what I thought it was going to be. I'll take a look at the guide a little later.
Look at our friendly members:
MX7 wrote:I'm not a fan of a racist, gun nut brony puking his odious and uninformed arguments over every thread that comes up.
Drum wrote:He's also a pederast. Presumably.
User avatar
njiska
Posts: 2412
Joined: Sun Jan 10, 2010 8:36 am
Location: Waterloo, On, Canada

Re: BASH Help

Post by njiska »

Code: Select all

#!/bin/sh
DNS="208.122.23.22
208.122.23.23"
CURRENT=`networksetup -getdnsservers Wi-Fi` 

if [ "$CURRENT" -eq "$DNS" ]; then
   networksetup -setdnsservers Wi-Fi empty;
else
   networksetup -setdnsservers Wi-Fi 208.122.23.22 208.122.23.23;
fi
Returns:

Code: Select all

./testscript.sh: line 10: [: 208.122.23.22
208.122.23.23: integer expression expected
Any thoughts on why I've ended up with this error? I added a few Echos to the script to confirm the variables were being filled

Code: Select all

echo $DNS
echo "---"
echo $CURRENT 
and it printed

Code: Select all

208.122.23.22 208.122.23.23
---
208.122.23.22 208.122.23.23
Look at our friendly members:
MX7 wrote:I'm not a fan of a racist, gun nut brony puking his odious and uninformed arguments over every thread that comes up.
Drum wrote:He's also a pederast. Presumably.
Barrakketh
Posts: 317
Joined: Sat Jan 22, 2011 10:44 pm

Re: BASH Help

Post by Barrakketh »

njiska wrote:Any thoughts on why I've ended up with this error?
Use "=" instead of "-eq", and make sure there is whitespace around the "=".

I forgot that bash uses a different operator when you're comparing strings.
User avatar
njiska
Posts: 2412
Joined: Sun Jan 10, 2010 8:36 am
Location: Waterloo, On, Canada

Re: BASH Help

Post by njiska »

Brilliant. Script works. Thanks.
Look at our friendly members:
MX7 wrote:I'm not a fan of a racist, gun nut brony puking his odious and uninformed arguments over every thread that comes up.
Drum wrote:He's also a pederast. Presumably.
Post Reply