Scripting help (or am I going mad..)

New Contributor III

I'm working upon a larger script to do some finder customisation and having a problem with a dscl lookup which is quite confusing unless I'm missing something silly!

All I'm wanting this small bit to do it echo out the path to the logged in active directory users network home directory, SMBHome.

Here's the really basic script

#!/bin/bash CurrentUser=$(stat -f%Su /dev/console | awk '!/root/') ADHome=$(dscl . -read /Users/"$CurrentUser" SMBHome) echo "$ADHome"

If I run that in terminal the result it outputs is different to what happens running the exact same commands manually. See the screen shot where I've run the script and then copied the contents directly into terminal and it gives a different answer.


As you can see in the script result it misses the first and then a from the path, I assuming I'm missing something but not sure what. Any one any ideas?



That works just fine for me for both ways of executing it. I'm invoking the script via the same method as you as well.

New Contributor III

Thanks @CypherCookie

So I'm not necessary going mad, there is something weird going on, I'll give it a try on another mac later on.

New Contributor III

When executing the script, try doing it without "sh" Just use the path to the file.

New Contributor III


bash /path/to/file

Valued Contributor

Doing it with the "sh" is removing the slash. Doing with just the path, gives the correct response. JAMF would run it without the "sh" since it's running as root.

Contributor III

I've been messing with this for a while and I think your variable is fine, and it's just the combination of echo and bash that is chopping up your output.

If you want to check on the results, try something like this:


CurrentUser=$(stat -f%Su /dev/console | awk '!/root/')
echo $CurrentUser
ADHome=$(dscl . -read /Users/"$CurrentUser" SMBHome)
justHome=$(dscl . -read /Users/"$CurrentUser" SMBHome | awk '{print $2}')
echo "ADHome Returns:"
echo ${ADHome}
echo "$ADHome"
echo $ADHome
printf '%s' $ADHome
printf '
echo "justHome Returns:"
echo ${justHome}
echo "$justHome"
echo $justHome
printf '%s' $justHome
printf '

New Contributor

Have you tried updating your script to include the "/" between "$CurrentUser"/SMBHome?
ADHome=$(dscl . -read /Users/"$CurrentUser"/SMBHome)

New Contributor III

After a few weeks off just got back to looking at this, thanks for all the suggestions guys!

Thanks to @merps great test script it does look to work with the printf command rather than echo. The command which produces the right format is the one at the bottom:

printf '%s' $justHome
printf ' '

Now to combine it with the larger script for adding a users network home to their Finder sidebar.

Thanks again for all the help.