Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix command to extract part of a hostname

Tags:

unix

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 .

like image 640
jdamae Avatar asked Mar 11 '11 03:03

jdamae


People also ask

How do I get the hostname only in Linux?

You could have used "uname -n" to just get the hostname only.

How do I find the fully qualified domain name in Linux?

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.

How do I print the hostname in Unix?

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.


1 Answers

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.

like image 93
pilcrow Avatar answered Sep 19 '22 15:09

pilcrow