Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / jmesqry.py
Created March 15, 2024 05:40
JMESPath Evaluator
#!/usr/bin/env python3
# vim: fileencoding=utf-8
import argparse, jmespath, json, sys
def get_args():
parser = argparse.ArgumentParser(description='JMESPath Evaluator')
parser.add_argument('-i', '--indent', type=int, help='indent')
parser.add_argument(
'--ensure-ascii'
@shinkou
shinkou / timeconv.py
Last active March 21, 2024 02:19
Local/Remote Time Converter
#!/usr/bin/env python3
# vim: fileencoding=utf-8 ff=unix
from datetime import datetime
from zoneinfo import ZoneInfo
import argparse, os, time
def get_args():
parser = argparse.ArgumentParser(
description='Local/Remote Time Converter'
@shinkou
shinkou / mstposter.py
Last active April 24, 2024 12:34
MS Teams Poster
#!/usr/bin/env python3
# vim: fileencoding=utf-8 ff=unix
import argparse, json, requests, sys
def message(
webhook, msg
, links=None, facts=None, subtitle=None, title=None, themecolor=None
):
payload = {
@shinkou
shinkou / imv.py
Created February 19, 2024 21:18
Randomized File Renamer
#!/usr/bin/env python3
# vim: fileencoding=utf-8 ff=unix
from math import log
from random import randint
import argparse
import logging
import os
import re
import sys
@shinkou
shinkou / dst.sh
Created December 6, 2023 14:46
A snippet which finds out whether a given date is DST (Daylight Saving Time) or not
#!/bin/bash
# Reference Source
# https://www.nist.gov/pml/time-and-frequency-division/popular-links/daylight-saving-time-dst
function is_dst()
{
epoch_secs=$(date --date="$1" +%s)
src_yr=$(date --date="$1" +%Y)
mar1wday=$(date --date="$src_yr-03-01" +%w)
@shinkou
shinkou / lcsv.py
Last active September 2, 2023 02:14
A snippet which crops from CSVs with a very small memory footprint
#!/usr/bin/env python3
# vim: fileencoding=utf-8 ff=unix hls lcs=tab\:>. list noet sw=4 ts=4 tw=76
#
# How to use:
#
# Scenario 1 - show only the first 16 entries
# $ lcsv.py --limit 16 my_data.csv
#
# Scenario 2 - convert CSV which uses pipe characters "|" back to commas
# $ lcsv.py --output-delimiter=, --delimiter='|' my_data.csv
@shinkou
shinkou / unescape.py
Created May 12, 2022 18:22
A snippet which does what the unescape function does in Javascript
#!/usr/bin/env python3
# vim: fileencoding=utf-8 ff=unix lcs=tab\:>. list noet sw=4 ts=4 tw=0
#
# How to use:
# $ unescape.py 'Line 1\nLine 2\nLine 3\n' 'いち\nに\nさん\n'
#
import sys
def unescape(s):
return s.encode('raw_unicode_escape').decode('unicode_escape')
@shinkou
shinkou / McrouterTest.java
Last active April 22, 2021 14:36
Test connectivity of memcached clusters backed by Mcrouter
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Chun-Kwong Wong
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is