Skip to content

Instantly share code, notes, and snippets.

@jpralves
Created May 14, 2019 22:53
Show Gist options
  • Save jpralves/0bbc8edc9582fbfb97e0e51c195a9622 to your computer and use it in GitHub Desktop.
Save jpralves/0bbc8edc9582fbfb97e0e51c195a9622 to your computer and use it in GitHub Desktop.
Sample bash script with embedded python script
#!/bin/bash
python_code_in_bash() {
python3 <(cat <<EOF
import sys
lines = sys.stdin.readlines()
print("Read {} lines from STDIN.".format(len(lines)))
print("Line {:<3}: {:<30} | {:<30}".format("N", "STDIN", "to lower"))
print("{:-<7} : {:-<30} | {:-<30}".format("", "", ""))
for cnt, line in enumerate(lines):
pline = line[:30]
print("Line {:<3}: {:<30} | {:<30}".format(cnt,
pline.strip(),
pline.strip().lower()))
print()
print("Number of args passed: {}".format(len(sys.argv)))
for cnt, arg in enumerate(sys.argv):
print("{}: {}".format(cnt, arg))
EOF
) $@
}
echo -e 'THIS IS A VERY LONG LINE OF TEXT THAT IS CUTTED\nSMALL LINE\nCONVERTED TO LOWCASE' | python_code_in_bash arg1 arg2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment