Skip to content

Instantly share code, notes, and snippets.

@foot-gun
foot-gun / capabilities.sh
Last active June 3, 2018 16:57
Note on capabilities and the one-liner to allow binding to low-numbered ports.
# Linux capabilities allow root to add a subset of root's powers to a binary owned and run by a non-root user.
# Please note this applies to BINARIES, not scripts, and gets weird with user namespacing.
# http://man7.org/linux/man-pages/man7/capabilities.7.html
# Checking a binary's capailities:
getcap /path/to/program
# Removing all capabilities from a binary:
setcap -r /path/to/program
@foot-gun
foot-gun / port_to_PID.sh
Created April 22, 2018 23:50
What to do when netstat isn't available (or cutting it).
#!/bin/sh
# On Linux, netstat -tulpn neatly lays down current ports in use and PIDs using them.
# However, these flags are not available on OSX, and netstat is deprecated anyway!
# https://dougvitale.wordpress.com/2011/12/21/deprecated-linux-networking-commands-and-their-replacements/
# ss is part of the iproute2 package (which is in Arch's base group).
ss -tulpn
# On OS X High Sierra and above:
@foot-gun
foot-gun / multiline.sh
Created April 22, 2018 22:52
Multiline comment hacks in bash.
#!/bin/sh
# Bash does not come with neat ways to indicate multi-line comments;
# however, some clever hacks are possible.
# : is shorthand for true and true does not process any parameters (man true).
# Remember to double any occurrences of '.
: '
line 1
line 2 we''re going to try this eventually
line 3
@foot-gun
foot-gun / ternary.sh
Created April 22, 2018 21:45
Bash ternary operator implementations
# The ternary operator is a neat trick in other languages (such as C++)
# which is a short form of if/then/else, returning the appropriate value.
# variable_name = (condition) ? value_if_true : value_if false;
# Bash does not provide this natively; here are a few implementations of
# int a = (b == 5) ? c : d;
# Cases (like switch statements):
case "$b" in
5) a=$c ;;
*) a=$d ;;
@foot-gun
foot-gun / newlines.md
Last active April 22, 2018 21:20
Notes on newlines.

DOS text files traditionally have carriage return and line feed pairs as their newline characters while Unix text files have the line feed as their newline character.

Convert DOS to Unix:

tr -d '\r' < input.file > output.file

or, depending on your sed flavor:

sed 's/^M$//' input.txt > output.txt
@foot-gun
foot-gun / lower_upper.sh
Last active April 17, 2018 01:32
A few ways to convert strings between upper and lower case.
#!/bin/sh
# In general, use translate (tr). It knows about several character sets.
tr '[:upper:]' '[:lower:]' < input.txt > output.txt
for i in *; do mv $i `echo $i | tr [:upper:] [:lower:]`; done
# It can also be fed ranges of characters like so.
tr '[A-Z]' '[a-z]' < $fileName
# Bash version 4.x+ has upper and lower built-in: ^^ ,,
y="thIs Is A tEst"
echo "${y^^}"
@foot-gun
foot-gun / three_fingered_claw.sh
Created April 15, 2018 23:47
Three-fingered claw trick for shell scripting.
#!/bin/sh
# From SO, of course:
# https://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs
# These functions are *NIX OS and shell flavor-robust.
# Put them at the beginning of your script (bash or otherwise), try() your statement and code on.
#
# yell: print the script name ($0) and all arguments ($*) to stderr/file descriptor 2 (>&2).
# die: calls yell, then exits with a non-0 exit status, indicating failure.
# try: uses boolean OR (||), which only evaluates the right side if the left one didn’t fail.
# $@ is all arguments again, but individually quoted (not as a single-argument blob).
@foot-gun
foot-gun / keybase.md
Created October 26, 2017 05:30
Keybase identity proof

Keybase proof

I hereby claim:

  • I am murphdasmurf on github.
  • I am citixen (https://keybase.io/citixen) on keybase.
  • I have a public key whose fingerprint is 97F1 10A5 9DBE 83EF BCAE 2CE5 8DFC 1200 B6EE E683

To claim this, I am signing this object: