How do I get the name of the active user via the command line in OS X?

前端 未结 12 1594
耶瑟儿~
耶瑟儿~ 2020-12-13 05:27

How do I get the name of the active user via the command line in OS X?

12条回答
  •  佛祖请我去吃肉
    2020-12-13 06:12

    The question has not been completely answered, IMHO. I will try to explain: I have a crontab entry that schedules a bash shell command procedure, that in turn does some cleanup of my files; and, when done, sends a notification to me using the OS X notification center (with the command osascript -e 'display notification ...). If someone (e.g. my wife or my daughter) switches the current user of the computer to her, leaving me in the background, the cron script fails when sending the notification.

    So, Who is the current user means Has some other people become the effective user leaving me in the background? Do stat -f "%Su" /dev/console returns the current active user name?

    The answer is yes; so, now my crontab shell script has been modified in the following way:

    ...
    if [ "$(/usr/bin/stat -f ""%Su"" /dev/console)" = "loreti" ]
    then /usr/bin/osascript -e \
      'display notification "Cleanup done" sound name "sosumi" with title "myCleanup"'
    fi
    

提交回复
热议问题