Last Updated: February 25, 2016
·
387
· cfeduke

mk-sh-script

A function to place in ~/.bashrc or a zsh aliases file to prime new shell scripts:

function mk-sh-script {
  readonly local filename=$(basename $1)
  local dirname=$(dirname $1)
  if [[ -z $dirname || ($dirname == "." && ${1:0:1} != ".") ]]; then
    dirname=$HOME/bin
  fi
  if [[ ! -d $dirname ]]; then
    echo "Directory $dirname does not exist"
    return
  fi

  readonly local target="$dirname/$filename"
  if [[ ! -f $target ]]; then
    echo "#!/usr/bin/env bash

set -o nounset
set -o errexit

" > $target
    chmod +x $target
  fi

  $EDITOR +6 $HOME/bin/$@
}