On 8/23/2012 11:34 PM, Matthew Kruer wrote:
> Thanks for taking the time to look at the script.
>
> This script will play a small part in a larger script. Right now I have virtually no error handling so I was tinkering on trying to make it more idiot proof.
>
> Hostname2 is what I am calling it its actually a second system and the system will attempt to query the remote system.
>
> I guess the logic was when the user first runs the script, it gathers hostname of the current system (realistically the system name would not change while running the script, however there is the possibility that they want to change the name from just the hostname to the hostname+domain. ) So the first time they might bypass the hostname accept the default and then for the second name enter the host+domain. Upon conformation they could go back a change either. I don�t suppose there is a good way to echo out the values to the read line? So if all they need to do is change the number, it�s their to hit backspace and change the character and hit enter to update? Doing it that way might get rid of the need of the formatted version of the value.
>
Ah, I see. Then, sticking to your original style as much as possible, I'd write
it as:
#!/bin/bash
get_hostname() {
do
printf "Enter Hostname %d %-43s" "$1" "[${hostname[$1]}]" >&2
read input_hostname
if [[ -n $input_hostname ]]; then
hostname[$1]="$input_hostname"
fi
if [[ -z ${hostname[$1]} ]]; then
printf "ERROR: Hostname $1 is required.\n" >&2
fi
until [[ -n ${hostname[$1]} ]]
}
#Set Default
hostname[1]="$(hostname -s)"
do
#### Enter First Hostname ###
get_hostname 1
#### Enter Second Hostname ###
get_hostname 2
### Confirm ####
printf "Host 1 is %s\n" "${hostname[1]}" >&2
printf "Host 2 is %s\n" "${hostname[2]}" >&2
printf "Is this correct? [y/n]" >&2
read correct
until [[ $correct = "y" ]]
Notice I changed all your variables to lower case since all-upper-case by
convention is for exported variables only.
Regards,
Ed.