From fef4d6ff2ad9f48e6dccde0f061453e6a3ac624e Mon Sep 17 00:00:00 2001 From: johannst Date: Sun, 19 Apr 2020 22:13:44 +0200 Subject: added new hierarchy --- src/misc/README.md | 0 src/misc/awk.md | 126 +++++++++++++++++++++++++++++++++++++++ src/misc/bash.md | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/misc/emacs.md | 86 ++++++++++++++++++++++++++ src/misc/fish.md | 16 +++++ src/misc/gdb.md | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ src/misc/git.md | 129 +++++++++++++++++++++++++++++++++++++++ src/misc/radare2.md | 27 +++++++++ src/misc/tmux.md | 110 ++++++++++++++++++++++++++++++++++ src/misc/zsh.md | 125 ++++++++++++++++++++++++++++++++++++++ 10 files changed, 950 insertions(+) create mode 100644 src/misc/README.md create mode 100644 src/misc/awk.md create mode 100644 src/misc/bash.md create mode 100644 src/misc/emacs.md create mode 100644 src/misc/fish.md create mode 100644 src/misc/gdb.md create mode 100644 src/misc/git.md create mode 100644 src/misc/radare2.md create mode 100644 src/misc/tmux.md create mode 100644 src/misc/zsh.md (limited to 'src/misc') diff --git a/src/misc/README.md b/src/misc/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/misc/awk.md b/src/misc/awk.md new file mode 100644 index 0000000..38b0cb1 --- /dev/null +++ b/src/misc/awk.md @@ -0,0 +1,126 @@ +# awk(1) + +```markdown +awk [opt] program [input] + -F field separator string (can be regex) + program awk program + input file or stdin if not file given +``` + +## Input processing + +Input is processed in two stages: +1. Splitting input into a sequence of `records`. + By default split at `newline` character, but can be changed via the + builtin `RS` variable. +2. Splitting a `record` into `fields`. By default strings without `whitespace`, + but can be changed via the builtin variable `FS` or command line option + `-F`. + +Fields are accessed as follows: +- `$0` whole `record` +- `$1` field one +- `$2` field two +- ... + +## Program + +An `awk` program is composed of pairs of the form: +```markdown +pattern { action } +``` +The program is run against each `record` in the input stream. If a `pattern` +matches a `record` the corresponding `action` is executed and can access the +`fields`. + +```markdown +INPUT + | + v +record ----> ∀ pattern matched + | | + v v +fields ----> run associated action +``` + +Any valid awk `expr` can be a `pattern`. + +### Special pattern + +awk provides two special patterns, `BEGIN` and `END`, which can be used +multiple times. Actions with those patterns are **executed exactly once**. +- `BEGIN` actions are run before processing the first record +- `END` actions are run after processing the last record + +### Special variables + +- `RS` _record separator_: first char is the record separator, by default + +- `FS` _field separator_: regex to split records into fields, by default + +- `NR` _number record_: number of current record + +### Special statements & functions + +- `printf "fmt", args...` + + Print format string, args are comma separated. + - `%s` string + - `%d` decimal + - `%x` hex + - `%f` float + + Width can be specified as `%Ns`, this reserves `N` chars for a string. + For floats one can use `%N.Mf`, `N` is the total number including `.` and + `M`. + +- `strftime("fmt")` + + Print time stamp formatted by `fmt`. + - `%Y` full year (eg 2020) + - `%m` month (01-12) + - `%d` day (01-31) + - `%F` alias for `%Y-%m-%d` + - `%H` hour (00-23) + - `%M` minute (00-59) + - `%S` second (00-59) + - `%T` alias for `%H:%M:%S` + + +## Examples + +### Filter records +```bash +awk 'NR%2 == 0 { print $0 }' +``` +The pattern `NR%2 == 0` matches every second record and the action `{ print $0 }` +prints the whole record. + +### Capture in variables +```bash +# /proc//status +# Name: cat +# ... +# VmRSS: 516 kB +# ... + +for f in /proc/*/status; do + cat $f | awk ' + /^VmRSS/ { rss = $2/1024 } + /^Name/ { name = $2 } + END { printf "%16s %6d MB\n", name, rss }'; +done | sort -k2 -n +``` +We capture values from `VmRSS` and `Name` into variables and print them at the +`END` once processing all records is done. + +### Run shell command and capture output +```bash +cat /proc/1/status | awk ' + /^Pid/ { + "ps --no-header -o user " $2 | getline user; + print user + }' +``` +We build a `ps` command line and capture the first line of the processes output +in the `user` variable and then print it. diff --git a/src/misc/bash.md b/src/misc/bash.md new file mode 100644 index 0000000..a4df7da --- /dev/null +++ b/src/misc/bash.md @@ -0,0 +1,169 @@ +# bash(1) + +## Expansion + +### Generator + +```bash +# generate sequence from n to m +{n..m} +# generate sequence from n to m step by s +{n..m..s} + +# expand cartesian product +{a,b}{c,d} +``` + +### Parameter +```bash +# default param +bar=${foo:-some_val} # if $foo set, then bar=$foo else bar=some_val + +# check param set +bar=${foo:?msg} # if $foo set, then bar=$foo else exit and print msg + +# indirect +FOO=foo +BAR=FOO +bar=${!BAR} # deref value of BAR -> bar=$FOO + +# prefix +${foo#prefix} # remove prefix when expanding $foo +# suffix +${foo%suffix} # remove suffix when expanding $foo + +# substitute +${foo/pattern/string} # replace pattern with string when expanding foo +# pattern starts with +# '/' replace all occurences of pattern +# '#' pattern match at beginning +# '%' pattern match at end +``` + +> Note: `prefix`/`suffix`/`pattern` are expanded as [pathnames](#pathname). + +### Pathname + +```bash +* match any string +? match any single char +\\ match backslash +[abc] match any char of 'a' 'b' 'c' +[a-z] match any char between 'a' - 'z' +[^ab] negate, match all not 'a' 'b' +[:class:] match any char in class, available: + alnum,alpha,ascii,blank,cntrl,digit,graph,lower, + print,punct,space,upper,word,xdigit +``` + +Wit `extglob` shell option enabled it is possible to have more powerful +patterns. In the following `pattern-list` is one ore more patterns separated +by `|` char. + +```bash +?(pattern-list) matches zero or one occurrence of the given patterns +*(pattern-list) matches zero or more occurrences of the given patterns ++(pattern-list) matches one or more occurrences of the given patterns +@(pattern-list) matches one of the given patterns +!(pattern-list) matches anything except one of the given patterns +``` +> Note: `shopt -s extglob`/`shopt -u extglob` to enable/disable `extglob` +> option. + +## I/O redirection + +> Note: The trick with bash I/O redirection is to interpret from left-to-right. + +```bash +# stdout & stderr to file +command >file 2>&1 +# equivalent +command &>file + +# stderr to stdout & stdout to file +command 2>&1 >file +``` + +### Explanation + +```bash +j>&i +``` +Duplicate `fd i` to `fd j`, making `j` a copy of `i`. See [dup2(2)][dup2]. + +Example: +```bash +command 2>&1 >file +``` +1. duplicate `fd 1` to `fd 2`, effectively redirecting `stderr` to `stdout` +2. redirect `stdout` to `file` + +## Completion + +The `complete` builtin is used to interact with the completion system. +```bash +complete # print currently installed completion handler +complete -F # install as completion handler for +complete -r # uninstall completion handler for +``` + +Variables available in completion functions: +```bash +# in +$1 # +$2 # current word +$3 # privous word + +COMP_WORDS # array with current command line words +COMP_CWORD # index into COMP_WORDS with current cursor position + +# out +COMPREPLY # array with possible completions +``` + +The `compgen` builtin is used to generate possible matches by comparing `word` +against words generated by `option`. +```bash +compgen [option] [word] + +# usefule options: +# -W specify list of possible completions +# -d generate list with dirs +# -f generate list with files +# -u generate list with users +# -e generate list with exported variables + +# compare "f" against words "foo" "foobar" "bar" and generate matches +compgen -W "foo foobar bar" "f" + +# compare "hom" against file/dir names and generate matches +compgen -d -f "hom" +``` + +### Example +Skeleton to copy/paste for writing simple completions. + +Assume a program `foo` with the following interface: +```bash +foo -c green|red|blue -s low|high -f -h +``` + +The completion handler could be implemented as follows: +```bash +function _foo() { + local curr=$2 + local prev=$3 + + local opts="-c -s -f -h" + case $prev in + -c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );; + -s) COMPREPLY=( $(compgen -W "low high" -- $curr) );; + -f) COMPREPLY=( $(compgen -f -- $curr) );; + *) COMPREPLY=( $(compgen -W "$opts" -- $curr) );; + esac +} + +complete -F _foo foo +``` + +[dup2]: http://man7.org/linux/man-pages/man2/dup.2.html diff --git a/src/misc/emacs.md b/src/misc/emacs.md new file mode 100644 index 0000000..b288fb1 --- /dev/null +++ b/src/misc/emacs.md @@ -0,0 +1,86 @@ +# emacs(1) + +## help +```markdown + C-h ? list available help modes + C-h f describe function + C-h v describe variable + C-h c print command bound to + C-h k describe command bound to + C-h b list buffer local key-bindings + C-h list possible key-bindings with + eg C-x C-h -> list key-bindings beginning with C-x +``` + +## package manager +```markdown + package-refresh-contents refresh package list + package-list-packages list available/installed packages +``` + +## window +```markdown + C-x 0 kill focused window + C-x 1 kill all other windows + C-x 2 split horizontal + C-x 3 split vertical +``` + +## yank/paste +```markdown + C- set start mark to select text + M-w copy selected text + C-w kill selected text + C-y paste selected text + M-y cycle through kill-ring +``` + +## block/rect +```markdown + C-x activate rectangle-mark-mode + M-x string-rectangle insert text in marked rect +``` + +## mass edit +```makrdown + C-x h mark whole buffer (mark-whole-buffer) + M-x delete-matching-line delete lines matching regex + M-x % search & replace region (query-replace) + C-M-x % search & replace regex (query-replace-regexp) +``` + +## grep +```markdown + M-x find-grep run find-grep result in *grep* buffer + n/p navigate next/previous match in *grep* buffer +``` + +## lisp mode +```markdown + M-x lisp-interaction-mode activate lisp mode + C-M-x evaluate top expr under cursor + C-x C-e eval-last-sexp + C-u C-x C-e eval-last-sexp and prints result in current buffer +``` + +## narrow +```markdown + C-x n n show only focused region (narrow) + C-x n w show whole buffer (wide) +``` + +## org +```markdown + M-up/M-down re-arrange items in same hierarchy + M-left/M-right change item hierarchy + C-RET create new item below current + C-S-RET create new TODO item below current + S-left/S-right cycle TODO states +``` + +### org source +```markdown + &1 | less;' to current cmdline +``` + +## debug +```markdown + status print-stack-trace .. prints function stacktrace (can be used in scripts) + breakpoint ................ halt script execution and gives shell (C-d | exit + to continue) +``` diff --git a/src/misc/gdb.md b/src/misc/gdb.md new file mode 100644 index 0000000..7a43ca1 --- /dev/null +++ b/src/misc/gdb.md @@ -0,0 +1,162 @@ +# gdb(1) + +# CLI + +```markdown + gdb [opts] [prg [-c coredump | -p pid]] + gdb [opts] --args prg + opts: + -p attach to pid + -c use + -x execute script before prompt + -ex execute command before prompt + --tty set I/O tty for debugee +``` + +# Interactive usage + +```markdown + tty + Set as tty for debugee. + Make sure nobody reads from target tty, easiest is to spawn a shell + and run following in target tty: + > while true; do sleep 1024; done + + set follow-fork-mode + Specify which process to follow when debuggee makes a fork(2) + syscall. + + sharedlibrary [] + Load symbols of shared libs loaded by debugee. Optionally use + to filter libs for symbol loading. + + break [-qualified] thread + Set a breakpoint only for a specific thread. + -qualified: Tred as fully qualified symbol (quiet handy to set + breakpoints on C symbols in C++ contexts) + + rbreak + Set breakpoints matching , where matching internally is done + on: .*.* + + command [] + Define commands to run after breakpoint hit. If is not + specified attach command to last created breakpoint. Command block + terminated with 'end' token. + + : Space separates list, eg 'command 2 5-8' to run command + for breakpoints: 2,5,6,7,8. + + info functions [] + List functions matching . List all functions if no + provided. + + info variables [] + List variables matching . List all variables if no + provided. + + info handle [] + Print how to handle . If no specified print for all + signals. + + handle + Configure how gdb handles sent to debugee. + : + stop/nostop Catch signal in gdb and break. + print/noprint Print message when gdb catches signal. + pass/nopass Pass signal down to debugee. + + catch signal + Create a catchpoint for . +``` + +# User commands (macros) + +Gdb allows to create & document user commands as follows: +```markdown + define + # cmds + end + + document + # docu + end +``` + +To get all user commands or documentations one can use: +```markdown + help user-defined + help +``` + +# Hooks + +Gdb allows to create two types of command `hooks` +- `hook-` will be run before `` +- `hookpost-` will be run after `` +```markdown + define hook- + # cmds + end + + define hookpost- + # cmds + end +``` + +# Examples + +## Catch SIGSEGV and execute commands +This creates a `catchpoint` for the `SIGSEGV` signal and attached the `command` +to it. +```markdown + catch signal SIGSEGV + command + bt + c + end +``` + +## Run `backtrace` on thread 1 (batch mode) +```markdown + gdb --batch -ex 'thread 1' -ex 'bt' -p +``` + +## Script gdb for automating debugging sessions +To script gdb add commands into a file and pass it to gdb via `-x`. +For example create `run.gdb`: +```markdown + set pagination off + + break mmap + command + info reg rdi rsi rdx + bt + c + end + + #initial drop + c +``` +This script can be used as: +```markdown + gdb --batch -x ./run.gdb -p +``` + +# Know Bugs + +## Workaround `command + finish` bug +When using `finish` inside a `command` block, commands after `finish` are not +executed. To workaround that bug one can create a wrapper function which calls +`finish`. +```markdown + define handler + bt + finish + info reg rax + end + + command + handler + end +``` diff --git a/src/misc/git.md b/src/misc/git.md new file mode 100644 index 0000000..d76ae77 --- /dev/null +++ b/src/misc/git.md @@ -0,0 +1,129 @@ +# git(1) + +## staging +```markdown + git add -p [] ............ partial staging (interactive) +``` + +## Remote +```markdown + git remote -v .................. list remotes verbose (with URLs) + git remote show [-n] .. list info for (like remote HEAD, + remote branches, tracking mapping) +``` + +## Branching +```markdown + git branch [-a] ................ list available branches; -a to include + remote branches + git branch -vv ................. list branch & annotate with head sha1 & + remote tracking branch + git branch ............. create branch with name + git checkout ........... switch to branch with name + git push -u origin .... push branch to origin (or other remote), and + setup as tracking branch +``` + +## Resetting +```markdown + git reset [opt] + opt: + --mixed .................... resets index, but not working tree + --hard ..................... matches the working tree and index to that + of the tree being switched to any changes to + tracked files in the working tree since + are lost + git reset HEAD .......... remove file from staging + git reset --soft HEAD~1 ........ delete most recent commit but keep work + git reset --hard HEAD~1 ........ delete most recent commit and delete work +``` + +## Tags +```markdown + git tag -a -m "descr" ........ creates an annotated tag (full object + containing tagger, date, ...) + git tag -l ........................... list available tags + git checkout tag/ ............. checkout specific tag + git checkout tag/ -b .. checkout specific tag in a new branch +``` + +## Diff +```markdown + git diff HEAD: origin/HEAD: ... diff files for different refs + git diff -U$(wc -l ) ......... shows complete file with diffs + instead of usual diff snippets +``` + +## Log +```markdown + git log --oneline .... shows log in single line per commit -> alias for + '--pretty=oneline --abbrev-commit' + git log --graph ...... text based graph of commit history + git log --decorate ... decorate log with REFs +``` + +## File history +```markdown + git log -p ......... show commit history + diffs for + git log --oneline .. show commit history for in compact format +``` + +## Patching +```markdown + git format-patch / + opt: + -N ................... use [PATCH] instead [PATCH n/m] in subject when + generating patch description (for patches spanning + multiple commits) + --start-number ... start output file generation with as start + number instead '1' + since spcifier: + -3 .................. e.g: create a patch from last three commits + ....... create patch with commits starting after + + git am ......... apply patch and create a commit for it + + git apply --stat ... see which files the patch would change + git apply --check .. see if the patch can be applied cleanly + git apply .......... apply the patch locally without creating a commit + + # eg: generate patches for each commit from initial commit on + git format-patch -N $(git rev-list --max-parents=0 HEAD) + + # generate single patch file from a certain commit/ref + git format-patch --stdout > my-patch.patch +``` + +## Submodules +```markdown + git submodule add [] .......... add new submodule to current project + git clone --recursive ............... clone project and recursively all + submodules (same as using + 'git submodule update --init + --recursive' after clone) + git submodule update --init --recursive ... checkout submodules recursively + using the commit listed in the + super-project (in detached HEAD) + git submodule update --remote .... fetch & merge remote changes for + , this will pull + origin/HEAD or a branch specified + for the submodule + git diff --submodule ...................... show commits that are part of the + submodule diff +``` + +## Inspection +```markdown + git ls-tree [-r] .... show git tree for , -r to recursively ls sub-trees + git show ............ show + git cat-file -p ..... print content of +``` + +## Revision Specifier +```markdown + HEAD ........ last commit + HEAD~1 ...... last commit-1 + HEAD~N ...... last commit-N (linear backwards when in tree structure, check + difference between HEAD^ and HEAD~) + git rev-list --max-parents=0 HEAD ........... first commit +``` diff --git a/src/misc/radare2.md b/src/misc/radare2.md new file mode 100644 index 0000000..673c911 --- /dev/null +++ b/src/misc/radare2.md @@ -0,0 +1,27 @@ +# radare2(1) + +## print +```markdown + + pd [@ ] # print disassembly for instructions + # with optional temporary seek to +``` + +## flags +```markdown + fs # list flag-spaces + fs # select flag-space + f # print flags of selected flag-space +``` + +## help +```markdown + ?*~ # '?*' list all commands and '~' grep for + ?*~... # '..' less mode /'...' interactive search +``` + +## relocation +```markdown + > r2 -B # open mapped to addr + oob # reopen current file at +``` diff --git a/src/misc/tmux.md b/src/misc/tmux.md new file mode 100644 index 0000000..04b1111 --- /dev/null +++ b/src/misc/tmux.md @@ -0,0 +1,110 @@ +# tmux(1) + +Terminology: +- `session` is a collection of pseudo terminals which can have multiple + `windows` +- `window` uses the entire screen and can be split into rectangular `panes` +- `pane` is a single pseudo terminal instance + +# Tmux cli +```markdown +# Session +tmux creates new session +tmux ls list running sessions +tmux kill-session -t kill running session +tmux attach -t [-d] attach to session , detach other clients [-d] +tmux detach -s detach all clients from session + +# Environment +tmux showenv -g show global tmux environment variables +tmux setenv -g set variable in global tmux env + +# Misc +tmux source-file source config +tmux lscm list available tmux commnds +tmux show -g show global tmux options +tmux display display message in tmux status line +``` +## Scripting +```markdown +# Session +tmux list-sessions -F '#S' list running sessions, only IDs + +# Window +tmux list-windows -F '#I' -t list window IDs for session +tmux selectw -t : select window in session + +# Pane +tmux list-panes -F '#P' -t : list pane IDs for window in session +tmux selectp -t :.

