I would like to extract the first part of this hostname testsrv1
from testsrv1.main.corp.loc.domain.com
in UNIX, within a shell script.
What command can I use? It would be anything before the first period .
You could have used "uname -n" to just get the hostname only.
You can check the FQDN using hostname --fqdn or the domain name using dnsdomainname. You cannot change the FQDN with hostname or dnsdomainname. Technically: The FQDN is the name getaddrinfo returns for the hostname returned by gethostname. The DNS domain name is the part after the first dot.
Print the hostname of the system The basic functionality of the hostname command is to display the name of the system on the terminal. Just type the hostname on the unix terminal and press enter to print the hostname.
Do you have the server name in a shell variable? Are you using a sh
-like shell? If so,
${SERVERNAME%%.*}
will do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With