|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# File: |
| 4 | +# loctfileforcmd |
| 5 | +# |
| 6 | +# Description: |
| 7 | +# Locate a file or directory for a command. |
| 8 | +# |
| 9 | +# Examples: |
| 10 | +# # Locate a file with a name containing "stat" for command "vim". |
| 11 | +# loctfileforcmd {vim} stat |
| 12 | +# |
| 13 | +# # Locate a directory with a name containing "docu" for command "thunar". |
| 14 | +# loctfileforcmd {thunar} --d docu |
| 15 | +# |
| 16 | +# # same as previous, except return the third occurrence |
| 17 | +# loctfileforcmd {thunar} --d --3 docu |
| 18 | +# |
| 19 | +# Usage: |
| 20 | +# loctfileforcmd {<command_to_execute>} [options] <partial_or_full_file_name> |
| 21 | +# |
| 22 | +# Usage note: |
| 23 | +# Variable LOCATE_FILE_COMMAND in the configurations section must be assigned |
| 24 | +# a command that returns a file or directory path. The command must return |
| 25 | +# ordinary files by default and directories via the option specified by |
| 26 | +# LOCATE_FILE_COMMAND_OPT_DIRECTORIES for option --d to apply. |
| 27 | +# |
| 28 | +# Options: |
| 29 | +# --d return a directory instead of a file |
| 30 | +# --<1-9> return the specified occurrence |
| 31 | +# --p print the returned files or directories only |
| 32 | +# |
| 33 | +# Exit codes: |
| 34 | +# 0: file or directory found |
| 35 | +# 1: error occurred or file or directory not found |
| 36 | +# |
| 37 | +# Notes: |
| 38 | +# The options for this utility use double dashes to avoid overriding the |
| 39 | +# options for the command in the brackets. |
| 40 | +# |
| 41 | + |
| 42 | +# ======= CONFIGURATIONS ============== |
| 43 | + |
| 44 | +# Locate file command |
| 45 | +readonly LOCATE_FILE_COMMAND='locatefile' |
| 46 | + |
| 47 | +# Locate file command option to return directories only; can be empty |
| 48 | +readonly LOCATE_FILE_COMMAND_OPT_DIRECTORIES='-d' |
| 49 | + |
| 50 | +# Locate file command option to change the located occurrence; can be empty; |
| 51 | +# must require a numerical argument |
| 52 | +readonly LOCATE_FILE_COMMAND_OPT_OCCURRENCE='-o' |
| 53 | + |
| 54 | +# ======= ! CONFIGURATIONS ============== |
| 55 | + |
| 56 | +# ============================================ |
| 57 | +# Get the execute command |
| 58 | +# ============================================ |
| 59 | + |
| 60 | +# Note: The following could be done using command "set", however, command "set" |
| 61 | +# removes all arguments containing "?". |
| 62 | + |
| 63 | +for ((x=1; x<="$#"; x++)); do |
| 64 | + arg="${@:x:1}" |
| 65 | + |
| 66 | + if [ -z "${arg##*'}'*}" ]; then |
| 67 | + EXECUTE_COMMAND="${@:1:x}" |
| 68 | + shift "${x}" |
| 69 | + fi |
| 70 | +done |
| 71 | + |
| 72 | +if [ -z "${EXECUTE_COMMAND}" ]; then |
| 73 | + echo -e 'loctfileforcmd: invalid arguments:\nexecute command not specified' \ |
| 74 | + 1>&2 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +EXECUTE_COMMAND="${EXECUTE_COMMAND#{}" |
| 79 | +EXECUTE_COMMAND="${EXECUTE_COMMAND%\}}" |
| 80 | + |
| 81 | +# ============================================ |
| 82 | +# Check prerequisites |
| 83 | +# ============================================ |
| 84 | + |
| 85 | +if [ "$#" -eq 0 ]; then |
| 86 | + echo 'loctfileforcmd: no file or directory name specified' 1>&2 |
| 87 | + exit 1 |
| 88 | +fi |
| 89 | + |
| 90 | +if ! hash "${LOCATE_FILE_COMMAND}" >/dev/null 2>&1; then |
| 91 | + echo "loctfileforcmd: command \"${LOCATE_FILE_COMMAND}\" does not exist" 1>&2 |
| 92 | + exit 1 |
| 93 | +fi |
| 94 | + |
| 95 | +# ============================================ |
| 96 | +# Process options and arguments |
| 97 | +# ============================================ |
| 98 | + |
| 99 | +OPTS="$(getopt -o '#' --long d,1,2,3,4,5,6,7,8,9,p -n 'loctfileforcmd' -- \ |
| 100 | + "${@}")" |
| 101 | +[ "$?" -ne 0 ] && exit 1 |
| 102 | +eval set -- "${OPTS}" |
| 103 | + |
| 104 | +while true; do |
| 105 | + case "${1}" in |
| 106 | + --d) OPT_RETURN_DIRECTORIES='true';; |
| 107 | + --1|--2|--3|--4|--5|--6|--7|--8|--9) OPT_RETURN_OCCURRENCE="${1#'--'}";; |
| 108 | + --p) OPT_PRINT_ONLY='true';; |
| 109 | + --) shift; break;; |
| 110 | + * ) break;; |
| 111 | + esac |
| 112 | + shift |
| 113 | +done |
| 114 | + |
| 115 | +# ============================================ |
| 116 | +# Process locate file command options |
| 117 | +# ============================================ |
| 118 | + |
| 119 | +if [ "${OPT_RETURN_DIRECTORIES}" = 'true' ] && [ -n \ |
| 120 | + "${LOCATE_FILE_COMMAND_OPT_DIRECTORIES}" ]; then |
| 121 | + locfileOpts="${LOCATE_FILE_COMMAND_OPT_DIRECTORIES}" |
| 122 | +fi |
| 123 | + |
| 124 | +if [ -n "${OPT_RETURN_OCCURRENCE}" ] && [ -n \ |
| 125 | + "${LOCATE_FILE_COMMAND_OPT_OCCURRENCE}" ]; then |
| 126 | + locfileOpts+=" ${LOCATE_FILE_COMMAND_OPT_OCCURRENCE} ${OPT_RETURN_OCCURRENCE}" |
| 127 | +fi |
| 128 | + |
| 129 | +# ============================================ |
| 130 | +# Begin locate process |
| 131 | +# ============================================ |
| 132 | + |
| 133 | +loctfileOutput="$(${LOCATE_FILE_COMMAND} ${locfileOpts} "${@}")" |
| 134 | + |
| 135 | +if [ -z "${loctfileOutput}" ]; then |
| 136 | + echo "loctfileforcmd: file or directory \"${@}\" not found" 1>&2 |
| 137 | + exit 1 |
| 138 | +fi |
| 139 | + |
| 140 | +loctfileOutput="$(echo "${loctfileOutput}" | sed 's/ /\\n/g')" |
| 141 | + |
| 142 | +if [ "${OPT_PRINT_ONLY}" = 'true' ]; then |
| 143 | + echo "${loctfileOutput}" |
| 144 | +else |
| 145 | + ${EXECUTE_COMMAND} "${loctfileOutput}" |
| 146 | +fi |
| 147 | + |
0 commit comments