Skip to content

Instantly share code, notes, and snippets.

View pmarreck's full-sized avatar

Peter Marreck pmarreck

  • formerly senior engineer @ desk.com, chief engineer @ thredup.com, software engineer @ lifebooker.com. Director of Engineering @ addigence.com, currently available
  • Long Island, NY
  • 23:58 (UTC -04:00)
  • X @pmarreck
  • LinkedIn in/petermarreck
View GitHub Profile
@pmarreck
pmarreck / coinflip.bash
Created February 9, 2022 23:40
Simplest way to simulate a coin flip or toss on the Linux command line (in Bash; may work in other shells)
# return code is either 0 (success) or 1 (fail), so you can use it straight-up in logical statements
coinflip() { return $(($RANDOM%2)); }
coinflip && echo "heads" || echo "tails"
@pmarreck
pmarreck / thanos_all_the_files.bash
Created February 8, 2022 18:47
destroy a random half of your files, Thanos-style, in linux or bash shell
# Wrote this on a whim. Consider it performance-art shell scripting.
# To really do this, set FFS=true, otherwise, only the paths to the files that would have been destroyed are printed.
# I take no responsibility for the use of this. Even writing it was scary, and I didn't test the whole thing (YET).
# You have been warned.
coinflip() { return $(($RANDOM%2)); }
thanos_logic() { while read -r line; do coinflip && echo "$line"; done; }
thanos_infinity_gauntlet_completed() {
@pmarreck
pmarreck / pman.bash
Created February 8, 2022 17:00
Shell function(s) to get a pretty man page viewed as a PDF up in a PDF viewer on (any?) Linux distro
# helper function
needs () {
local bin=$1;
shift;
command -v $bin > /dev/null 2>&1 || {
echo "I require $bin but it's not installed or in PATH; $*" 1>&2;
return 1
}
}
@pmarreck
pmarreck / ShowListeners.ps1
Created January 21, 2022 18:31
On Windows, use PowerShell to get a list of open ports and the process and service names (sometimes with paths) that are holding them open
# Script: ShowListeners.ps1
# Author: MotoX80 and Evgenij Smirnov on MS forums
# pmarreck note: If you get a security block, you may need to run the following first:
# Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
# (or use CurrentUser for the -Scope if you want it to apply outside the current session, less secure though)
# For more, see: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.2
$rpt = @()
$services = Get-CimInstance -Class Win32_Service -Filter 'State="Running"' | Select Name, ProcessID
$Listeners = Get-NetTCPConnection | Where-Object {$_.State -eq "Listen"}
@pmarreck
pmarreck / kick_homebrew.sh
Created January 6, 2022 17:35
Kick/fix homebrew
for tab in $(grep -l -o '[a-z0-9-]*proto' $(brew --cellar)/*/*/INSTALL_RECEIPT.json)
do
formula=$(basename $(dirname $(dirname $tab)))
brew remove --ignore-dependencies $formula
brew install $formula
done
@pmarreck
pmarreck / uninstall_brews.sh
Created January 6, 2022 17:19
uninstall all brews from homebrew
while [[ `brew list | wc -l` -ne 0 ]]; do
#Iterate over each installed package
for EACH in `brew list`; do
#Uninstall each package
brew uninstall --ignore-dependencies $EACH --force
done
done
@pmarreck
pmarreck / random_password.sh
Last active June 29, 2022 13:19
Generate random strings or dictionary words in Bash
#!/usr/bin/env bash
# get a random-character password
# First argument is password length
# Can override the default character set by passing in PWCHARSET=<charset> as env
randompass() {
# globbing & history expansion here is a pain, so we store its state, temp turn it off & restore it later
local maybeglob="$(shopt -po noglob histexpand)"
set -o noglob # turn off globbing
set +o histexpand # turn off history expansion
@pmarreck
pmarreck / aoc_1.exs
Last active December 4, 2021 04:56
possible AOC #1 solution in Elixir
defmodule Increases do
def count_increases(string) when is_binary(string) do
string
|> String.split("\n")
|> Enum.map(fn x -> Integer.parse(x) end)
|> Increases.count_increases
end
def count_increases(list) when is_list(list), do: count_increases(list, 0)
@pmarreck
pmarreck / capture_stdout_stderr_retcode_to_vars.bash
Created October 25, 2021 22:17
A bash function to capture the stdout, stderr and return code of any line of shell into separate name-provided variables all at once without using the filesystem
capture () {
local out_var out err_var err ret_var ret debugflag
debugflag=
if [ "$1" = "--debug" ]; then
debugflag=1
shift
fi
if [ "$#" -lt 4 ]; then
echo "Usage: capture [--debug] <stdoutvar> <stderrvar> <returncodevar> command [arg ...]"
return 1
@pmarreck
pmarreck / nothing_improbable_about_this.exs
Last active June 23, 2020 16:10
In which I demonstrate that googling a random number between 100 and 999 plus "new cases" having a result makes total sense
# So I did a little experiment in my language of choice to prove this is completely expected,
# I will walk you through it.
# My comments start with a #.
Interactive Elixir (1.10.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> set_of_all_possible_numbers = 100..999
100..999
iex(2)> list_of_all_possible_numbers = Enum.to_list(set_of_all_possible_numbers)
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,