Skip to content

Instantly share code, notes, and snippets.

@d630
d630 / x.sh
Created June 24, 2016 04:27
bash: how to get port number from the "port: 5432," line (no quotes)
# Nr. 1
p='port: 5432,'
echo ${p//[!0-9]/}
# Nr. 2
IFS=':, ' read -r _ p <<< "port: 5432,"
# Nr. 3
awk -F[,:] '{print $2+0}' <<< "port: 5432,"
@d630
d630 / x.sh
Last active November 3, 2016 00:38
bash: tmp variables while using declare -x +x
unset -v x y r
x=5 y=2 r="$x*$y*2" \
declare -x +x -i r;
declare -p x y r
@d630
d630 / x.sh
Last active September 21, 2016 20:54
bash: match PATTERN in tail -f and kill background process
# execute it with source
unalias tail
echo 1 > /tmp/TEST_LOG;
sleep 100 &
pid_bg=$!
i=0
while
@d630
d630 / x.sh
Last active October 7, 2016 20:08
bash: It's better to have many assoc. arrays with just a few keys instead of one with many keys
#!/usr/bin/env bash
AssocTest ()
{
create1 ()
{
declare -g -A Assoc
declare i
for ((i=${1?}; i <= ${2?}; i++))
@d630
d630 / x.sh
Last active October 27, 2016 16:39
bash: using subshells
% time for ((i=0; i < 1000; i++)); do \bash -c ':;:' ; done
real 0m4.710s
user 0m0.444s
sys 0m1.420s
% time for ((i=0; i < 1000; i++)); do \dash -c ':;:' ; done
real 0m3.047s
user 0m0.080s
@d630
d630 / x.sh
Last active November 2, 2016 15:27
bash: stack overflow, segmentation fault
% printf 'echo dylan?;' \; >/tmp/prize; for i in {0..32767}; do printf ':;'; done >> /tmp/prize
% stat /tmp/prize
File: '/tmp/prize'
Size: 65548 Blocks: 136 IO Block: 4096 regular file
Device: 801h/2049d Inode: 12320778 Links: 1
Access: (0640/-rw-r-----) Uid: ( 1000/ user1) Gid: ( 1000/ user1)
Access: 2016-11-02 13:42:52.864527834 +0000
Modify: 2016-11-02 13:46:12.501955930 +0000
Change: 2016-11-02 13:46:12.501955930 +0000
Birth: -
@d630
d630 / x.sh
Last active October 27, 2019 15:41
bash: bash: bash: bash:
#!/usr/bin/env bash
#
# No local scope, use command substitution, process
# substitution, regular pipes or execute external procedures/scripts.
#
# Use set -o allexport, if you like to be oldschool.
#mapfile -t -c 1 -C "printf '%.0s%s\n'" -u 7 "${FUNCNAME[1]%%.*}"
function fn.call {
@d630
d630 / x.md
Last active November 3, 2016 15:37
Essay: copy wanted

Looking for

David Korn, KSH - A Shell Programming Language

from:

                PPN: 303892498
@d630
d630 / x.sh
Last active November 10, 2016 22:47
bash: send stdout to a handler in a process substitution via exec and wait for it to be finished with processing input (SIGSTOP/SIGCONT)
#!/usr/bin/env bash
function setup {
typeset -g "$1" 2>/dev/null && {
typeset -n fd=$1;
((${!fd})) &&
exec {fd}>&-;
exec {fd}> >('handler');
};
};
@d630
d630 / x.sh
Last active June 4, 2022 00:44
posix shell: understanding command search and execution
#!/bin/sh
get_path () {
command -v "$1";
};
has_operator () {
case "$1" in
(*";"*|*"&"*|*"&&"*|*"||"*|*";;"*|*"("*|*")"*)
command \return 1;;