#!/usr/bin/env bash # Create and edit executable scripts in a directory EDSPATH (defaults to ~/.local/bin) # Give up completely if no BASH_VERSINFO (<2.0) [ -n "$BASH_VERSINFO" ] || exit # Process options, including detecting requests for help declare -a opts for arg ; do case $arg in --) shift break ;; -*) shift opts[${#opts[@]}]=$arg ;; esac done # Need at least one file after options are parsed out if ! (($#)) ; then printf >&2 'eds: Need at least one script name\n' exit 2 fi # Create the script directory if it doesn't exist yet edspath=${EDSPATH:-$HOME/.local/bin} if [[ ! -d $edspath ]] ; then mkdir -p -- "$edspath" || exit fi # Create a new array with the script directory prepended to the given names declare -a files files=("${@/#/"$edspath"/}") # Collect the names of any scripts that don't exist yet so we can make them # executable after we're done editing declare -a creations for file in "${files[@]}" ; do [[ -e $file ]] && continue creations[${#creations[@]}]=$file done # Run the editor; if EDITOR isn't set, use vi(1) "${EDITOR:-vi}" "${opts[@]}" -- "${files[@]}" # Make any created scripts executable if they now appear to be files for creation in "${creations[@]}" ; do [[ -f $creation ]] || continue chmod +x -- "$creation" done