Skip to content

Instantly share code, notes, and snippets.

@bersace
Last active December 30, 2015 15:27

Revisions

  1. bersace revised this gist Dec 30, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion py.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    ## In bash, declare a variable script, assign value "#". In python, declare a
    ## variable scriptd, assign a heredoc containing the bash script.
    # shellcheck disable=SC2034
    script=""""
    script=""" "
    ## BEGIN BASH
    # my_sh_func [ARG ...]
  2. bersace revised this gist Dec 30, 2015. No changes.
  3. bersace revised this gist Dec 30, 2015. 1 changed file with 19 additions and 15 deletions.
    34 changes: 19 additions & 15 deletions py.sh
    Original file line number Diff line number Diff line change
    @@ -3,29 +3,33 @@
    #
    # This python script is callable and sourceable by bash.
    #
    # lint python with: flake8 --ignore=E225,E265 py.sh
    # lint bash with: sed '/^## END BASH/,$d' py.sh | shellcheck -

    ## In bash, declare a variable script, assign value "#". In python, declare a
    ## variable script. Assign a heredoc containing the bash script.
    ## variable scriptd, assign a heredoc containing the bash script.
    # shellcheck disable=SC2034
    script=""""
    ## BEGIN BASH
    if [[ ${BASH_SOURCE[0]} = $0 ]]; then
    if [ -z "${1-}" ]; then
    # Show help
    sed -E '1d;/^#/!d;/^##/d;s/^# ?//' $0
    exit 1
    else
    set -eux
    fi
    fi
    ## BASH FUNCTIONS
    # my_sh_func [ARG ...]
    # Echo arguments
    #
    # my_sh_func toto
    #
    my_sh_func() {
    echo $*
    }
    if [[ ${BASH_SOURCE[0]} = $0 ]]; then
    if declare -f "$1" >&2; then
    $@
    if declare -f -- "${1-}" >&2; then
    # When $1 is a bash function, call it and exit.
    set -eux
    $*
    exit 0
    else
    exec python3 $0 $*
    # Any other argument is passed to python script.
    exec python3 ${BASH_SOURCE[0]} $*
    fi
    else
    echo ${BASH_SOURCE[0]} sourced
  4. bersace created this gist Dec 30, 2015.
    45 changes: 45 additions & 0 deletions py.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/bin/bash
    ## -*- python -*-
    #
    # This python script is callable and sourceable by bash.
    #

    ## In bash, declare a variable script, assign value "#". In python, declare a
    ## variable script. Assign a heredoc containing the bash script.
    script=""""
    ## BEGIN BASH
    if [[ ${BASH_SOURCE[0]} = $0 ]]; then
    if [ -z "${1-}" ]; then
    # Show help
    sed -E '1d;/^#/!d;/^##/d;s/^# ?//' $0
    exit 1
    else
    set -eux
    fi
    fi
    ## BASH FUNCTIONS
    if [[ ${BASH_SOURCE[0]} = $0 ]]; then
    if declare -f "$1" >&2; then
    $@
    else
    exec python3 $0 $*
    fi
    else
    echo ${BASH_SOURCE[0]} sourced
    return 0
    fi
    ## Assert this statement is not reached in bash.
    exit 1
    ## END BASH
    """ # " # Close quote for bash syntax highlighting.

    ## Drop script variable, and begin python script as usual.
    del script

    import sys

    print(sys.argv)