Skip to content

Instantly share code, notes, and snippets.

@pmbuko
Last active August 29, 2015 14:27
Show Gist options
  • Save pmbuko/445c56e5d63ec332eb98 to your computer and use it in GitHub Desktop.
Save pmbuko/445c56e5d63ec332eb98 to your computer and use it in GitHub Desktop.

Revisions

  1. pmbuko revised this gist Aug 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/bash

    # This script will change the hostname of a Mac to the full name of the
    # most-commonly-logged-in user with a local home found under /Users.
    # This script will change the hostname of a Mac to a dot-separated version
    # of the full name of the most-commonly-logged-in user with a local home.

    # Give a list of usernames to ignore, separated by spaces
    ignore_list=( root admin administrator bukowinskip )
  2. pmbuko revised this gist Aug 13, 2015. 1 changed file with 16 additions and 17 deletions.
    33 changes: 16 additions & 17 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # most-commonly-logged-in user with a local home found under /Users.

    # Give a list of usernames to ignore, separated by spaces
    ignore_list=( root admin administrator )
    ignore_list=( root admin administrator bukowinskip )

    # Get a list of usernames sorted by login frequency, limited to the last 100 logins
    login_list=$(last -t console -100 | \
    @@ -13,29 +13,28 @@ login_list=$(last -t console -100 | \

    # Simple function to check if user is in ignore list
    ignore_match () {
    local e
    for e in "${ignore_list[@]}"; do [[ "$e" == "$1" ]] && return 0; done
    local i
    for i in "${ignore_list[@]}"; do [[ "$i" == "$1" ]] && return 0; done
    return 1
    }

    # This block will run through login_list and operate on only the first username in the list
    # that happens to have a local home.
    for u in $login_list; do
    # proceed only if user shouldn't be ignored
    if ! ignore_match "$u"; then
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')
    # Proceed if user is not in ignore_list and has a local home
    if ! ignore_match "$u" && [ -d "/Users/${u}" ]; then
    # Get user's full name, substituting periods for spaces
    dotted_name=$(dscl -plist . read "/Users/$u" RealName | \
    awk 'BEGIN { OFS="." } /string/{gsub("<.?string>","");$1 = $1;print}')

    # Now set the hostnames
    echo "This script would have changed the compter name to '$dotted_name'"
    echo "Uncomment the three 'scutil' lines in the script to make it actually change it."
    #scutil --set ComputerName $dotted_name
    #scutil --set LocalHostName $dotted_name
    #scutil --set HostName $dotted_name
    # Set the hostnames (comment echo lines and uncomment scutil lines after testing)
    echo "This script would have changed the compter name to '$dotted_name'"
    echo "Uncomment the three 'scutil' lines in the script to make it actually change it."
    #scutil --set ComputerName $dotted_name
    #scutil --set LocalHostName $dotted_name
    #scutil --set HostName $dotted_name

    # skip all further names in login_list
    break
    fi
    # skip all further names in login_list
    break
    fi
    done
  3. pmbuko revised this gist Aug 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ ignore_match () {
    # that happens to have a local home.
    for u in $login_list; do
    # proceed only if user shouldn't be ignored
    if [ ! $(ignore_match "$u") ]; then
    if ! ignore_match "$u"; then
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')
  4. pmbuko revised this gist Aug 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ login_list=$(last -t console -100 | \
    sort | uniq -c | sort -r | awk '{print $2}')

    # Simple function to check if user is in ignore list
    ignore_user () {
    ignore_match () {
    local e
    for e in "${ignore_list[@]}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
    @@ -22,7 +22,7 @@ ignore_user () {
    # that happens to have a local home.
    for u in $login_list; do
    # proceed only if user shouldn't be ignored
    if [ ! $(ignore_user "$u") ]; then
    if [ ! $(ignore_match "$u") ]; then
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')
  5. pmbuko revised this gist Aug 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ login_list=$(last -t console -100 | \
    # Simple function to check if user is in ignore list
    ignore_user () {
    local e
    for e in "${ignoreUsers[@]}"; do [[ "$e" == "$1" ]] && return 0; done
    for e in "${ignore_list[@]}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
    }

  6. pmbuko revised this gist Aug 13, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -3,26 +3,26 @@
    # This script will change the hostname of a Mac to the full name of the
    # most-commonly-logged-in user with a local home found under /Users.

    # Give a list of usernames to ignore (leave a space between each username)
    ignore_list=( root administrator )
    # Give a list of usernames to ignore, separated by spaces
    ignore_list=( root admin administrator )

    # Get a list of usernames sorted by login frequency, limited to the last 100 logins
    login_list=$(last -t console -100 | \
    awk '/console/{print $1}' | \
    sort | uniq -c | sort -r | awk '{print $2}')

    # Simple function to check if item is in an array
    # Simple function to check if user is in ignore list
    ignore_user () {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    for e in "${ignoreUsers[@]}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
    }

    # This block will run through login_list and operate on only the first username in the list
    # that happens to have a local home.
    for u in $login_list; do
    # proceed only if user is not in ignore list
    if [ ! $(ignore_user "$u" "${ignoreUsers[@]}") ]; then
    # proceed only if user shouldn't be ignored
    if [ ! $(ignore_user "$u") ]; then
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')
  7. pmbuko revised this gist Aug 13, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ login_list=$(last -t console -100 | \
    sort | uniq -c | sort -r | awk '{print $2}')

    # Simple function to check if item is in an array
    shouldIgnore () {
    ignore_user () {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
    @@ -22,11 +22,11 @@ shouldIgnore () {
    # that happens to have a local home.
    for u in $login_list; do
    # proceed only if user is not in ignore list
    if [ ! shouldIgnore "$u" "${ignoreUsers[@]}"]; then
    if [ ! $(ignore_user "$u" "${ignoreUsers[@]}") ]; then
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')

    # Now set the hostnames
    echo "This script would have changed the compter name to '$dotted_name'"
    echo "Uncomment the three 'scutil' lines in the script to make it actually change it."
  8. pmbuko revised this gist Aug 13, 2015. 1 changed file with 25 additions and 12 deletions.
    37 changes: 25 additions & 12 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -3,26 +3,39 @@
    # This script will change the hostname of a Mac to the full name of the
    # most-commonly-logged-in user with a local home found under /Users.

    # Give a list of usernames to ignore (leave a space between each username)
    ignore_list=( root administrator )

    # Get a list of usernames sorted by login frequency, limited to the last 100 logins
    login_list=$(last -t console -100 | \
    awk '/console/ && !/root/{print $1}' | \
    awk '/console/{print $1}' | \
    sort | uniq -c | sort -r | awk '{print $2}')

    # Simple function to check if item is in an array
    shouldIgnore () {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
    }

    # This block will run through login_list and operate on only the first username in the list
    # that happens to have a local home.
    for u in $login_list; do
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')
    # proceed only if user is not in ignore list
    if [ ! shouldIgnore "$u" "${ignoreUsers[@]}"]; then
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')

    # Now set the hostnames
    echo "This script would have changed the compter name to '$dotted_name'"
    echo "Uncomment the three 'scutil' lines in the script to make it actually change it."
    #scutil --set ComputerName $dotted_name
    #scutil --set LocalHostName $dotted_name
    #scutil --set HostName $dotted_name
    # Now set the hostnames
    echo "This script would have changed the compter name to '$dotted_name'"
    echo "Uncomment the three 'scutil' lines in the script to make it actually change it."
    #scutil --set ComputerName $dotted_name
    #scutil --set LocalHostName $dotted_name
    #scutil --set HostName $dotted_name

    # skip all further names in login_list
    break
    # skip all further names in login_list
    break
    fi
    fi
    done
  9. pmbuko revised this gist Aug 12, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@ login_list=$(last -t console -100 | \
    awk '/console/ && !/root/{print $1}' | \
    sort | uniq -c | sort -r | awk '{print $2}')


    # This block will run through login_list and operate on only the first username in the list
    # that happens to have a local home.
    for u in $login_list; do
  10. pmbuko revised this gist Aug 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # This script will change the hostname of a Mac to the full name of the
    # most-commonly-logged-in user with a local home found under /Users.

    # Get a list of usernames sorted by frequency, limited to the last 100 logins
    # Get a list of usernames sorted by login frequency, limited to the last 100 logins
    login_list=$(last -t console -100 | \
    awk '/console/ && !/root/{print $1}' | \
    sort | uniq -c | sort -r | awk '{print $2}')
  11. pmbuko revised this gist Aug 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ login_list=$(last -t console -100 | \
    sort | uniq -c | sort -r | awk '{print $2}')


    # This block will run through login_list and operate on only the first name in the list
    # This block will run through login_list and operate on only the first username in the list
    # that happens to have a local home.
    for u in $login_list; do
    # Proceed only if the user has a local home in the expected location
  12. pmbuko created this gist Aug 12, 2015.
    29 changes: 29 additions & 0 deletions rename_mac_to_primary_local_user.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/bin/bash

    # This script will change the hostname of a Mac to the full name of the
    # most-commonly-logged-in user with a local home found under /Users.

    # Get a list of usernames sorted by frequency, limited to the last 100 logins
    login_list=$(last -t console -100 | \
    awk '/console/ && !/root/{print $1}' | \
    sort | uniq -c | sort -r | awk '{print $2}')


    # This block will run through login_list and operate on only the first name in the list
    # that happens to have a local home.
    for u in $login_list; do
    # Proceed only if the user has a local home in the expected location
    if [ -d "/Users/${u}" ]; then
    dotted_name=$(dscl . read "/Users/$u" RealName | awk 'BEGIN { OFS="." } !/:/{$1 = $1;print}')

    # Now set the hostnames
    echo "This script would have changed the compter name to '$dotted_name'"
    echo "Uncomment the three 'scutil' lines in the script to make it actually change it."
    #scutil --set ComputerName $dotted_name
    #scutil --set LocalHostName $dotted_name
    #scutil --set HostName $dotted_name

    # skip all further names in login_list
    break
    fi
    done