jdamae
jdamae

Reputation: 3909

unix command to extract part of a hostname

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 .

Upvotes: 32

Views: 51110

Answers (8)

James
James

Reputation: 2143

Assuming the variable $HOSTNAME exists, so try echo ${HOSTNAME%%.*} to get the top-most part of the full-qualified hostname. Hope it helps.

If interested, the hint is from the below quoted partial /etc/bashrc on a REHL7 host:

      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;; ... ```

Upvotes: 0

monokrome
monokrome

Reputation: 1375

You can use IFS to split text by whichever token you want. For domain names, we can use the dot/period character.

#!/usr/bin/env sh

shorthost() {
  # Set IFS to dot, so that we can split $@ on dots instead of spaces.
  local IFS='.'

  # Break up arguments passed to shorthost so that each domain zone is
  # a new index in an array.
  zones=($@)

  # Echo out our first zone
  echo ${zones[0]}
}

If this is in your script then, for instance, you'll get test when you run shorthost test.example.com. You can adjust this to fit your use case, but knowing how to break the zones into the array is the big thing here, I think.

I wanted to provide this solution, because I feel like spawning another process is overkill when you can do it easily and completely within your shell with IFS. One thing to watch out for is that some users will recommend doing things like hostname -s, but that doesn't work in the BSD userland. For instance, MacOS users don't have the -s flag, I don't think.

Upvotes: 1

user3305937
user3305937

Reputation: 43

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

Upvotes: 1

user7040344
user7040344

Reputation:

I use command cut, awk, sed or bash variables

Operation

Via cut

[flying@lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | cut -d. -f1
testsrv1
[flying@lempstacker ~]$

Via awk

[flying@lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | awk -v FS='.' '{print $1}'
testsrv1
[flying@lempstacker ~]$

Via sed

[flying@lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | sed -r 's@([^.]*).(.*)@\1@g'
testsrv1
[flying@lempstacker ~]$

Via Bash Variables

[flying@lempstacker ~]$ hostName='testsrv1.main.corp.loc.domain.com'
[flying@lempstacker ~]$ echo ${hostName%%.*}
testsrv1
[flying@lempstacker ~]$

Upvotes: 5

H-man
H-man

Reputation: 281

try the -s switch: hostname -s

Upvotes: 28

Anthony Palmer
Anthony Palmer

Reputation: 972

To build upon pilcrow's answer, no need for new variable, just use inbuilt $HOSTANME.

echo $HOSTNAME-->my.server.domain
echo ${HOSTNAME%%.*}-->my

Tested on two fairly different Linux's.

2.6.18-371.4.1.el5, GNU bash, version 3.2.25(1)-release (i386-redhat-linux-gnu) 3.4.76-65.111.amzn1.x86_64, GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Upvotes: 27

pilcrow
pilcrow

Reputation: 58599

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.

Upvotes: 33

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181290

You can use cut:

echo "testsrv1.main.corp.loc.domain.com" | cut -d"." -f1

Upvotes: 30

Related Questions