Interrogating the JSS with bash script - HELP!

franton
Valued Contributor III

Hi,

What i'm trying to do is to run a query in our firstrun script, on the JSS for the Mac it's currently running on. All it's trying to do is to see if the department and building settings are correctly configured. (Our JSS occasionally doesn't configure these fields properly via prestage imaging.) If they are, it skips a section of code that would give a menu choice. Here's what i've got so far (in a highly simplified version):

#!/bin/bash

JSSUSR="x"
JSSPWD="x"
JSSURL=$( /usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url )
JSSPATH="JSSResource/computers/macaddress"
MACMAC=$( networksetup -getmacaddress en0 | awk '{print $3}' | sed 's/:/./g' )
BUILDING=$( curl -s -u $JSSUSR:$JSSPWD $JSSURL$JSSPATH/$MACMAC | xpath //computer/location/building/text | sed -e 's/<building>//;s/</building>//' )
DEPT=$( curl -s -u $JSSUSR:$JSSPWD $JSSURL$JSSPATH/$MACMAC | xpath //computer/location/department/text | sed -e 's/<department>//;s/</department>//' )

if [[ $BUILDING == "" || $DEPT == "" ]] ;
then
  *code here*
fi

I've redacted our api readonly account name and password for security.

The idea is that if either or both of the two fields are blank, the script in the if statement should run. If not, it skips and gets on with the rest of the firstrun script.

What actually seems to happen is that the code in the if statement executes regardless! That's not such a bad flaw at this stage, but it's something i'd like to correct. Any thoughts?

1 ACCEPTED SOLUTION

jarednichols
Honored Contributor
6 REPLIES 6

jarednichols
Honored Contributor

I think you may want to instead do

if [ -z $BUILDING]

In the database, for that record, if the building or department aren't set, they should be NULL. You can verify this by looking in your database with read-only access and MySQL Workbench. You want to check to see if $BUILDING is NULL, not if it's empty. There's a difference.

franton
Valued Contributor III

That's pretty cool. Is there a way to check if either or both of them is NULL?

e.g.

if [ -z $BUILDING ] || [ -z $DEPT}

jarednichols
Honored Contributor

Sure can. Except I don't think you want a curly brace at the end there. That'll trip you up for sure.

tlarkin
Honored Contributor

Double ampersands is the answer here if you want to test two things in a if then statement

if [[ -z var1 ]] && [[ -z var2 ]]

then unix stuff happens here
else we stop or do other Unix stuff

fi

jarednichols
Honored Contributor

franton
Valued Contributor III

We ended up giving up and not using the check after we abandoned all use of prestage imaging configuration. Thanks anyway!