8 Replies Latest reply: Aug 28, 2012 10:18 AM by Clif H
Clif H Level 1 Level 1 (115 points)

Trying to convert my existing shell script to AppleScript to configure our Macs to Active Directory. Stuck on the following, which appears to be a case of escaping escape characters. Thoroughly lost. Anyone with experience in this? If I remove the space between the double-backslash and escaped quotations to follow, the script fails. Eliminating that space is just what I need to do to get the correct syntax.

 

set domain to "MYDOMAIN"

set admins to "MYADMINS"

 

do shell script "dsconfigad -groups " & domain & "\"\\ \"" & admins with administrator privileges

 

Thanks in advance for any advice here. Feel like this must be something obvious I a missing here.

 

Cliff

  • twtwtw Level 5 Level 5 (4,900 points)

    for the moment, replace the do shell script with display dialog, like so:

     

    display dialog "dsconfigad -groups " & domain & "\"\\ \"" & admins


    that will show you precisely what command you're trying to run - most likely you have a quoting issue.  If you can't see the error, post back.

  • Clif H Level 1 Level 1 (115 points)

    Thanks. That's a good way at visualizing the result I had not considered. Definitely a quoting issue, which I had seen as well in viewing script events/replies. Unfortunately the result still leaves me puzzled on logistics of how to both escape backslash and ensure proper quoting, since removing the quotes kills ability to escape the the backslash. Seems like a catch 22.

     

    Here's result of above:

     

    dsconfigad -groups MYDOMAIN"\ "MYADMINS

     

    If it helps, absent use of the variables, I have no problem with the following:

     

    do shell script "dsconfigad -groups \"MYDOMAIN\\deptAdmins\"" with administrator privileges

  • Clif H Level 1 Level 1 (115 points)

    Should also note that I started at trying to define a variable that combined the other two variables, delimited by that backslash. Still hit a wall due to need to escape the backslash:

     

    set domain to "MYDOMAIN"

    set admins to "MYADMINS"

    set admingroups to domain & "\"\\ \"" & admins

     

    Result:

    MYDOMAIN\"\\ \"MYADMINS

  • twtwtw Level 5 Level 5 (4,900 points)

    try:

     

    do shell script "dsconfigad -groups " & quoted form of (domain & "\\" & admins) with administrator privileges

     

    That won't work if there are any wildcard expansions (quoted form of adds single quotes).

     

    As a rule, you only need to escape two characters in applescript: " and \ (escaped as \" and \\).  it can get confusing if you look in the events log, though, because the editor displays the code with the escapes.

  • twtwtw Level 5 Level 5 (4,900 points)

    second thought, you might need to use the following:

     

    do shell script "dsconfigad -groups " & quoted form of domain & "\\" & quoted form of admins with administrator privileges


    I'm not sure exactly how the quoting needs to be done.

  • Clif H Level 1 Level 1 (115 points)

    No dice. That gets the correct backslash in (good), but appears to be failing due to the need for the entire do shell script needing to be encapsulated in quotes(assuming).

     

    Events/Results:

     

    tell current application

      do shell script "dsconfigad -groups 'MYDOMAIN'\\'MYADMINS'" with administrator privileges

      --> error "/bin/sh: -c: line 0: unexpected EOF while looking for matching `''

    /bin/sh: -c: line 1: syntax error: unexpected end of file" number 2

    Result:

    error "/bin/sh: -c: line 0: unexpected EOF while looking for matching `''

    /bin/sh: -c: line 1: syntax error: unexpected end of file" number 2

  • Camelot Level 8 Level 8 (46,395 points)

    You may be over-thinking this, especially if you're looking at the result in your Script Editor.

     

    If a text object contains a \ then the script editor will add a leading backslash when it displays the result, but that doesn't mean it's there - it's only added for display purposes.

     

    Your script should work fine with the right quoting:

     

    do shell script "dsconfigad -groups 'MYDOMAIN\\MYADMINS'" with administrator privileges


    Note the single quotes around the entire domain specifier. If using variables, just substitute in the appropriate places:

     

    do shell script "dsconfigad -groups '" & domainName & "\\" & groupName & "'" with administrator privileges

     

    (Note where the single quotes are)

  • Clif H Level 1 Level 1 (115 points)

    Thanks so much. I was quite sure I was overthinking the script and that was a big reason why I posted back here for 2nd/3rd perspectives. Got hung up on assumption I had to wrap end of do shell script with a double quote, since the first example you have above is where I started from. Need to do more homework on how and when to use double vs single quoting, as that was an angle I completely overlooked. Thanks again.