FQDN into domain name and hostname

I'm working on a rather large script atm (it already takes 9 arguments). As such, I need to obtain both a server name and domain from the FQDN. From this, I want to both populate the hostname of the server, as well as the domain line in the /etc/resolv.conf file.

Obviously, this isn't working. I know the regex should be correct.
Here are the regex's I'm working with to obtain the hostname and domain portions:

Domain: /(?<=\.)[[:graph:]]*/
Hostname: /[[:alnum:]]*(?=\.)/

Can anyone recommend an implementation using either sed or perl? Once I have them output to stdout I can cast them into variables, or even directly into the files. I'd appreciate any help anyone can offer. Thanks!

how about the

domainname

command ?

The script is being used immediately following automated system provisioning, so that returns (none).

Basically the script takes in the following as inputs:
<bond0ip> <bond0snm> <bond0network> <eth1ip> <eth1snm> <eth1network> <FQDN> <DGW> <nameserver>

From this it will perform a ton of different actions. <FQDN> had originally just been hostname, however there is now a vendor requirement that we include a domain line in our resolv.conf file, so I have to make it into a FQDN and extract both from the one item so I can write them into the appropriate config files. Hope that clears stuff up a bit.

The easiest way would be if I could put in host name and domain separately, but as far as I know you can't use more than 9 arguments in a shell script (at least I've always had issues since $10 is viewed as $1 plus a 0).

# cat script
#!/bin/sh
for i in ${@};do echo ${i};done

# /temp/script 1 2 3 4 5 6 7 8 9 10 11
1
2
3
4
5
6
7
8
9
10
11
1 Like

you probably can't get much help without posting your script or at least relevant portions of it... nobody really wants to guess how you're trying to do everthing.

the way you're doing your hostname and domain seems a lot more complicated then it needs to be.

if, for example, you input your FQDN and assign it to a varialbe called FQDN

HOSTNAME=`echo $FQDN |awk -F. '{ print $1 }'`
DOMAIN=`echo $FQDN |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' `

a few approachs come to mind, for getting variables input for your script.

  1. you could use read statements to get all your paramaters, this would let you have as many input varbs as you would ever need.
  2. you could put all your paramaters into a seperate file, then simply source that.

parm file contents:
host=somehostname
domain=something.com
bond0ip=whatever
(etc)...

1 Like

I used the method you showed above, thank you!
FYI here are the applicable parts:

#Format:
#network.sh [bond0ip] [bond0snm] [bond0network] [eth1ip] [eth1snm] [eth1network] [FQDN] [DGW] [nameserver]
#Arguments are not optional.

#Split FQDN into hostname and domain
HOSTNAME=`echo $7 |awk -F. '{ print $1 }'`
DOMAIN=`echo $7 |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' `
#Configure DGW and Hostname
sed -i '/HOSTNAME/ d' /etc/sysconfig/network
echo HOSTNAME=$HOSTNAME >> /etc/sysconfig/network
echo GATEWAY=$8 >> /etc/sysconfig/network

#Configure DNS Server
echo domain $DOMAIN > /etc/resolv.conf
echo nameserver $9 >> /etc/resolv.conf
echo options timeout:1 attempts:2 >> /etc/resolv.conf
echo hosts:    files    dns >> /etc/nsswitch.conf

Many thanks for your help.

awk? Hmmm... I suppose. If portability is the goal (no bash, for example), then use expr or cut or even sed instead (IMHO).

#Split FQDN into hostname and domain
HOSTNAME=`expr "$7" : '\([^.][^.]*\)\..*'`
DOMAIN=`expr "$7" : '[^.][^.]*\.\(.*\)'`

Or use cut...

HOSTNAME=`echo "$7" | cut -f1 -d.`
DOMAIN=`expr "$7" | cut -f2- -d.`

or even sed... you're using sed to doctor things up anyhow...

HOSTNAME=`echo "$7" | sed  's/\..*//'`
DOMAIN=`expr "$7" | sed -n 's/[^.]*\.//p'`

Just my opinion... awk would be my last choice for this.

Actually in this case $i does not contain an enumeration but the parameter values, so it isn't necessary to use curly brackets, but the quotes are necessary to capture arguments that contain space characters..

for i in "$@";do echo "$i";done
HOSTNAME=${7%%.*}
DOMAIN=${7#*.}
1 Like

As I said, and as you mentioned in your post, if you have bash, then it opens up more possibilities (that are even more efficient). It's just not portable, Bourne shell wise.

This is portable with bash, ksh and all POSIX shells, on a SUN OS perhaps with /usr/xpg4/bin.