Repository navigation
Expand file tree
/
Copy pathuserman
More file actions
160 lines (137 loc) · 3.71 KB
/
Copy pathuserman
File metadata and controls
160 lines (137 loc) · 3.71 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# No globbing
set -f
# Absolute paths to executables - Specify the versions we want to use. We don't want to use whatever is in ${PATH} (as specified in the assignment)
CAT=/usr/bin/cat
CLEAR=/usr/bin/clear
CUT=/usr/bin/cut
GREP=/usr/bin/grep
ID=/usr/bin/id
PRINTF=/usr/bin/printf
# Use the built in for exit
EXIT="command exit"
# Checks for the existence of a user. Exits this script if the user doesn't exist
function checkuser() {
if ! "$ID" -u "$1" > /dev/null 2>&1; then
$PRINTF "\nUser ${1} doesn't exist.\n"
$EXIT 3
fi
}
function showuserid() {
local USERID
if [ "${1}" ]; then
checkuser "$1"
USERID=$($CAT /etc/passwd 2>/dev/null | $GREP "$1" 2>/dev/null | $CUT -d':' -f 3 2>/dev/null)
if [ "${USERID}" ];then
$PRINTF "\nThe userid is ${USERID}\n"
else
$PRINTF "\nFailed to get the userid from /etc/passwd\n"
$EXIT 3
fi
else
$PRINTF "\nUser name was not specified.\n"
# Don't use a reserved exit code http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
$EXIT 3
fi
}
function showgroupid() {
local GROUPID
if [ "${1}" ]; then
checkuser "$1"
GROUPID=$($CAT /etc/passwd 2>/dev/null 2>/dev/null | $GREP "$1" 2>/dev/null | $CUT -d':' -f 4 2>/dev/null)
if [ "${GROUPID}" ];then
$PRINTF "\nThe groupid is ${GROUPID}\n"
else
$PRINTF "\nFailed to get the groupid from /etc/passwd\n"
$EXIT 3
fi
else
$PRINTF "\nUser name was not specified.\n"
# Don't use a reserved exit code http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
$EXIT 3
fi
}
function showcomment() {
local COMMENT
if [ "${1}" ]; then
checkuser "$1"
COMMENT=$($CAT /etc/passwd 2>/dev/null | $GREP "$1" 2>/dev/null | $CUT -d':' -f 5 2>/dev/null)
if [ "${COMMENT}" ]; then
$PRINTF "\nThe comment is ${COMMENT}\n"
else
$PRINTF "\nFailed to get the comment from /etc/passwd\n"
$EXIT 3
fi
else
$PRINTF "\nUser name was not specified.\n"
# Don't use a reserved exit code http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
$EXIT 3
fi
}
function showhome() {
local HOME
if [ "${1}" ]; then
checkuser "$1"
HOME=$($CAT /etc/passwd | $GREP "$1" | $CUT -d':' -f 6)
if [ "${HOME}" ]; then
$PRINTF "\nThe home directory is ${HOME}\n"
else
$PRINTF "\nFailed to get the HOME directory from /etc/passwd\n"
$EXIT 3
fi
else
$PRINTF "\nUser name was not specified.\n"
# Don't use a reserved exit code http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
$EXIT 3
fi
}
function showshell() {
local SHELL
if [ "${1}" ]; then
checkuser "$1"
SHELL=$($CAT /etc/passwd | $GREP "$1" | $CUT -d':' -f 7)
if [ "${SHELL}" ]; then
$PRINTF "\nThe shell is ${SHELL}\n"
else
$PRINTF "\nFailed to get the shell from /etc/passwd\n"
fi
else
$PRINTF "\nUsername was not specified.\n"
# Don't use a reserved exit code http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
$EXIT 3
fi
}
function showusage() {
$PRINTF "\nusage: $0 userid [user] - to show the userid for the user specified"
$PRINTF "\nusage: $0 groupid [user] - to show the group for the user specified"
$PRINTF "\nusage: $0 comment [user] - to any comments for the user specified"
$PRINTF "\nusage: $0 home [user] - to show the home directory for the user specified"
$PRINTF "\nusage: $0 shell [user] - to show the shell for the user specified\n"
# Don't use a reserved exit code http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
$EXIT 3
}
if [ -z "$1" ]; then
$PRINTF "\nNo arguments supplied. Usage examples are :\n"
showusage
fi
${CLEAR}
case $1 in
"userid")
showuserid "$2"
;;
"groupid")
showgroupid "$2"
;;
"comment")
showcomment "$2"
;;
"home")
showhome "$2"
;;
"shell")
showshell "$2"
;;
*)
$PRINTF "\nUnsupported argument. Usage examples are :\n"
showusage
esac