aboutsummaryrefslogtreecommitdiff
path: root/bin/eds
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-19 16:18:01 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-19 16:18:01 +1200
commit6da0d94530a532a6dfbb3ac6c5b9fa4f52139275 (patch)
treecf1f98da7ba86c3df20e18bd4f7e943624f55566 /bin/eds
parentTranslate apf(1) to POSIX sh (diff)
downloaddotfiles-6da0d94530a532a6dfbb3ac6c5b9fa4f52139275.tar.gz
dotfiles-6da0d94530a532a6dfbb3ac6c5b9fa4f52139275.zip
Port eds(1) to POSIX sh(1)
Even managed to keep the options. Removed issue about Bash scripts; han(1) needs to be Bash as it's specifically for the Bash help() builtin. There's no advantage to making it POSIX.
Diffstat (limited to 'bin/eds')
-rwxr-xr-xbin/eds75
1 files changed, 37 insertions, 38 deletions
diff --git a/bin/eds b/bin/eds
index 2afd0a07..3d0fceed 100755
--- a/bin/eds
+++ b/bin/eds
@@ -1,53 +1,52 @@
-#!/usr/bin/env bash
+#!/bin/sh
# 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
+# Need at least one script name
+if [ "$#" -eq 0 ] ; 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
+ep=${EDSPATH:-$HOME/.local/bin}
+if ! [ -d "$ep" ] ; then
+ mkdir -p -- "$ep" || exit
fi
-# Create a new array with the script directory prepended to the given names
-declare -a files
-files=("${@/#/"$edspath"/}")
+# Warn if that's not in $PATH
+case :$PATH: in
+ *:"$ep":*) ;;
+ *)
+ printf >&2 'eds: warning: %s not in PATH\n' "$ep"
+ ;;
+esac
-# 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
+# Prepend the path to each of the names given if they don't look like options
+for arg ; do
+ [ -n "$reset" ] || set -- && reset=1
+ case $arg in
+ --)
+ optend=1
+ set -- "$@" "$arg"
+ continue
+ ;;
+ -*)
+ if [ -z "$optend" ] ; then
+ set -- "$@" "$arg"
+ continue
+ fi
+ ;;
+ esac
+ optend=1
+ set -- "$@" "$ep"/"$arg"
done
-# Run the editor; if EDITOR isn't set, use ed(1)
-"${VISUAL:-"${EDITOR:-ed}"}" "${opts[@]}" -- "${files[@]}"
+# Run the editor over the arguments
+echo "${VISUAL:-"${EDITOR:-ed}"}" "$@"
+exit
# Make any created scripts executable if they now appear to be files
-for creation in "${creations[@]}" ; do
- [[ -f $creation ]] || continue
- chmod +x -- "$creation"
+for script ; do
+ [ -f "$script" ] || continue
+ chmod +x -- "$script"
done