-
Notifications
You must be signed in to change notification settings - Fork 0
/
swuser
executable file
·74 lines (67 loc) · 1.94 KB
/
swuser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
#
# swuser - Switch users via Mac OS X's Fast User Switching
#
# Originally via Schoun Reagan, but later
# http://www.macosxhints.com/article.php?story=20031102031045417
#
# v0.1 2006-10-18 - Morgan Aldridge <[email protected]>
# Initial release
# v0.2 2010-12-27 - Morgan Aldridge
# Renamed to 'swuser', added usage instructions, options
# for switching to login window, user by name, or user by ID.
#
# global variables
user=$(whoami)
id=$(id -u $user)
cgsession="/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"
# print usage instructions (help)
function usage() {
printf "Usage: swuser [options [username|useried]]\n"
printf " -h print these usage instructions\n"
printf " -l switch to the login window\n"
printf " -n switch to a different user by user name\n"
printf " -u switch to a different user by user id\n"
}
function check_id() {
if (( $1 == $2 )); then
printf "Sorry, you can't switch to yourself.\n"
exit 1;
fi
}
# exit with an error if being run from screen
if [ "$STY" != "" ]; then
printf "Sorry, swuser doesn't work from within screen. I know, I hate it too!\n"
exit 1;
fi
# see if any arguments were passed in
if [ $# -gt 0 ]; then
# if so, step through them all and process them
while [ $# -gt 0 ]; do
# see if the user requested help
if [ "$1" = "-h" ]; then
usage
exit;
# handle switch to login window
elif [ "$1" = "-l" ]; then
"$cgsession" -suspend
shift
# handle switching to another user by name
elif [ "$1" = "-n" ]; then
shift
new_id=$(id -u "$1")
check_id $id $new_id
"$cgsession" -switchToUserID $(id -u "$1")
shift
# handle switch to another user by id
elif [ "$1" = "-u" ]; then
shift
check_id $id $1
"$cgsession" -switchToUserID $1
shift
fi
done
else
printf "No switch user action specified.\n\n"
usage
fi