Last active
December 30, 2015 15:27
-
-
Save bersace/6b09e6bff8fb53bf64d2 to your computer and use it in GitHub Desktop.
Script python exécutable et sourceable par bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pour toi @jpic 😍