Skip to content

Instantly share code, notes, and snippets.

@estysdesu
estysdesu / tmuxColors.sh
Last active March 25, 2020 20:31
[Tmux: View Colours] #tmux #sh #colour
#!/usr/bin/env bash
# https://github.com/estysdesu/dotFiles/blob/master/bin/tmuxColour
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"
done
@estysdesu
estysdesu / controlFlow.sh
Last active July 21, 2019 06:31
[Shell: classic control flow (conditionals, loops)] #if #case #then #sh #while #for #if #case #switch #else
##### if, elif, else, fi (CONDITIONAL) #####
# https://www.netacad.com/courses/os-it/ndg-linux-essentials --> Ch11: Basic Scripting
if [ "$1" = "hello" ]; then # square brackets are same as test command `test "$1" = "hello"`
echo "hello yourself"
elif [ "$1" = "goodbye" ]; then
echo "nice to have met you"
echo "I hope to see you again"
else
echo "I didn't understand that"
fi
@estysdesu
estysdesu / regex.sh
Last active July 21, 2019 06:03
[Shell: regex (regular expression) patterns] #regex #sh #character #classes
##### REGEX CHARACTERS/GLOBS #####
. # match any one single character
* # match the previous character repeated 0+ times
? # match the previous character repeated 0 or 1 time (extended pattern `-E`)
+ # match the previous character repeated 1+ times (extended pattern `-E`)
{<num>} # match the previous character repeated the number of times specificed in the braces (extended pattern `-E`)
[<chars>] # character range to match one time; check `ascii`
[^<chars>] # character range to not match one time; check `ascii`
^ # if first char of pattern, pattern must be at start of line to match
$ # if last char of pattern, pattern must be at end of line to match
@estysdesu
estysdesu / mod.sh
Last active July 21, 2019 08:54
[Shell: text/file modification] #grep #tr #sed #awk #cut #sort #sh #chmod
##### tr (TRANSLATE/DELETE) #####
# tr <pattern-translate-in> <pattern-translate-out>
# -d: delete SET1
# -t: truncate SET1 to length of SET2
# -c: compliment SET1
# -s: squeeze the repitition SET1
tr 'a-z' 'A-Z' < <file> # converts input to all caps
tr -d ' ' < <file># delete spaces
cat <file> | tr -cd [:digit:] # delete all but digits
@estysdesu
estysdesu / text.sh
Last active July 21, 2019 06:04
[Shell: view and work with file contents] #more #less #cat #head #tail #wc #sh
##### cat (SHOW ALL FILE CONTENTS) #####
cat <file> # shows all file contents
cat <enter>...<ctrl+d> # echos whatever is typed after enter (useful for redirecting stdin)
##### less/more (PAGERS) #####
less <file> # more advanced version of `more`; allows vim navigation
##### head/tail (PARTIAL CONTENTS) #####
# Normal indexing (GNU/UNIX)
head -n 5 <file> # first 5 lines (defaults to 10) == `head -5 <file>`
@estysdesu
estysdesu / zipper.sh
Last active July 21, 2019 05:58
[Shell: file compression and archiving] #tar #gzip #sh #bzip #xz #zip
##### tar (ARCHIVING) #####
# -z: gzip
# -j: bzip2
# -J: xz
# if compression method is missing, tar tries to guess compression method (from extension)
# -c: create
# -x: extract
# -v: verbose (list each file)
# -f: archive file name
# -r: append to unzipped archive
@estysdesu
estysdesu / with.py
Last active July 29, 2019 01:20
[Python: `with` context manager] #with #python
# https://effbot.org/zone/python-with-statement.htm
class Tyler():
def __enter__(self):
# <set-up>
# return <something> # essentially "with this state thats returned, do something"
def __exit__(self):
# <tear-down>
with Tyler() as ty:
# <do-something-with-ty>
@estysdesu
estysdesu / test_cond.sh
Last active July 21, 2019 06:02
[Shell: test conditionals] #test #conditionals
##### test OR [ -s/-n/-t/(etc...) ] (TEST CONDITIONALS) #####
# https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
# numeric and string are different for numbers -- `test 01 -eq 1` is true, but `test "01" == "1"` is obviously not
# -f: file exists
# -s: file exists and has size > 0
# ! <-flag>: negate
# -d: directory exists
# -x: can execute
# <#> -eq <#>: numeric equality; good for exit codes
# <#> -ne <#>: numeric inequality (test if not equal)
@estysdesu
estysdesu / fd.sh
Last active July 21, 2019 08:54
[Shell: file descriptors and file directors] #fd #file #descriptors #sh #read
# https://thoughtbot.com/blog/input-output-redirection-in-the-shell
# http://mywiki.wooledge.org/BashSheet#Streams
# http://www.learnlinux.org.za/courses/build/shell-scripting/ch01s04.html
##### fd (FILE DESCRIPTORS) #####
# 0 == stdin
# 1 == stdout
# 2 == stderr
# & == pointer (`2>&1`)
# &> == `2> <file> 1> <file>` (file direction)
@estysdesu
estysdesu / chrome_web_app.sh
Last active July 21, 2019 05:52
[Chrome App: macOS windowed Chrome web app without all the junk] #chrome #app #windowed #web #client #sh
#!/usr/bin/env bash
# https://github.com/estysdesu/dotFiles/blob/master/bash/.bash_profile
# one-liner
alias spotify_web="'`locate "*Google\ Chrome"`' --app="https://play.spotify.com"" # Spotify web client without all the junk
# functional
chrome_app=`locate "*Google\ Chrome"`
web_app () {
"$chrome_app" --app="$1"