Scripting help (or am I going mad..)

achmelvic
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.

9b59ada744654259b34f1e6a52297852

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?

8 REPLIES 8

CypherCookie
Contributor

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.

achmelvic
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.

jssmith
New Contributor III

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

jssmith
New Contributor III

or

bash /path/to/file

millersc
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.

merps
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:

#!/bin/bash

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 '
'

gregory_floro
New Contributor

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

achmelvic
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.