Using the awk tool, write the command that will search through the /etc/passwd
file for an account with the username of "trev1". If the account name exist, the
command should output the login directory for this account.
Note: The use of pipes is NOT permitted!!!!
just to help the learners, here is the explanation of the command and what it does :
First let us analyze the fields in the /etc/passwd file :
same way, as per the question's condition : the login directory of such users is in the 6th field of the /etc/passwd file
trev1:x:1001:1001:Trev User:/home/trev1:/bin/bash
1 2 3 4 5 6 7
so, this command when we use it : awk -F ':' ' $1 == "trev1" { print $6 } ' /etc/passwd
awk -F ':' sets the field separator to a colon (:) character. This instructs awk to interpret each line of the input file as a sequence of fields, delimited by ':'.
$1 == "trev1" { print $6 }
: The condition $1 == "trev1" checks if the first field of the current line, which represents the username in the /etc/passwd file, matches "trev1".
and then If the username matches "trev1", the command prints the sixth field, which corresponds to the user's home directory in the /etc/passwd file.
and done !!
awk -F ':' ' $1 == "trev1" { print $6 } ' /etc/passwd
And we have a winner!!!! That command syntax will
absolutely accomplish the task!!!
Great job TM!!!
Another form that would get the job done is:
awk -F: ' /trev1/ { print $6 }' /etc/passwd
just to help the learners, here is the explanation of the command and what it does :
First let us analyze the fields in the /etc/passwd file :
same way, as per the question's condition : the login directory of such users is in the 6th field of the /etc/passwd file
trev1:x:1001:1001:Trev User:/home/trev1:/bin/bash
1 2 3 4 5 6 7
so, this command when we use it : awk -F ':' ' $1 == "trev1" { print $6 } ' /etc/passwd
awk -F ':' sets the field separator to a colon (:) character. This instructs awk to interpret each line of the input file as a sequence of fields, delimited by ':'.
$1 == "trev1" { print $6 }
: The condition $1 == "trev1" checks if the first field of the current line, which represents the username in the /etc/passwd file, matches "trev1".
and then If the username matches "trev1", the command prints the sixth field, which corresponds to the user's home directory in the /etc/passwd file.
and done !!
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.