Script Help For pulling Current Logged in User and Creating a File

TomDay
Release Candidate Programs Tester

I've written this script and it's not quite working as intended, looking for some help.

Goal is to:
Get current logged in user.
Write a file; /Users/Shared/user_setupTEST.config.

Lone remaining issue:
Script doesn't pass the variable in to the new file, see the line folders: /$CurrentUserHome/Movies

#!/bin/bash

CurrentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

#Current user home folder
CurrentUserHome=(/Users/"$CurrentUser")

cat << 'EOF' > "/Users/Shared/user_setupTEST.config"
[Computers]
desktop_enabled: True
documents_enabled: True
pictures_enabled: True
folders: /$CurrentUserHome/Movies
high_quality_enabled: False
always_show_in_photos: False
# Delete mode can be: ALWAYS_SYNC_DELETES, ASK, NEVER_SYNC_DELETES
delete_mode: NEVER_SYNC_DELETES
[Settings]
autolaunch: True
show_overlays: False
EOF

exit 0
2 REPLIES 2

ryan_ball
Valued Contributor
  1. You are setting the user's home directory as if it was an array rather than a string when you use parentheses. If you really wanted to use that method you could do CurrentUserHome="/Users/$CurrentUser". In my example I'm just getting what their actual home folder is rather than assuming (although typically it would be at /Users/username).
  2. The single quotes around EOF in your cat command is causing you an issue with expanding the variable inside your heredoc, so I've removed them in my example.
#!/bin/bash

loggedInUser=$( echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' )
userHome=$(/usr/bin/dscl . read "/Users/$loggedInUser" NFSHomeDirectory | awk '{print $NF}')

cat << EOF > "/Users/Shared/user_setupTEST.config"
[Computers]
desktop_enabled: True
documents_enabled: True
pictures_enabled: True
folders: $userHome/Movies
high_quality_enabled: False
always_show_in_photos: False
# Delete mode can be: ALWAYS_SYNC_DELETES, ASK, NEVER_SYNC_DELETES
delete_mode: NEVER_SYNC_DELETES
[Settings]
autolaunch: True
show_overlays: False
EOF

exit 0

TomDay
Release Candidate Programs Tester

@ryan.ball This is amazing thanks so much. I beat myself up with this for 2 days before reaching out for some expertise. Appreciate your time it works like a charm, I owe you a beer if we even have another live JNUC.