fix(lib/suggest/suggest.sh): fix usage of 'read'

pull/143/head
jwerle 2 years ago
parent e6faf5cced
commit 7523983418

@ -1,7 +1,7 @@
#!/bin/bash
## suggest version
VERSION="0.0.2"
VERSION="0.1.0"
## output usage
usage () {
@ -14,22 +14,22 @@ suggest () {
declare -a paths=()
declare -a seen=()
declare -a found=()
local query="${1}"
local query="$1"
case "${query}" in
case "$query" in
-h|--help)
usage
return 0
;;
-V|--version)
echo "${VERSION}"
echo "$VERSION"
return 0
;;
*)
if [ "-" = "${query:0:1}" ]; then
echo >&2 "error: Unknown argument \`${query}'"
echo >&2 "error: Unknown argument \`$query'"
return 1
fi
;;
@ -38,39 +38,40 @@ suggest () {
## search path
{
local res=""
IFS=':' read -ar paths <<< "${PATH}"
IFS=':' read -r -a paths <<< "$PATH"
for (( i = 0; i < ${#paths[@]}; ++i )); do
local path="${paths[$i]}"
local skip=0
## omit non existent paths
if ! test -d "${path}"; then
if ! test -d "$path"; then
continue
else
for (( n = 0; n < "${#seen[@]}"; ++n )); do
if [ "${path}" = "${seen[$n]}" ]; then
if [ "$path" = "${seen[$n]}" ]; then
skip=1;
break;
fi
done
## check if skip needed
if [ "1" = "${skip}" ]; then
if [ "1" = "$skip" ]; then
continue
fi
fi
## mark seen
seen+=( "${path}" )
seen+=("$path")
## find in path
if res=$(find "${path}" -name "${query}*" -prune -print 2>/dev/null); then
if [ -z "${res}" ]; then
if res=$(find "$path" -name "$query*" -prune -print 2>/dev/null); then
if [ -z "$res" ]; then
continue
fi
res="$(echo "${res}" | tr '\n' ' ')"
res="$(echo "$res" | tr '\n' ' ')"
## add to found count
found+=( $(echo -n "${res}") )
# shellcheck disable=SC2207
found+=($(echo -n "$res"))
fi
done
}
@ -81,13 +82,13 @@ suggest () {
if (( count == 1 )); then
echo "${found[0]}"
elif (( count > 0 )); then
printf "suggest: found %d result(s)\n" "${count}"
printf "suggest: found %d result(s)\n" "$count"
echo
for (( i = 0; i < count; ++i )); do
printf "%d %s\n" "$(echo -n "${found[$i]}" | wc -c | tr -d ' ')" "${found[$i]}"
done | sort -n | awk '{ print $2 }' | xargs printf ' %s\n'
else
echo "suggest: Couldn't anything to match \`${query}'"
echo "suggest: Couldn't anything to match \`$query'"
return 1
fi
return 0

Loading…
Cancel
Save