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 18, 2025 05:19
Compare the dependencies listed by a MacPorts port with the output of otool
#!/bin/sh
# usage: port-diff [portname]
{
port -q deps --no-build "$1" | awk -F': ' '/Lib/{gsub(/, /,"\n");print $NF}'
echo
port -q contents "$1" | xargs otool -L 2>/dev/null |
grep -E '\.(dylib|framework)' | xargs port -q provides |
awk -v name="$1" '$1!=name{print $1}' | sort | uniq
} | awk '
/^$/{ c = 1; next }
@mohd-akram
mohd-akram / json.awk
Created March 9, 2025 08:29
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 / port-links
Created December 2, 2024 18:33
Check if a MacPorts port links against a particular library
@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 / gh-ssh
Last active April 17, 2024 09:13
Utility to handle creating and using GitHub deploy keys
#!/bin/sh
set -euo pipefail
dir=~/.ssh/github.com
name=$(basename "$0")
help=$(printf "\
usage: %s [-d] [repo]
@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