Skip to main content
Question

iCloud Username

  • May 22, 2020
  • 4 replies
  • 41 views

Forum|alt.badge.img+3

I am using the following script as a EA to pull up the users iCloud account name, this works fine but if the machine hasn't updated inventory, or, if the user hasn't created a iCloud account then its just blank, I would like the script to report None if the iCloud account doesn't exist, could anyone modify the script to accommodate this ?

#!/bin/bash for user in $(ls /Users/ | grep -v Shared); do if [ -d "/Users/$user/Library/Application Support/iCloud/Accounts" ]; then Accts=$(find "/Users/$user/Library/Application Support/iCloud/Accounts" | grep '@' | awk -F'/' '{print $NF}') iCloudAccts+=(${user}: ${Accts}) fi done echo "<result>$(printf '%s ' "${iCloudAccts[@]}")</result>"

4 replies

DBrowning
Forum|alt.badge.img+24
  • Esteemed Contributor
  • May 22, 2020

@gary.owen Try this:

#!/bin/bash

for user in $(ls /Users/ | grep -v Shared); do
    if [ -d "/Users/$user/Library/Application Support/iCloud/Accounts" ]; then
        Accts=$(find "/Users/$user/Library/Application Support/iCloud/Accounts" | grep '@' | awk -F'/' '{print $NF}')
        iCloudAccts+=(${user}: ${Accts})
    else
        Accts="None"
        iCloudAccts+=(${user}: ${Accts})
    fi
done

echo "<result>$(printf '%s
' "${iCloudAccts[@]}")</result>"

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • May 22, 2020

Thats great, Thanks


Forum|alt.badge.img+5
  • Contributor
  • April 5, 2024

I tried to run this on a 2021 M1Pro Sonoma 14.4.1 and received these results: 

Last login: Fri Apr  5 10:51:22 on ttys000

robert.buss@W2LFT4NV52 ~ % #!/bin/bash

 

for user in $(ls /Users/ | grep -v Shared); do

    if [ -d "/Users/$user/Library/Application Support/iCloud/Accounts" ]; then

        Accts=$(find "/Users/$user/Library/Application Support/iCloud/Accounts" | grep '@' | awk -F'/' '{print $NF}')

        iCloudAccts+=(${user}: ${Accts})

    else

        Accts="None"

        iCloudAccts+=(${user}: ${Accts})

    fi

done

 

echo "<result>$(printf '%s

' "${iCloudAccts[@]}")</result>"

zsh: event not found: /bin/bash

robert.buss@W2LFT4NV52 ~ %

How do I correct this so it runs? 

 


Forum|alt.badge.img
  • New Contributor
  • April 30, 2024

You need to put it into a file (for example, icloud.sh) and then make that file executable with a `chmod u+x icloud.sh`