Back (Current repo: scraps)

random scraps and notes that are useful to me
To clone this repository:
git clone https://git.viktor1993.net/scraps.git
Log | Download | Files | Refs

commit 3def0c99509cccc70f9ce24ef1d1fd295264041c
parent aedaf7e498a94e69623338dca6572c5f41c71b38
Author: root <root>
Date:   Mon, 21 Apr 2025 20:50:54 +0200

some more additions

Diffstat:
Abash/funcs/get_subprocesses.sh | 29+++++++++++++++++++++++++++++
Rbash/funcs/git-list-old-unmerged-branches-on-remote.sh -> bash/funcs/git_list_unmerged_branches.sh | 0
Ased/find_in_paragraph.sh | 26++++++++++++++++++++++++++
Msed/flatten_newlines.sh | 2+-
Msed/replace_match_and_next2.sh | 4++--
5 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/bash/funcs/get_subprocesses.sh b/bash/funcs/get_subprocesses.sh @@ -0,0 +1,29 @@ +#!/bin/bash +function get_subprocesses() { + + if [[ -z "$1" ]]; then + echo "Usage: $0 <PID>" + return 1 + fi + + function getpids() { + echo -n $1 " " + for sub in $(ps -o pid --no-headers --ppid "$1"); do + echo -n $sub $(getpids $sub) " " + done; + } + ps f $(getpids "$1") +} + + + # Explanation for those who wandered in here + # $(ps -o pid --no-headers --ppid $1) -> get all child process id's of the process id given by $1 + # recursively call getpids() on each child until we run out of descendant process id's + # finally, call ps f on the whole thing, to print a process family + + # This can be useful when a script might spawn many subprocesses, such as other scripts in parallel, + # sql connections, etc. it can be a way to check the status of a script i.e. how far down towards the + # script execution has reached + # It can also be useful to kill processes without leaving orphaned sub-processes, e.g. killing a script + # as well as any mysql connections it spawned -- in general, killing a bash script will not kill any + # spawned mysql processes diff --git a/bash/funcs/git-list-old-unmerged-branches-on-remote.sh b/bash/funcs/git_list_unmerged_branches.sh diff --git a/sed/find_in_paragraph.sh b/sed/find_in_paragraph.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +function find_in_paragraph() { + + PAT="$1" + FILE="$2" + + if [ -z $PAT ]; then + echo "missing pattern" + return 1; + fi + + if [ -z $FILE ]; then + echo "missing file" + return 1; + fi + + sed -e '/./{H;$!d;}' -e 'x;/'$PAT'/!d;' + + #Explanation for the random wanderer looking in here: + #'/./{H;$!d;}' -> append each line to the old space, and keep buffering, but only if the line is NOT blank + #x -> at a blank line (or end of file) swap butter into pattern space + #/'$PAT'/!d -> if $PAT is NOT in the paragraph, then delete the paragraph + + #if you want to match more stuff, just keep slapping more patterns into it -> 'x;/'$PAT'/!d;/'$PAT2'/!d;' etc. +} diff --git a/sed/flatten_newlines.sh b/sed/flatten_newlines.sh @@ -1,6 +1,6 @@ function flatten_newlines() { - FILE=$1 + FILE="$1" if [ -z $FILE ]; then echo "A file is needed" diff --git a/sed/replace_match_and_next2.sh b/sed/replace_match_and_next2.sh @@ -2,8 +2,8 @@ function replace_match_and_next2() { - PAT=$1 - FILE=$2 + PAT="$1" + FILE="$2" if [ -z $PAT ]; then echo "A pattern is needed";