#!/bin/bash # wants 'export el' in .bashrc function help { echo "$1 v0.3: find/edit files in basedir matching all args" echo "use: $1 [-h][-z][-a] [search terms]" echo " -a include hidden files" echo " -h help" echo " -z show basedir (\$el, defaults to current dir if unset)" } [ -n "$el" ] && { [ -d $el ] || exit 1 cd $el } case $1 in -a)o=A; shift;; -h)help $(basename $0); exit;; -z)echo ${el:-$(pwd)}; exit;; esac [ $1 ] || { ls -p$o; exit } eval $(ls -p$o | awk -v z="$*" ' BEGIN { split(z,a," ") } { t=1 for( x in a ) if( $0 !~ a[x] ) t=0 if( t ) { if( $0 !~ /\/$/ ) b[++i] = $0 c[++j] = $0 } } END { if( i == 1 ) { print "$EDITOR \"" b[1] "\";exit" } else for(k=1;k<=j;k++) print "matches[" k "]=\"" c[k] "\";" }') echo " 0) $*" while [[ $i -lt ${#matches[@]} ]];do i=$(($i+1));echo " $i) ${matches[$i]}";done read -e -p " $el$ " [ "$REPLY" == "0" ] && { $EDITOR "$*"; exit } [ -n "$(echo $REPLY | awk '/^[1-9]/')" ] && [ -n "${matches[$REPLY]}" ] && { $EDITOR "${matches[$REPLY]}"; exit } [ ${#REPLY} -ne 0 ] && $EDITOR "$REPLY"