diff --git a/README.md b/README.md index bfbc237..84315ef 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -mksh -==== +mkx +=== -- [mkx](#mkx) +- [Formerly _mksh_](#formerly-mksh) ``` -mksh 1.2.1 +mkx 2.0.0 -Usage: mksh [-bhvx] [-i interpreter] scriptfile +Usage: mkx [-bhvx] [-i interpreter] scriptfile Make new shell script executable file from template. @@ -26,23 +26,8 @@ Options: ``` -## mkx +## Formerly _mksh_ -`mksh` can be invoked as `mkx` and is aliased as such in this repo. Invoking it as `mkx` is like calling `mksh -b`, and `mkx outputfile interpreter` is like `mksh -i interpreter outputfile`. +As the name _mksh_ is more commonly associated with the [MirBSD Korn Shell](https://www.mirbsd.org/mksh.htm), this repo has been renamed from _mksh_ to _mkx_. The same goes for the namesake script within. -``` -mksh 1.2.1 - -Usage: mkx [-hv] scriptfile [interpreter] - -Make new base shell script executable file. -If the file already exists, it will make it executable. - -When not specified, interpreter defaults to '/bin/sh'. - -Options: - - -h Show this help screen. - -v Show script name and version number. - -``` +Invoking `mksh` with `mkx` used to be the equivalent of `mksh -b` or `mksh -i `. But the with this name change, `mkx` was now made to work like `mksh`. diff --git a/mksh b/mksh deleted file mode 100755 index 8e3d96d..0000000 --- a/mksh +++ /dev/null @@ -1,312 +0,0 @@ -#!/bin/sh -# mksh -# Make new shell script executable file from template. - -VERSION='1.2.1' - -mksh_main() { - trap _exit INT TERM - - e_args=16 - e_exists=17 - e_missing_app=18 - - _require realpath - - scriptinv="$( basename "$0" )" - scriptpath="$( realpath "$0" )" - scriptname="$( basename "$scriptpath" )" - scriptdir="$( dirname "$scriptpath" )" - - bare=0 - interpreter='/bin/sh' - mode="$scriptinv" - - case "$mode" in - mkx) ;; - *) mode=mksh ;; - esac - - while getopts bhi:vx opt - do - case $opt in - b|x) bare=1 ;; - h) mksh_help && return ;; - i) bare=1; interpreter="$OPTARG" ;; - v) mksh_version && return ;; - *) _invalid_opt ;; - esac - done - - shift "$(( OPTIND - 1 ))" - - [ $# -lt 1 ] && mksh_help && return 16 - - newscript="$1" - - shift - - if [ "$mode" = "mkx" ] - then - bare=1 - [ $# -gt 0 ] && interpreter="$1" && shift - fi - - if [ $bare -eq 0 ] - then - mksh_template - else - mksh_bare - fi - - chmod a+x "$newscript" - _echo "$newscript" - _exit -} - -mksh_bare() { - if [ ! -f "$newscript" ] - then - echo "#!$interpreter" > "$newscript" - echo "" >> "$newscript" - fi - - [ -f "$newscript" ] && chmod a+x "$newscript" -} - -mksh_help() { - [ "$mode" = "mkx" ] && mkx_help && return - -cat < "$newscript" -} - -mksh_version() { - _echo "$scriptname $VERSION" -} - -mkx_help() { -cat <&2 -} - -_exit() { - exit_code=$? - exit $exit_code -} - -_invalid_opt() { - _error "Invalid option: $opt" - _echo - mksh_help - exit $e_args -} - -_require() { - missing_bin=0 - - for bin in "$@" - do - if ! which "$bin" > /dev/null 2>&1 - then - missing_bin=1 - _error "Required: $bin" - fi - done - - if [ $missing_bin -ne 0 ] - then - _fatal $e_missing_app "One or more executables or apps are missing." - fi -} - -mksh_main "$@" -exit - -__TEMPLATE__ -#!/bin/sh -# __SCRIPTNAME__ - -VERSION='1.0.0' - -__SCRIPTNAMESC___main() { - trap _exit INT TERM - - e_args=16 - e_missing_app=17 - - _require realpath - - scriptinv="$( basename "$0" )" - scriptpath="$( realpath "$0" )" - scriptname="$( basename "$scriptpath" )" - scriptdir="$( dirname "$scriptpath" )" - - while getopts hv opt - do - case $opt in - h) cmd=help ;; - v) cmd=version ;; - *) _invalid_opt ;; - esac - done - - shift "$(( OPTIND - 1 ))" - - [ -z "$cmd" ] && cmd="$1" - shift - - [ -z "$cmd" ] && cmd="help" - #[ -z "$cmd" ] && cmd="process" - - case "$cmd" in - help|process|version) "__SCRIPTNAMESC___$cmd" "$@" ;; - *) _invalid_cmd ;; - esac - - _exit -} - -__SCRIPTNAMESC___help() { -cat <&2 -} - -_exit() { - exit_code=$? - exit $exit_code -} - -_fatal() { - exit_code="$1" - shift - _error "$@" - exit "$exit_code" -} - -_invalid_cmd() { - _error "Invalid command: $cmd" - _echo - __SCRIPTNAMESC___help - exit $e_args -} - -_invalid_opt() { - _error "Invalid option: $opt" - _echo - __SCRIPTNAMESC___help - exit $e_args -} - -_require() { - missing_bin=0 - - for bin in "$@" - do - if ! which "$bin" > /dev/null 2>&1 - then - missing_bin=1 - _error "Required: $bin" - fi - done - - if [ $missing_bin -ne 0 ] - then - _fatal $e_missing_app "One or more executables or apps are missing." - fi -} - -__SCRIPTNAMESC___main "$@" diff --git a/mkx b/mkx deleted file mode 120000 index 197c0a1..0000000 --- a/mkx +++ /dev/null @@ -1 +0,0 @@ -mksh \ No newline at end of file diff --git a/mkx b/mkx new file mode 100755 index 0000000..e78bcd8 --- /dev/null +++ b/mkx @@ -0,0 +1,276 @@ +#!/bin/sh +# mkx +# Make new shell script executable file from template. + +VERSION='2.0.0' + +mkx_main() { + trap _exit INT TERM + + e_args=16 + e_exists=17 + e_missing_app=18 + + _require realpath + + scriptpath="$( realpath "$0" )" + scriptname="$( basename "$scriptpath" )" + scriptdir="$( dirname "$scriptpath" )" + + bare=0 + interpreter='/bin/sh' + + while getopts bhi:v opt + do + case $opt in + b) bare=1 ;; + h) mkx_help && return ;; + i) bare=1; interpreter="$OPTARG" ;; + v) mkx_version && return ;; + *) _invalid_opt ;; + esac + done + + shift "$(( OPTIND - 1 ))" + + [ $# -lt 1 ] && mkx_help && return 16 + + newscript="$1" + + shift + + if [ $bare -eq 0 ] + then + mkx_template + else + mkx_bare + fi + + chmod a+x "$newscript" + _echo "$newscript" + _exit +} + +mkx_bare() { + if [ ! -f "$newscript" ] + then + echo "#!$interpreter" > "$newscript" + echo "" >> "$newscript" + fi + + [ -f "$newscript" ] && chmod a+x "$newscript" +} + +mkx_help() { +cat < "$newscript" +} + +mkx_version() { + _echo "$scriptname $VERSION" +} + +_echo() { + echo "$@" +} + +_error() { + _echo "$@" >&2 +} + +_exit() { + exit_code=$? + exit $exit_code +} + +_invalid_opt() { + _error "Invalid option: $opt" + _echo + mkx_help + exit $e_args +} + +_require() { + missing_bin=0 + + for bin in "$@" + do + if ! which "$bin" > /dev/null 2>&1 + then + missing_bin=1 + _error "Required: $bin" + fi + done + + if [ $missing_bin -ne 0 ] + then + _fatal $e_missing_app "One or more executables or apps are missing." + fi +} + +mkx_main "$@" +exit + +__TEMPLATE__ +#!/bin/sh +# __SCRIPTNAME__ + +VERSION='1.0.0' + +__SCRIPTNAMESC___main() { + trap _exit INT TERM + + e_args=16 + e_missing_app=17 + + _require realpath + + scriptinv="$( basename "$0" )" + scriptpath="$( realpath "$0" )" + scriptname="$( basename "$scriptpath" )" + scriptdir="$( dirname "$scriptpath" )" + + while getopts hv opt + do + case $opt in + h) cmd=help ;; + v) cmd=version ;; + *) _invalid_opt ;; + esac + done + + shift "$(( OPTIND - 1 ))" + + [ -z "$cmd" ] && cmd="$1" + shift + + [ -z "$cmd" ] && cmd="help" + #[ -z "$cmd" ] && cmd="process" + + case "$cmd" in + help|process|version) "__SCRIPTNAMESC___$cmd" "$@" ;; + *) _invalid_cmd ;; + esac + + _exit +} + +__SCRIPTNAMESC___help() { +cat <&2 +} + +_exit() { + exit_code=$? + exit $exit_code +} + +_fatal() { + exit_code="$1" + shift + _error "$@" + exit "$exit_code" +} + +_invalid_cmd() { + _error "Invalid command: $cmd" + _echo + __SCRIPTNAMESC___help + exit $e_args +} + +_invalid_opt() { + _error "Invalid option: $opt" + _echo + __SCRIPTNAMESC___help + exit $e_args +} + +_require() { + missing_bin=0 + + for bin in "$@" + do + if ! which "$bin" > /dev/null 2>&1 + then + missing_bin=1 + _error "Required: $bin" + fi + done + + if [ $missing_bin -ne 0 ] + then + _fatal $e_missing_app "One or more executables or apps are missing." + fi +} + +__SCRIPTNAMESC___main "$@"