From 642e0d8ebd904ae71198ef68e5e9b967d2c5e5e7 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Tue, 13 Jul 2021 12:15:58 +0200 Subject: [PATCH] bin/b: revamp to use sudo if needed --- .local/bin/scripts/b | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.local/bin/scripts/b b/.local/bin/scripts/b index 292d302..f0cbf5b 100755 --- a/.local/bin/scripts/b +++ b/.local/bin/scripts/b @@ -1,17 +1,18 @@ #!/bin/sh # Show type & contents of given files or PWD +set -eo pipefail # gets the last arg or current dir for last; do true; done last=${last:-.} -tput setaf 4 && file "$last" | grep -v --color=never 'directory$' -test -e "$last" || exit 1 +tput setaf 4 && $(test -r "$last" || echo "sudo") file -E "$last" | ( grep -v --color=never 'directory$' || true ) -case "$(file --dereference --mime "$last")" in - *inode/directory*) ls -l --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "$@";; +elevate="$(test -r "$last" || echo "sudo")" +case "$($elevate file --dereference --mime "$last")" in + *inode/directory*) tput sgr0 && $(test -x "$last" || echo "sudo") ls -l --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "$@";; *binary) ;; - *) $(test -f "$last" -a ! -r "$last" && echo "sudo") bat --style header "$@" - tput setaf 3 && stat --format '%A size %sB, birth: %.10w mod %.10y' "$last" + *) $elevate bat --style header "$@" + tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last" ;; esac