From 764227657c87ea0c1b85bedfbece407c3a02535d Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Tue, 26 May 2020 11:04:40 +0200 Subject: [PATCH] zshsrc + zshenv: support hostnamectl iff hostname isn't present The hostname binary doesn't necessarily exist, while hostnamectl might be available. Closes: grml/grml-etc-core#96 --- etc/zsh/zshenv | 8 +++++++- etc/zsh/zshrc | 21 ++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/etc/zsh/zshenv b/etc/zsh/zshenv index 9a97788..9b70921 100644 --- a/etc/zsh/zshenv +++ b/etc/zsh/zshenv @@ -26,7 +26,13 @@ [[ -r /etc/environment ]] && source /etc/environment # set environment variables (important for autologin on tty) -export HOSTNAME=${HOSTNAME:-$(hostname)} +if [ -n "${HOSTNAME}" ] ; then + export HOSTNAME="${HOSTNAME}" +elif [[ -x $(which hostname) ]] ; then + export HOSTNAME="$(hostname)" +elif [[ -x $(which hostnamectl) ]] ; then + export HOSTNAME="$(hostnamectl --static)" +fi # make sure /usr/bin/id is available if [[ -x /usr/bin/id ]] ; then diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index 2969729..2875222 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -837,8 +837,16 @@ function grmlcomp () { _ssh_hosts=() _etc_hosts=() fi + + local localname + if check_com hostname ; then + localname=$(hostname) + elif check_com hostnamectl ; then + localname=$(hostnamectl --static) + fi + hosts=( - $(hostname) + "${localname}" "$_ssh_config_hosts[@]" "$_ssh_hosts[@]" "$_etc_hosts[@]" @@ -2509,8 +2517,15 @@ function grml_vcs_to_screen_title () { } function grml_maintain_name () { - # set hostname if not running on host with name 'grml' - if [[ -n "$HOSTNAME" ]] && [[ "$HOSTNAME" != $(hostname) ]] ; then + local localname + if check_com hostname ; then + localname=$(hostname) + elif check_com hostnamectl ; then + localname=$(hostnamectl --static) + fi + + # set hostname if not running on local machine + if [[ -n "$HOSTNAME" ]] && [[ "$HOSTNAME" != "${localname}" ]] ; then NAME="@$HOSTNAME" fi } -- 2.1.4