Scripting: Bash, XML, Python & Plist

msample
Contributor II

I've been successful with several scripts working in correlation with policies, but I've noticed that some don't work as well. Sometimes converting from #!/bin/bash to #!/bin/sh work better, and with others just the reverse. I'm sure some of you out there have experienced this, Seems that XML scripts work best within the Managed Preferences arena, please correct me if I'm wrong here. And I know the MCX is dying, but while it's still in the console I've been making use of it which isn't giving me any trouble.

Thoughts from any other engineers out there? And what are some of your thoughts concerning scripting that works best? Got a lot of tips from Github, but really trying to tie-up some of the loose ends with scripting. I welcome any comments. Thanks all.

14 REPLIES 14

zanb
New Contributor III
but I've noticed that some don't work as well
Sometimes converting from #!/bin/bash to #!/bin/sh work better, and with others just the reverse
Thoughts from any other engineers out there?

My first thought is that I need context and examples so I can give you some feedback.

trying to tie-up some of the loose ends with scripting

Again, it would be great if there was an example so I knew where to start. Thank you.

msample
Contributor II

Thanks, Zan.

I can use the following script as an example, when trying to enable the function of selecting hot corners under 10.9 in bash. I haven't tested this if I changed #!/bin/bash to #!/bin/sh:

#!/bin/bash

# This script enables the bottom-left hot corner and starts the screen saver
defaults write com.apple.dock wvous-bl-corner -int 5

echo "script complete"

exit 0

The second example is a combo script that's not working either:

#!/bin/sh

# This script enables scroll bars always
sudo defaults write NSGlobalDomain AppleShowScrollBars -string “Always”

# This script enables status bar at the bottom of any finder window
sudo defaults write com.apple.finder ShowStatusBar -bool true

echo "script complete"

exit 0

calumhunter
Valued Contributor

sorry maybe i missed it but can you please post, and use the script tags on this forum to preserve the script formatting, a script that works in one shell but not the other?

It is likely that its a syntax issue. Generally all sh scripts will run under bash. but not all bash scripts will run under sh.

Also whats an XML script?

WUSLS
New Contributor

@ msample - Sir, thank you much. I was just about to start looking for the command to do this and just happened to look at this thread see the exact command I was looking for. Thank you for the commands!!

msample
Contributor II

@ WUSLS - You're welcome, Scott. Test each of the scripts and let me know how they work for you. I'm simply testing both of these shells (i.e, bash or sh) to see how well they work, or if I need to re-write them in XML or python. I appreciate you comments.

bentoms
Release Candidate Programs Tester

All my bash scripts have #!/bin/sh

Never had to change it, & this is over 7 years of using the suite.

msample
Contributor II

@ bentoms - Thanks, Ben.

alexjdale
Valued Contributor III

One point: never use sudo inside of a script. Sudo should be used to execute the script if it needs root-level access. Casper will handle that for you when it executes scripts.

chriscollins
Valued Contributor

Keep in mind, that the binaries for both /bin/bash and /bin/sh are actually the same version of bash. So it really doesn't matter what you use.

~ > /bin/sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.
~ > /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.

msample
Contributor II

@ alexjdale - Thanks Alex, my apologies, just a slip of the fingers while scripting. You're correct, sudo should always be dropped.

zanb
New Contributor III

@msample

I believe your scripts may not be working because you have typed "curly" quotation marks in your example post up top. They look like the type of quotes that a word processor uses (also known as "typographer's quotes"). My guess is you're trying to write your BASH scripts in Text Edit or Microsoft Word. I recommend using Text Wrangler or XCode for editing scripts.

Also, I went ahead and took the liberty to write a script for you. It may solve your problem. I wrote this script as a reference because I wanted to programmatically get the user's home folder so I could use "defaults" to write to the full path to the preference file. The "defaults" program can be finicky when writing preferences to memory and to preference files.

I also added a function to write the user preference to any local user account plus the currently logged in user (sans root). This is under the assumption that the script is being launched with "sudo" (as Casper does). If you don't want to set preferences for all users, simply comment out (or delete) the 2nd-to-last line.

Please review and test this script before attempting to run it on a production machine. Thanks.

#!/bin/bash
# Author: Zan Bassi
# Email: zan@zeroonelabs.com
#
# Terms of service: Use at your own risk.
#
# Gets the username of the user currently logged in. 
# Will return "root" if no one is logged in through the Finder.
currentuser="$(ls -l /dev/console | awk '{ print $3 }')"
# Gets the full path of the user home folder. 
# Will return "/var/root" if no user is logged in through the Finder.
curuserhome="$(dscl . read /Users/"${currentuser}" NFSHomeDirectory | awk -F": " '{print $NF}')"

# If there is a user logged in (who is not root), set it for that person.
if [[ ! ${currentuser} = "root" ]];then
  echo "Setting bottom-left hot corner for ${currentuser}."
  defaults write "${curuserhome}"/Library/Preferences/com.apple.dock.plist wvous-bl-corner -int 5
  echo "Setting status bar Finder preference for ${currentuser}."
  defaults write "${curuserhome}"/Library/Preferences/com.apple.finder ShowStatusBar -bool true
fi
defaults write NSGlobalDomain AppleShowScrollBars -string "Always"

_setPrefForAll () {
    # Will return an array of full paths for any local user accounts on the machine.
    # Each array index is separated by a newline character.
    uhomearray=($(dscl . list /Users NFSHomeDirectory | grep -vE "/var|_www|^com" | awk '{print $NF}'))
    # For each index in the array, do:
    for UHOME in ${uhomearray[@]};do
        # defaults write /Users/xxx/Library/...
        echo "Setting bottom-left hot corner for the following user path: ${UHOME}."
        defaults write "${UHOME}"/Library/Preferences/com.apple.dock.plist wvous-bl-corner -int 5
        echo "Setting status bar Finder preference for the following user path: ${UHOME}."
        defaults write "${UHOME}"/Library/Preferences/com.apple.finder ShowStatusBar -bool true
    done
}
_setPrefForAll
exit

msample
Contributor II

Thanks, Zan. I'm testing this out today under 10.9.5 & 10.10.2. You were also correct concerning the quotes, not sure how that happened...but I've made corrections. I'm using TW to write bash scripts and so forth.

zanb
New Contributor III

@msample

Found your problem.

Text Wrangler -> Preferences -> Editor Defaults : Uncheck "Use typographer's quotes"

msample
Contributor II

Done. Great catch, I didn't know that feature was checked by default. I usually just jump right into TW. I've gotta go back a check an additional script that might have been stymied. Thanks, Zan.