file rename
[automated-distro-installer] / chost
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # chost: get canonical hostname
5
6 set -eE -o pipefail
7 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
8
9 host=$1
10 # ||: because if we are using 2+ resolvers, 1 may fail, causing error, but we still get
11 # a valid address and we just use that
12 addr=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
13 h=$(host $addr)
14 case $h in
15 *"no PTR record")
16 echo "error: chost: $h" >&2
17 exit 1
18 ;;
19 esac
20 h=${h##* }
21 # trailing dot in a hostname will mess up rsync
22 h=${h%.}
23 echo $h