Chris Jakson
Chris Jakson

Reputation: 7

input data automatically into read variable on bash script

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

Upvotes: 0

Views: 952

Answers (2)

mklement0
mklement0

Reputation: 440122

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"

Upvotes: 1

George Vasiliou
George Vasiliou

Reputation: 6345

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

Upvotes: 1

Related Questions