EA scripting query

macmanmk
Contributor

Is an EA script able to output multiple lines of output? For instance, we want to display two attributes out of a plist file at /Users/loggedInUser/Library/Preferences/com.dms.echo.plist

The plist file looks like this...

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>NSNavPanelExpandedSizeForOpenMode</key> <string>{712, 448}</string> <key>NSStatusItem Preferred Position Item-0</key> <real>881</real> <key>echoDays</key> <integer>30</integer> <key>echoing</key> <true/> <key>dmsEchoHasBeenLaunched</key> <true/> <key>supportLogging</key> <true/> </dict> </plist>

We really only want the "echoDays" and "echoing" attributes displayed. Can a script format the output so those two attributes appear one line over the other?

1 ACCEPTED SOLUTION

rderewianko
Valued Contributor II

Okay, i took a stab at this
The first two are single result EA's the third combines them

#!/bin/bash
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
Echoing=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoing)

#if that result is blank then
    if [ -z "$Echoing" ]; then
    echo "<result>No Echoing</result>"
    else 
#if its not blank print the result  
    echo "<result>$Echoing</result>"
    fi
exit 0
#!/bin/bash
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
echoDays=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoDays)


#if that result is blank then
    if [ -z "$echoDays" ]; then
    echo "<result>No echoDays</result>"
    else 
#if its not blank print the result  
    echo "<result>$echoDays</result>"
    fi
exit 0
#!/bin/bash
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
Echoing=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoing)

#if that result is blank then
    if [ -z "$Echoing" ]; then
    echoing_Result="no Echoing"
    else 
#if its not blank print the result  
    echoing_Result=$Echoing
    fi

echoDays=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoDays)
#if that result is blank then
    if [ -z "$echoDays" ]; then
    echoDays_Result="no echoDays"
    else 
#if its not blank print the result  
    echoDays_Result=$echoDays
    fi


echo "<result> $echoing_Result & $echoDays_Result </result>"
exit 0

View solution in original post

5 REPLIES 5

rderewianko
Valued Contributor II

Okay, i took a stab at this
The first two are single result EA's the third combines them

#!/bin/bash
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
Echoing=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoing)

#if that result is blank then
    if [ -z "$Echoing" ]; then
    echo "<result>No Echoing</result>"
    else 
#if its not blank print the result  
    echo "<result>$Echoing</result>"
    fi
exit 0
#!/bin/bash
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
echoDays=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoDays)


#if that result is blank then
    if [ -z "$echoDays" ]; then
    echo "<result>No echoDays</result>"
    else 
#if its not blank print the result  
    echo "<result>$echoDays</result>"
    fi
exit 0
#!/bin/bash
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
Echoing=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoing)

#if that result is blank then
    if [ -z "$Echoing" ]; then
    echoing_Result="no Echoing"
    else 
#if its not blank print the result  
    echoing_Result=$Echoing
    fi

echoDays=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoDays)
#if that result is blank then
    if [ -z "$echoDays" ]; then
    echoDays_Result="no echoDays"
    else 
#if its not blank print the result  
    echoDays_Result=$echoDays
    fi


echo "<result> $echoing_Result & $echoDays_Result </result>"
exit 0

boberito
Valued Contributor

Use printf it won't print a new line like echo.

So you could do

printf "<result>" then printf whatever you want, multiple values, multiple printfs. Then printf "</result>"

But the above solution works too.

macmanmk
Contributor

Works great. Thanks guys!

mm2270
Legendary Contributor III

I typically drop any items into a bash array in the script, and then use printf to print the array, splitting on line breaks.

#!/bin/bash

loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')

if [ -e "/Users/$loggedInUser/Library/Preferences/com.dms.echo.plist" ]; then
     echoDaysVal=$(defaults read /Users/$loggedInUser/Library/Preferences/com.dms.echo.plist echoDays)
     echoingVal=$(defaults read /Users/$loggedInUser/Desktop/com.dms.echo.plist echoing)

     results=("echoDays: ${echoDaysVal}" "echoing: ${echoingVal}")

     echo "<result>$(printf '%s
' "${results[@]}")</result>"
else
     echo "<result>No results</result>"
fi

That will or should show 2 lines for the results. What you need to keep in mind is that not all areas of the Jamf UI are able to show multiple lines for EAs or other items, so your results here may vary. in some places it may show up as one line, not two. Jamf also has an odd track record of enabling, then breaking this functionality. Some versions may not display multiple lines and others might.

rderewianko
Valued Contributor II

I've never used arrays but now you've got me interested @mm2270