Post

0x03 - writing a Python program

0x03 - writing a Python program

bin 0x03 of LiveOverflow’s binexp series
he moves from C to Python this time shows how to turn boring bash input repetition into a tiny script.

the result?
this lil script:

1
2
3
4
5
6
import sys

if len(sys.argv) == 2:
    print("Knock, knock, {0}".format(sys.argv[1]))
else:
    sys.stderr.write("Usage: {0} <name>\n".format(sys.argv[0]))

breaking it down (my way)

  • sys.argv[] → like argv[] in C, holds command-line arguments
  • argv[0] → script name
  • argv[1] → the actual input
  • sys.stderr.write() → prints error to stderr, not stdout
  • .format() → OG string formatting style (not f-strings, but still ✨clean✨ tho i prefer f-string)

basic af, but the feeling of swapping bash repetition for Python?
kinda addicting.


run it like this

1
python3 knock.py lav
Knock, knock, lav

no args?

1
python3 knock.py
Usage: knock.py <name>

tl;dr

  • wrote a python script to handle cli args
  • learned to print errors to stderr (like in C)
  • replaced bash muscle memory with logic muscle
  • realized Python + binaries gonna be a powerful combo

till the next leak :D
~ lav

This post is licensed under CC BY 4.0 by the author.