select pane

in window in session + +# Run commands +tmux send -t :.

"ls" C-m send cmds/keys to pane +tmux run -t

run shell command in background and report output on pane -t

+``` + +For example cycle through all panes in all windows in all sessions: +```bash +# bash +for s in $(tmux list-sessions -F '#S'); do + for w in $(tmux list-windows -F '#I' -t $s); do + for p in $(tmux list-panes -F '#P' -t $s:$w); do + echo $s:$w.$p + done + done +done +``` + +# Bindings + +```markdown +prefix d detach from current session +prefix c create new window +prefix w open window list +prefix $ rename session +prefix , rename window +prefix . move current window +``` + +Following bindings are specific to my [`tmux.conf`](https://github.com/johannst/dotfiles/blob/master/tmux.conf): +```markdown +C-s prefix + +# Panes +prefix s horizontal split +prefix v vertical split +prefix f toggle maximize/minimize current pane + +# Movement +prefix Tab toggle between window + +prefix h move to pane left +prefix j move to pane down +prefix k move to pane up +prefix l move to pane right + +# Resize +prefix C-h resize pane left +prefix C-j resize pane down +prefix C-k resize pane up +prefix C-l resize pane right + +# Copy/Paste +prefix C-v enter copy mode +prefix C-p paste yanked text +prefix C-b open copy-buffer list + +# In Copy Mode +v enable visual mode +y yank selected text +``` + +# Command mode + +To enter command mode `prefix :`. + +Some useful commands are: +```markdown +setw synchronize-panes on/off enables/disables synchronized input to all panes +list-keys -t vi-copy list keymaps for vi-copy mode +``` diff --git a/src/misc/zsh.md b/src/misc/zsh.md new file mode 100644 index 0000000..ae2a4bc --- /dev/null +++ b/src/misc/zsh.md @@ -0,0 +1,125 @@ +# zsh(1) + +## Keybindings + +Change input mode: +```zsh +bindkey -v change to vi keymap +bindkey -e change to emacs keymap +``` + +Define key-mappings: +```zsh +bindkey list mappings in current keymap +bindkey in-str cmd create mapping for `in-str` to `cmd` +bindkey -r in-str remove binding for `in-str` + +# C-v dump code, which can be used in `in-str` +# zle -l list all functions for keybindings +# man zshzle(1) STANDARD WIDGETS: get description of functions +``` + +## Completion + +### Installation + +Completion functions are provided via files and need to be placed in a location +covered by `$fpath`. By convention the completion files are names as `_`. + +A completion skeleton for the command `foo`, stored in `_foo` +```zsh +#compdef _foo foo + +function _foo() { + ... +} +``` + +Alternatively one can install a completion function explicitly by calling `compdef `. + +### Completion Variables + +Following variables are available in Completion functions: +```zsh +$words # array with command line in words +$#words # number words +$CURRENT # index into $words for cursor position +$words[CURRENT-1] # previous word (relative to cursor position) +``` + +### Completion Functions +- `_describe` simple completion, just words + description +- `_arguments` sophisticated completion, allow to specify actions + +#### Completion with [`_describe`][zsh-comp-utils] +```zsh +_describe MSG COMP +``` +- `MSG` simple string with header message +- `COMP` array of completions where each entry is `"opt:description"` + +```zsh +function _foo() { + local -a opts + opts=('bla:desc for bla' 'blu:desc for blu') + _describe 'foo-msg' opts +} +compdef _foo foo + +foo + -- foo-msg -- +bla -- desc for bla +blu -- desc for blu +``` + +#### Completion with [`_arguments`][zsh-comp-utils] +```zsh +_arguments SPEC [SPEC...] +``` +where `SPEC` can have one of the following forms: +- `OPT[DESC]:MSG:ACTION` +- `N:MSG:ACTION` + +Available actions +```zsh +(op1 op2) list possible matches +->VAL set $state=VAL and continue, `$state` can be checked later in switch case +FUNC call func to generate matches +{STR} evaluate `STR` to generate matches +``` + +### Example +Skeleton to copy/paste for writing simple completions. + +Assume a program `foo` with the following interface: +```zsh +foo -c green|red|blue -s low|high -f -d

-h +``` + +The completion handler could be implemented as follows in a file called `_foo`: +```zsh +#compdef _foo foo + +function _foo_color() { + local colors=() + colors+=('green:green color') + colors+=('red:red color') + colors+=('blue:blue color') + _describe "color" colors +} + +function _foo() { + _arguments \ + "-c[define color]:color:->s_color" \ + "-s[select sound]:color:(low high)" \ + "-f[select file]:file:_files" \ + "-d[select dir]:fir:_files -/" \ + "-h[help]" + + case $state in + s_color) _foo_color;; + esac +} +``` + +[zsh-comp-utils]: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions -- cgit v1.2.3