sed scripting experts?

Contributor III

I want to create a script that will include the ability to convert a Windows unc path to something usable for the Mac. I'm only working on the conversation portion right now and have put the following together by finding some examples online.

echo '\server.domain.eduhome$mmouse' | sed -e 's/\///g'

This works for the moment but then I'd like to also add on the "smb:" portion. I thought I could use command substitution for this to keep it all on one line but I'm running into an error.

echo smb:`echo '\server.domain.eduhome$mmouse' | sed -e 's/\///g'`
sed: 1: "s////g
": unescaped newline inside substitute pattern

If I try the following, it somewhat works but drops a "/".

echo smb:`echo '\server.domain.eduhome$mmouse'` | sed -e 's/\///g'

In a script I figure I could always do the conversion and add the "smb:" on a separate line but I'm curious how to make it happen in one line. Can anyone explain what I'm doing wrong assuming this is possible?


Legendary Contributor III

Just curious, but what is the end goal with the script? If it's to allow end users to click on Windows style links to open them on their Mac, there is already a service built into the OS services that let's them do that by right/control clicking on the link and choosing Services > Open URL. Just in case that's what this is all about. If not, maybe you can elaborate on what you're looking to do.

If that isn't the case, the following code should work:

echo '\server.domain.eduhome$mmouse' | sed 's/^/smb:/;s/\///g'

Edited with a better more reliable version. The single quotes allow sed to replace both 's with //

Contributor III

Thanks! I wasn't aware of that feature so that's good to know.

So my grand plan for this is actually seeing if I could create my own version of a login script of sorts for mapping end user network drives. In our environment there are other guys who handle the file share access for everyone and configure drive mappings for Windows users through group policies. Sometimes they let me know that I need to set something up for the Mac users I support and sometimes they go directly to the user to do it manually. It gets irritating because it ends up creating inconsistencies and the miscommunication doesn't help(example...system gets rebuilt and user wants to know where their network drive is). Since they don't use Casper, I thought maybe I could create something where the Windows admins could simply place the share name paths in a text file(csv) on a file share where each file is configured with permissions for that particular user or group that they belong to(AD environment). The share would have to be wide open so I figured the custom permissions on each file would keep anyone nosy out of them. I mention "each file" because I figured I would also read what AD groups they are members of and access a similar file if one exists that would be associated with file shares for each group. I work at a community college so there might be file shares based on specific users and then depts people are in.

On the Mac side of things I was looking at creating a launchagent that would kick off a script on login to access the windows share I just mentioned and grab the share name path(s) out of that user's file(s) on the server that would be in the Windows unc format that the Windows admins are use to. The script would convert it and kick it off then unmount that file server that had the drive mapping data.

There might be something I'm missing with it but on the surface it seems like it should work. Part of this also includes using mysides to put the mounts into the favorites directory in the Finder. They decided to use a Windows feature called enumeration on some of the shares with the idea to hide folders from users that they don't have permission to. I have to use mysides to setup that favorite item or the user can't access their data through the finder or any application. Enumeration works great for Windows systems but it's a little wonky trying to make it work with Macs.

All in all I'm trying to create something to make it more simple and reliable for the Windows admins so that it's more consistent for the Mac users. The way I see it is if I can make this work then they could even have the option of using this on the Windows side with a simple powershell script kicked off with a login script. Maybe I can get cats and dogs to live together for a change.

That's pretty much it in nutshell. The data being in a csv file is for having one field being used for documentation of what the share is for. Some of them have had trouble with documenting this. This won't enforce it but if there's a field right there that's empty, it might help them remember to fill it in with a description.

If you have any insight on this or simply want to say "you're nuts", feel free. lol

Legendary Contributor III

Yes, there are ways of doing this. Have you looked at Ben Toms' write up on how he was doing it from some years back? - It may still be relevant, but I'm not certain. Worth a look anyway.

Also search around on here because I'm sure there are some other threads on this very same topic, and there may already be some ready made solutions on there for you to look at.

Edit: OTOH, maybe it's not so much the AD group membership that you're after, but the SMBHome item from the AD record? If a user has a home share defined in their AD record, you can grab that with dscl. Either by looking at their local cached AD mobile account, or by searching against Active Directory itself. The following code should print out their home share, assuming those are actually set up. It wasn't clear from your description if they are or not. I assume they are, but you tell me.

dscl "/Active Directory/DOMAIN/All Domains" read /Users/username SMBHome | awk '{print $NF}'

Replace the "DOMAIN" with your AD domain name, and the "username" with a username or grab it from the logged in username. It should print out the sharename in the Windows sharenamehome style. But with the above sed command you can convert it to a Mac friendly address.

Honored Contributor

Python example:

>>> string = '\\home$\mmouse'
>>> new_string = string.replace('\','/')
>>> print new_string
>>> print "smb:" + new_string

is a literal so you have to escape it by adding an additional one in the original string in Python.

Contributor III

For something like this which only requires a single pattern substitution, you don't really need to use sed. Instead, just use Bash's built-in pattern substitution mechanism:

# Single quotes are important here

# Result: \server.domain.eduhome$mmouse
echo "${unc_path}"

# Result: smb://$/mmouse
echo "smb:${unc_path//\//}"