comint-mime icon indicating copy to clipboard operation
comint-mime copied to clipboard

error when command hostname not available

Open jnboehm opened this issue 3 years ago • 2 comments
trafficstars

When using comint-mime in shell-mode, it complains that the command hostname cannot be found

bash: hostname: command not found

Is there perhaps a more portable way to retrieve the value for hostname? I suppose the variable $HOSTNAME could be checked (which bash sets) or if hostname is not available maybe hostnamectl hostname could be executed, which does the same on systemd-based systems. If all that fails maybe it could just pass along any string? The error above didn't change the outcome as far as I could see.

Otherwise, it might be sensible to specify that this command is a dependency of the project.

jnboehm avatar Nov 29 '21 22:11 jnboehm

Sure, any help to make that script as portable as possible is welcome. But from what you, I gather $HOSTNAME bash-specific, and systemd is even less ubiquitous. So I'm not quite sure what to do here. What kind of system doesn't have the hostname command?

astoff avatar Nov 30 '21 15:11 astoff

Fair point, I was surprised myself that I didn't have it installed, apparently it's part of the inetutils package, which I didn't need to install yet.

I was thinking along of trying those three options in order and set a local hostname variable based on that, which is passed on. The general procedure would be

        if command -v hostname >/dev/null;
        then
            hostname=$(hostname);
        elif command -v hostnamectl >/dev/null;
        then
            hostname=$(hostnamectl hostname);
        elif [ -n $HOSTNAME ];
        then
            hostname=$HOSTNAME
        else
            echo "hostname not detected" >&2
            # maybe just use a fixed string?
            return 1
        fi

I wrote this down and for me it does not complain about a missing executable anymore, but maybe this option is a bit verbose to just retrieve the hostname. I actually don't know what is being done with that value, so maybe a fixed string could just be used instead? I don't know if that causes problems, but as I said it still worked for me even when the hostname command was not found.

The entire diff reads

diff --git a/comint-mime.sh b/comint-mime.sh
index b718aac..3808ac2 100644
--- a/comint-mime.sh
+++ b/comint-mime.sh
@@ -4,6 +4,7 @@
 mimecat () {
     local type
     local file
+    local hostname
     case "$1" in
         -h|--help)
             echo "Usage: mimecat [-t TYPE] [FILE]"
@@ -22,8 +23,22 @@ mimecat () {
         base64 | xargs -0 printf '\033]5151;{"type":"%s"}\n%s\033\\\n' "$type"
     else
         file=$(realpath -e "$1") || return 1
+        if command -v hostname >/dev/null;
+        then
+            hostname=$(hostname);
+        elif command -v hostnamectl >/dev/null;
+        then
+            hostname=$(hostnamectl hostname);
+        elif [ -n $HOSTNAME ];
+        then
+            hostname=$HOSTNAME
+        else
+            echo "hostname not detected" >&2
+            # maybe just use a fixed string?
+            return 1
+        fi
         [ -n "$type" ] || type=$(file -bi "$file")
         printf '\033]5151;{"type":"%s"}\nfile://%s%s\033\\\n' \
-               "$type" "$(hostname)" "$file"
+               "$type" "$hostname" "$file"
     fi
 }

jnboehm avatar Nov 30 '21 23:11 jnboehm

It looks like uname -n is the right way to get the hostname portably.

astoff avatar Aug 14 '22 10:08 astoff