Skip to content

Instantly share code, notes, and snippets.

View mohd-akram's full-sized avatar

Mohamed Akram mohd-akram

View GitHub Profile
@mohd-akram
mohd-akram / port-diff
Last active April 19, 2025 13:47
Compare the dependencies listed by a MacPorts port with the output of otool
#!/bin/sh
set -euo pipefail
usage() {
printf >&2 "usage: %s [-a] [portname]\n" "$(basename "$0")"
}
archive=
verbose=
@mohd-akram
mohd-akram / json.awk
Last active June 30, 2025 17:07
Extract a value from a JSON object or array in awk
#
# Extract a JSON value in an object:
#
# items = get_json_value(json, "payload.tree.items")
#
# Or in an array:
#
# while ((item = get_json_value(items, i++)))
# name = decode_json_string(get_json_value(item, "name"))
#
@mohd-akram
mohd-akram / console.js
Created June 10, 2024 11:48
A custom Node.js console that writes to stderr and the debug console
const inspector = require("inspector");
// See:
// https://github.com/nodejs/node/blob/v22.2.0/lib/internal/util/inspector.js#L83
// Wrap a console implemented by Node.js with features from the VM inspector
function wrapConsole(console) {
const inspectorConsole = inspector.console;
for (const key of Object.keys(inspectorConsole)) {
// If the console has the same method as the inspector console,
@mohd-akram
mohd-akram / main.c
Created May 30, 2024 09:01
Execute C files directly
//usr/bin/cc -o ${o=`mktemp`} "$0" && exec -a "$0" "$o" "$@"
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%s", argv[0]);
for (int i = 1; i < argc; i++)
printf(" %s", argv[i]);
printf("\n");
return 0;
@mohd-akram
mohd-akram / create-postfix
Last active May 13, 2024 16:58
Create a local Postfix instance
#!/bin/sh
set -eux
# Enable multi-instance operation
sudo postmulti -e init
mkdir -p ~/.config ~/.local/state/spool
# Create a new local instance
@mohd-akram
mohd-akram / forward
Created May 12, 2024 12:52
Forward a port from a server using SSH
#!/bin/sh
# usage: forward host port [localport]
forward() {
ssh -NL ${3-$2}:localhost:$2 $1
}
@mohd-akram
mohd-akram / lsopen
Created May 10, 2024 16:56
List open TCP and UDP ports
#!/bin/sh
# usage: lstcp [options] [port]
lstcp() (
while getopts : o; do opts="$opts -$OPTARG"; done; shift $((OPTIND-1))
lsof -i tcp"${1+:}$1" -s tcp:listen $opts
)
# usage: lsudp [options] [port]
lsudp() (
@mohd-akram
mohd-akram / port-revbump
Created October 9, 2023 14:51
Utility to bump revision of a list of ports (MacPorts)
#!/bin/sh
set -euo pipefail
root=
while getopts D: opt; do
case $opt in
D) root=$OPTARG ;;
?) exit 2
esac
done
@mohd-akram
mohd-akram / foreach2of.js
Last active September 11, 2023 11:00
jscodeshift mod to convert Array.forEach to for-of loop
/** @type {import('jscodeshift').Transform} */
module.exports = function (file, api) {
const j = api.jscodeshift;
const root = j(file.source);
return root
.find(j.CallExpression, { callee: { property: { name: "forEach" } } })
.replaceWith((path) => {
const call = path.value;
const callee = /** @type {import('jscodeshift').MemberExpression} */ (
@mohd-akram
mohd-akram / urls
Created August 3, 2022 19:09
Find URLs in a directory
#!/bin/sh
grep -ro "http[^ )>']*" "$1" | grep -v "[{}]" | cut -d: -f 2- | sort | uniq