script based on Computer Name

I am trying to run a command based on computer name

This is what I have so far


#!/bin/bash

ComputerName=`scutil --get ComputerName`

if [[ "ComputerName" = MAC ]]

then

echo""

fi


When I run the script dose not run based on contains "MAC" as part of the computer name.

What am I missing in the command? How do I run a script based on if contains?

Posted on Jul 30, 2012 8:31 AM

Reply
10 replies

Jul 30, 2012 12:49 PM in response to Jade Curtis

There's probably an easier way to do some of this, but...


The following has some simple help text, and performs four separate matches; one for the command options, one for the whole hostname string, one for the first three characters of the hostname string, and one that's case-blind . The first hostname match directly uses the response from the scutil command, while the second hostname match uses the hostname string via a symbol. The third hostname match is the first three characters, and case-blind, and that one can be coded a couple of different ways; doing the truncation and the upcasing could easily be all on one line.


#!/bin/bash

if [ "$1" == "-h" ]

then

echo "Tests the current host name"

echo "usage: $0 [option]"

echo " -h display this help text"

exit

fi


if [ `scutil --get ComputerName` = "Host" ]

then

echo "Host"

else

echo "Not Host"

fi


prefix=`scutil --get ComputerName`

if [ "${prefix:0:3}" = "Hos" ]

then

echo "Hos"

else

echo "Not Hos"

fi


prefix=`echo ${prefix} | tr a-z A-Z`

if [ "${prefix:0:3}" = "HOS" ]

then

echo "Hos"

else

echo "Not Hos"

fi


The TLDP folks and some other sites have some bash-related documentation. (An issue with using the generic bash documentation with OS X is that the installed bash version is fairly old.)

Jul 31, 2012 5:56 AM in response to MrHoffman

This is the full script I am working with.


#!/bin/bash

[ "$ComputerName" = "scutil --get ComputerName" ]

if [ "$ComputerName" == "MBP" ]

echo "current MBP name"

then

[ "$Admin" = "Admin02" ]

else

/usr/sbin/dsconfigad -groups "Admin01","$Admin"

echo ""

fi

exit


Where I have this part, It dose not run. Is else in the right place?

[ "$Admin" = "Admin02" ]

else

/usr/sbin/dsconfigad -groups "Admin01","$Admin"

Jul 31, 2012 7:20 AM in response to Jade Curtis

Are you centrally looking to learn how to write and debug a script?


If so, please start with the TLDP link and the introductory bash scripting materials available there and (depending on how much of this you'll be doing) work up to the advanced bash scripting guide that's available there.


From what is posted, the Admin variable isn't being set.


Some of the [ ] syntax around the scutil call and near the first Admin02 reference looks wrong, too. I'd expect that to be an if, and there's a missing fi for that nested block.


And there's a == around, which I'd expect is intended to be a =


And you don't want spaces around the = character.


And the then is on the wrong side of the current MBP name.


And FWIW, the dsconfigad command will likely need to be sudo dsconfigad, or the script invoked as sudo. And I'd not test with -groups, I'd use -show or some such; start with a command that won't make changes to AD if something in the script goes sideways during testing.


If /usr/sbin is in your PATH, then you don't need to specify it.


As for debugging, insert an echo $admin command before that test, and see what's stored there.


To see what's happening with what I've written, extract what I've posted previously, and test with it using echo commands or related.


If you're not looking to learn bash scripting but are rather seeking somebody to write this shell script for you, then you'll want to explain what you want to happen here in a little more detail, and provide a little more background on the environment. From what I can infer from the code you've posted, it looks like you want to perform the AD operation to modify the groups (only) if the user is on a specifically-named system, and apparently also with something involving Admin02 group (or user?), but that latter part is not clear (to me).

Jul 31, 2012 7:36 AM in response to MrHoffman

Thanks for all the helps. I am


What I am looking to do is to

if Computer name contains MBP or MP then run command /usr/sbin/dsconfigad -groups. "Admin01","$Admin" where $Admin veriable matchs the computer name

if MBP then MBPADMIN


I did get this script to work in AppleScript now trying to convert it to bash

This is the AppleScript

set computerName to (do shell script "scutil --get ComputerName")

if (computerName contains "MBP") then

set MBPAdmin to "MBPMACADMIN"

end if

set computerName to do shell script ("sudo dsconfigad -groups ADMIN01," & MBPAdmin) with administrator privileges

Jul 31, 2012 8:04 PM in response to Jade Curtis

That AppleScript probably isn't correct; the MBPAdmin variable can be uninitialized, but still passed into the dsconfigad tool.


Also, tthat dsconfigad command is being executed for all hosts, not just the hosts that have the substring MBP in the host name. That probably isn't intended.


Here's something that's pretty close to what you probably want, and where you could decide to add a couple of substrings into the test; I've tweaked the "nasty" command for testing purposes.


#!/bin/bash

ComputerName=`scutil --get ComputerName | tr a-z A-Z`

case $ComputerName in

*"MBP"* )

echo 'sudo dsconfigad -groups "Admin","MBPMACADMIN" '

;;

*)

;;

esac



Here's a shorter version, with the same tweak for testing.



#!/bin/bash

ComputerName=`scutil --get ComputerName | tr a-z A-Z`

if [[ "$ComputerName" == *"MBP"* ]]

then

echo 'sudo dsconfigad -groups "Admin","MBPMACADMIN" '

fi


There are probably a few (more) tweaks...

Aug 1, 2012 1:09 PM in response to Jade Curtis

The case-statement version of the shell script can be modified to do what you are now asking for.


The following update to the case-based script was composed in the editor window; I've not verified this code:


#!/bin/bash

ComputerName=`scutil --get ComputerName | tr a-z A-Z`

case $ComputerName in

*"MBP"* )

echo 'sudo dsconfigad -groups "Admin","THIS" '

;;

*"MP"* )

echo 'sudo dsconfigad -groups "Admin","THAT" '

;;

*)

;;

esac


If you're not in position to do some reading here, then I'd suggest engaging somebody that can discuss your particular requirements with you, and that can provide you with some bash coding assistance; that'll be the fastest way for you to get the application you need, and (hopefully) without also unduly perturbing the AD environment.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

script based on Computer Name

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