0

How can i detect the servers domain name and automatically have it inputted so i can just press enter

    echo "
Please enter your domain name : "
read Domain
echo $Domain > /root/domain.info
Hostname=server.$Domain

Thanks, Chris

  • 1
    assuming this is a linux server, and not Microsoft Windows, depending on your linux distribution see if you have the environment variable `$dnsdomainname` or `$domainname`. If so problem solved. Also look at http://unix.stackexchange.com/questions/38209/how-to-get-the-hostname-along-with-the-domain-name – ron Dec 19 '16 at 00:26
  • 1
    can also do a `if [ -z "$domainname" ]` for example to check if there is a domain name set, if not then prompt user to enter something. http://stackoverflow.com/questions/11686208/check-if-environment-variable-is-already-set – ron Dec 19 '16 at 00:30

2 Answers2

1
Hostname=$(uname -n) #or $(hostname -f)
Nameserver=$(cat /etc/resolv.conf)

To check if it is set:

if [ -z "$Hostname" ];then  
read -p "Give name " name
Hostname=$name
fi

Also notice that Linux / UNIX comes with the following utilities to display hostname / domain name:

a) hostname – show or set hostname
b) domainname – show or set NIS/YP domain name
c) dnsdomainname – show DNS domain name
d) nisdomainname – show or set NIS/YP domain name
e) ypdomainname – show or set NIS/YP domain name

George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • While this does indeed get your host name, very frequently that ISN"T the FQDN that "the world" uses to get to the box. – ivanivan Dec 19 '16 at 03:47
1

It sounds like you want to prefill the edit buffer when you use the read builtin.

Bash v4+ allows you to do that by combining the -e option (which activates readline library support) with -i <editBufferDefault>; e.g.:

editBufferDefault=$(domainname)  # get default value to offer to the user
read -e -p "Please enter your domain name: " -i "$editBufferDefault"
mklement0
  • 382,024
  • 64
  • 607
  • 775