summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2022-03-06 14:10:50 -0500
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2022-03-06 14:11:41 -0500
commitc3678e8877669b55de9cd8d7b44fa7e642e3cf1f (patch)
tree5fbe3090142e15e7a7c5432372e7a7bc401c9163
parent796db64fcb1024278da4cc9989c055b694226459 (diff)
downloadnextbin-c3678e8877669b55de9cd8d7b44fa7e642e3cf1f.tar.xz
nextbin-c3678e8877669b55de9cd8d7b44fa7e642e3cf1f.zip
/usr/bin/env shebang, check test -ef works, refactor
-rwxr-xr-xnextbin33
1 files changed, 18 insertions, 15 deletions
diff --git a/nextbin b/nextbin
index 3a7a4e6..006b158 100755
--- a/nextbin
+++ b/nextbin
@@ -1,14 +1,7 @@
-#!/bin/sh
+#!/usr/bin/env sh
set -e
-die() {
- fmt=$1
- shift
- printf "nextbin %s failed: $fmt\n" "$orig_exe" "$@" >&2
- exit 1
-}
-
usage() {
# shellcheck disable=SC2016
echo 'usage: nextbin [-v] "$0" "$@"' >&2
@@ -19,12 +12,21 @@ case "$1" in
-*|'') usage; exit 1;;
*) action="exec"
esac
+
orig_exe=$1
shift
-exe_name=${orig_exe##*/}
+die() {
+ fmt=$1
+ shift
+ printf "nextbin %s failed: $fmt\n" "$orig_exe" "$@" >&2
+ exit 1
+}
+
+[ / -ef / ] || exec bash "$0" "$orig_exe" "$@" || die "test -ef doesn't work, and bash not found"
[ -n "$PATH" ] || die 'PATH is empty'
+exe_name=${orig_exe##*/}
path=$PATH
phase=1
found=
@@ -36,9 +38,7 @@ while :; do
phase=2
fi
elif [ "$exe" -ef "$orig_exe" ]; then
- if [ -n "$found" ]; then
- die 'duplicate PATH entries found'
- fi
+ [ -z "$found" ] || die 'duplicate PATH entries found'
elif [ -z "$found" ]; then
found=$exe
fi
@@ -49,6 +49,9 @@ while :; do
esac
done
-[ "$phase" != 1 ] || found=$(command -v "$exe_name") || die '%s is not in PATH' "$orig_exe"
-[ -n "$found" ] || die '%s is last instance' "$orig_exe"
-$action "$found" "$@"
+if [ "$phase" = 1 ]; then
+ found=$(command -v "$exe_name") || die '%s is not in PATH' "$exe_name"
+elif [ -z "$found" ]; then
+ die '%s is last instance' "$orig_exe"
+fi
+"$action" "$found" "$@"