Discussion
Loading...

Post

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Bjarni |grep -i tech
@HerraBRE@floss.social  ·  activity timestamp 5 months ago

Yesterday, for reasons best left unstated, I invented a technique for combining #bash and #Python into a single file.

The motivation was to have something a bit more powerful than the #! to decide which installed Python to use.

The magic is in line two:

#!/bin/bash
""""true"
# bash sees this, python ignores it
if [ -e /path/to/file ]; then
exec python3 "$0" "$@"
else
echo "OMG, something is missing"
exit 1
fi
"""
# begin python fun!
from thing import python_stuff
python_stuff()

  • Copy link
  • Flag this post
  • Block
FND
@FND@hachyderm.io replied  ·  activity timestamp 2 months ago
@HerraBRE FWIW, @mahemoff used to call this kinda trick "chameleon file", which seems like an apt name: https://softwareas.com/svg-and-vml-in-one-chameleon-file/

PS: I believe the trailing quotation mark in `true"` is a typo, as that breaks the entire shebonk?

  • Copy link
  • Flag this comment
  • Block
Bjarni |grep -i tech
@HerraBRE@floss.social replied  ·  activity timestamp 5 months ago
@HerraBRE I have decided to call this technique "the shebonk".

Because it's link a shebang, only weird!

I've added shebonk support to my forked zipapp.py, will be using to build even more portable pagekite.py bundles than before!

I wonder if this hurts my chances of getting my enhanced #zipapp merged back into upstream #CPython? :-P

#python #bash #hacks

  • Copy link
  • Flag this comment
  • Block
Bjarni |grep -i tech
@HerraBRE@floss.social replied  ·  activity timestamp 5 months ago

... I shared this good fun with my local hackerspace Signal group, and one of the members responded with examples of TCL hackers pulling similar stunts.

He then followed up with the ultimate culmination of this kind of silliness: Actually Portable Executables https://justine.lol/ape.html

Amazing. :-D

  • Copy link
  • Flag this comment
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.0-rc.3.1 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login