77# Recursively find a file or directory for a command.
88#
99# Examples:
10- # # recursively find and return a file with a name containing "stat" for
11- # # command "vim -c 'set title'"
12- # findfileforcmd {vim -c 'set title'} stat
10+ # # Recursively find a file with a name containing "stat" for command "vim".
11+ # findfileforcmd {vim} stat
1312#
14- # # recursively find and return a directory with a name containing "docume"
15- # # for command "thunar"
13+ # # Recursively find a directory with a name containing "docume" for command
14+ # # "thunar".
1615# findfileforcmd {thunar} --d docume
1716#
1817# # same as previous, except return the third occurrence
1918# findfileforcmd {thunar} --d --3 docume
2019#
2120# Usage:
22- # findfileforcmd {<command to execute >} [options] <partial or full file name >
21+ # findfileforcmd {<command_to_execute >} [options] <partial_or_full_file_name >
2322# [directory]
2423#
2524# [directory]: directory to search in; default is current.
4342
4443# Set this variable to "true" to support file names with spaces. If true and
4544# all occurrences are returned using option --a, only the last occurrence
46- # will be used by most execute commands .
45+ # will be used by most execute command .
4746readonly SUPPORT_FILE_NAMES_WITH_SPACES=' false'
4847
4948# ======= ! CONFIGURATIONS ==============
5049
5150# ============================================
52- # Get execute command
51+ # Get the execute command
5352# ============================================
5453
5554# Note: The following could be done using command "set", however, command "set"
5655# removes all arguments containing "?".
5756
5857for (( x= 1 ; x<= "$# "; x++ )) ; do
59- if [[ " ${@: x: 1} " = * ' }' * ]]; then
60- EXEC_CMD=" ${@: 1: x} "
58+ arg=" ${@: x: 1} "
59+
60+ if [ -z " ${arg##* ' }' * } " ]; then
61+ EXECUTE_COMMAND=" ${@: 1: x} "
6162 shift " ${x} "
6263 fi
6364done
6465
65- if [ -z " ${EXEC_CMD } " ]; then
66+ if [ -z " ${EXECUTE_COMMAND } " ]; then
6667 echo -e ' findfileforcmd: invalid arguments:\nexecute command not specified' \
6768 1>&2
6869 exit 1
6970fi
7071
71- EXEC_CMD =" ${EXEC_CMD # {} "
72- EXEC_CMD =" ${EXEC_CMD % \} } "
72+ EXECUTE_COMMAND =" ${EXECUTE_COMMAND # {} "
73+ EXECUTE_COMMAND =" ${EXECUTE_COMMAND % \} } "
7374
7475# ============================================
7576# Process prerequisites
@@ -91,10 +92,10 @@ eval set -- "${OPTS}"
9192
9293while true ; do
9394 case " ${1} " in
94- --d) RET_DIRS =' true' ;;
95- --1|--2|--3|--4|--5|--6|--7|--8|--9) RET_OCCUR =" ${1# ' --' } " ;;
96- --a) RET_ALL_OCCUR =' true' ;;
97- --p) PRINT_ONLY =' true' ;;
95+ --d) OPT_RETURN_DIRECTORIES =' true' ;;
96+ --1|--2|--3|--4|--5|--6|--7|--8|--9) OPT_RETURN_OCCURRENCE =" ${1# ' --' } " ;;
97+ --a) OPT_RETURN_ALL_OCCURRENCES =' true' ;;
98+ --p) OPT_PRINT_ONLY =' true' ;;
9899 --) shift ; break ;;
99100 * ) break ;;
100101 esac
@@ -111,13 +112,13 @@ else
111112 findDir=' .'
112113fi
113114
114- if [ " ${RET_DIRS } " = ' true' ]; then
115+ if [ " ${OPT_RETURN_DIRECTORIES } " = ' true' ]; then
115116 findOptType=' -type d'
116117else
117118 findOptType=' -type f'
118119fi
119120
120- if [ -z " ${RET_OCCUR } " ] && [ " ${RET_ALL_OCCUR } " != ' true' ]; then
121+ if [ -z " ${OPT_RETURN_OCCURRENCE } " ] && [ " ${OPT_RETURN_ALL_OCCURRENCES } " != ' true' ]; then
121122 findOptsPrintQuit=' -print -quit'
122123fi
123124
@@ -133,26 +134,26 @@ if [ -z "${findOutput}" ]; then
133134 exit 1
134135fi
135136
136- if [ " ${RET_ALL_OCCUR } " != ' true' ]; then
137- if [ -n " ${RET_OCCUR } " ]; then
137+ if [ " ${OPT_RETURN_ALL_OCCURRENCES } " != ' true' ]; then
138+ if [ -n " ${OPT_RETURN_OCCURRENCE } " ]; then
138139 findOutputNumOccurs=" $( echo " ${findOutput} " | wc -l) "
139140
140- if [ " ${RET_OCCUR } " -gt " ${findOutputNumOccurs} " ]; then
141- RET_OCCUR =" ${findOutputNumOccurs} "
141+ if [ " ${OPT_RETURN_OCCURRENCE } " -gt " ${findOutputNumOccurs} " ]; then
142+ OPT_RETURN_OCCURRENCE =" ${findOutputNumOccurs} "
142143 fi
143- findOutput=" $( echo " ${findOutput} " | sed " ${RET_OCCUR } q;d" ) "
144+ findOutput=" $( echo " ${findOutput} " | sed " ${OPT_RETURN_OCCURRENCE } q;d" ) "
144145 else
145146 findOutput=" $( echo " ${findOutput} " | sed " 1q;d" ) "
146147 fi
147148fi
148149
149- if [ " ${PRINT_ONLY } " = ' true' ]; then
150+ if [ " ${OPT_PRINT_ONLY } " = ' true' ]; then
150151 echo " ${findOutput} "
151152else
152153 if [ " ${SUPPORT_FILE_NAMES_WITH_SPACES} " = ' true' ]; then
153- ${EXEC_CMD } " ${findOutput} "
154+ ${EXECUTE_COMMAND } " ${findOutput} "
154155 else
155- ${EXEC_CMD } ${findOutput}
156+ ${EXECUTE_COMMAND } ${findOutput}
156157 fi
157158fi
158159
0 commit comments