This is a shell function written in bash that takes a domain name as an argument and returns the document root directory for that domain. It first checks if a domain name is provided and if not, it returns an error message.

It then uses the grep command to search for the domain name in the /etc/userdatadomains file. The ^ character matches the start of the line and the : character marks the end of the search, helping to prevent matching subdomains. If a match is found, awk is used to extract the 5th field in the line using == as a delimiter, which is the document root directory for that domain.

This function is useful for quickly retrieving the document root directory for a domain on a server.


[root@cloudvpsserver ~]# nk_docroot nkern.net


nk_docroot () {
# nk_docroot expects to be passed a domain name. Fail if not.
if [ "$1" = "" ]; then
    echo "You must provide a domain name"
    return 0
    # No longer using whodaat because of performance reasons.
    # whodat "$1" | awk '/Docroot:/ {print $NF}'

    # Using grep -E to find the domain in userdatadomains.
    # Need the ^ to denote that it's the start of the line and the : to end off the search. This helps prevent matching subdomains.
    # And the docroot is the 5th field in /etc/userdatadomains when using "==" as a delimeter.
    grep -E "^$domain:" /etc/userdatadomains | awk -F "==" '{print $5}'

Author: Nichole Kernreicht

Created: 2023-04-09 Sun 21:00