Skip to content

Instantly share code, notes, and snippets.

@shinkou
shinkou / argtest.sh
Last active May 1, 2024 12:30
A snippet to showcase how to manipulate options and arguments in Bash
#!/bin/bash
main()
{
local opt_a=
local opt_b=
local opt_c=
local args=
getargs "$@"
if [ -n "$opt_a" ]; then echo "opt_a [$opt_a]"; fi
@shinkou
shinkou / catchsig.sh
Created May 2, 2024 04:34
A snippet to showcase how to trap and control signals in Bash
#!/bin/bash
main()
{
echo "PID: $$"
for s in SIGHUP SIGINT SIGQUIT SIGTERM EXIT; do
if [ SIGTERM = $s ]; then
trap "echo '$s is caught'; exit" $s
else
trap "echo '$s is caught'" $s
fi
@shinkou
shinkou / semademo.py
Created January 30, 2025 04:18
A short script to demostrate how to use semaphore to manipulate async coroutines
#!/usr/bin/env python3
# -*- vim: set fileencoding=utf-8 ff=unix: -*-
from random import randint
import argparse, asyncio
async def wait4me(semaphore, taskno):
async with semaphore:
waittime = randint(1, 10)
await asyncio.sleep(waittime)
@shinkou
shinkou / aggcsv.py
Last active April 18, 2025 22:44
CSV Data Aggregator
#!/usr/bin/env python3
# -*- vim: set fileencoding=utf-8 ff=unix noet sw=4 ts=4 tw=0: -*-
import argparse, csv, re, sys
from functools import cmp_to_key
from itertools import groupby
from statistics import mean, median, mode, stdev
AGG = [
# int