macOS Command-Line – Mounting Samba Shares Under /Volumes Without GUI

command linemacosmountscriptsmb

I know I can mount Samba share using either mount or open command from the terminal shell. See this related question: How can I mount an SMB share from the command line?. I prefer using open because it mounts the shared folder under /Volumes which seems kind of OS X standard, the expected thing. Namely this happens by typing

open "smb://guest:@${host}/${path_component}"

(where ${host} is the host name or IP address of the Samba share and ${path_component} is the path or "name" of the Samba share).
The result and the main thing is that I can access the stuff from /Volumes/${path_component}.
This is very nice indeed. Just there are two problems or nuisance doing this from within scripts. The first is that this will open a superfluous progress indicator and then Finder window. The second, lesser one as there are scriptable work-arounds, is that the command returns immediately without waiting for the mount to be really available.

Is there a way to (synchronously) mount Samba shares under /Volumes without extraneous windows popping up?

(I am using the latest OS X (Sierra).)

Best Answer

The following is what I use to mount Samba shares via launchd:

/usr/bin/osascript -e "try" -e "mount volume \"smb://guest@${host}\"" -e "end try"

Using osascript's mount means any keychain access needed is done "automagically", there's no progress indicator or Finder window, and the command waits for the mount to be available before proceeding (try it with && echo -n "Done."; mount appended to the end of the above command to test this).