#!/bin/ksh # bin - Will Mengarini # Use to edit new or existing executable files in ~/bin. # New files automatically have permissions set correctly; # existing files may be selected from wildcard expressions, # so the command "bin scri*", executed in $HOME, could be used to # edit "bin/scriptWithReallyLongName". # The following items must be in $ENV (presumably .kshrc): # # alias p=print # # function die # { # p "$*" >&2; exit 1 # } # # function now # { # eval $1 && return 0 # p -nu2 "Internal failure" # [[ -n $2 ]] && p -nu2 " ($2)" # p -u2 ": not $1" # exit 1 # } # # function Y # { # yn="" # until [[ $yn = [NnYy] ]];do # print -nu2 "$1 (Y/N)" # [[ $2 != "" ]] && print -nu2 " [$2]" # print -nu2 '? ' # stty raw;yn=`dd bs=1 count=1 2>/dev/null`;stty -raw # print -u2 # [[ $yn = ' ' ]] && yn=$2 # done # [[ $yn = [Yy] ]] && return 0 || return 1 # } #Expand wildcards target="$HOME"'/bin/'`cd ~/bin;p $1` #literal twiddle in [[ ]] is expanded but one from in a variable isn't #`cd wherever` is executed in a subshell so no `cd -` is needed #Test to avoid editing multiple wildcard matches [[ `p "$target"|wc -w` -ne 1 ]] && { #unquoted $target above could be expanded in dir different from bin select f in $target;do [[ -z $f ]] && p -u2 "Pick a number from the selections." || break #select always uses stderr, hence -u2 done || { p; exit 1; } #That allows quitting with ^D target=$f } now "[[ -n $target ]]" #Test for wildcards unexpanded in ~/bin [[ "$target" = *[][*?]* ]] && die "$0: $target: no match" #in [[ string = pattern ]], pattern is a globexp, not a regexp #Test for new file; prompt before creating in case of typos if [[ ! -f $target ]];then Y "Create $target" && { >$target; chmod u+rx $target; } || exit 1 fi #Edit using exec so "bin bin" will work exec $VISUAL +"se wm=0|1" $target