Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env bash
#
# The logic in the getpos function is stolen from
# http://stackoverflow.com/a/2575525
# Pass "row" or "col" as an argument to get their positions
getpos ()
{
exec </dev/tty
@d630
d630 / x.sh
Last active June 11, 2016 17:38
bash: PS as WORD in for-loop
for I in <(ls -1ld /) <(ls -l1d /)
do
exec 3<"$I" || exec 3<&-;
echo is:$I
o=$(cat 0<&3)
echo $o
done
@d630
d630 / x.sh
Last active June 13, 2016 09:36
bash: Indentation styles
# tab: 8
# Bourne/Korn/Horstmann
while ((x == 1))
do echo foo
echo bar
done
for ((i=0; i < 10; i++))
do if ((i % 2 == 0))
@d630
d630 / x.sh
Last active June 13, 2016 09:59
bash: Indentation
# 10
while ((x == 1))
do
echo foo
echo bar
done
for ((i=0; i < 10; i++))
do
if ((i % 2 == 0))
@d630
d630 / x.sh
Last active June 17, 2016 13:20
mksh: arithmetic compound command in function definition
fn() {
((1,1,1))
a=(f o o)
}
typeset -fp fn
@d630
d630 / x.sh
Last active June 17, 2016 13:34
zsh: nested function definition with reserved word "function" and arithmetic compound as body
fn () {
function fn ((1))
function fn ((1)) 2>/dev/null
}
functions fn
@d630
d630 / x.sh
Last active June 18, 2016 09:59
bash: multi stage pipelines with bash commands set in parentheses
for i in {1..10}
do
bash -c '
TIMEFORMAT=%3R
time (
printf "%d\n" {1..10000} \
| while read; do echo $REPLY done > /dev/null;
)
'
done \
@d630
d630 / x.sh
Created June 22, 2016 21:14
bash: read stdin from pipe into variable
% printf '%d\n' {1..10000} | { time a=$(</dev/stdin) ; echo "$a"; } | wc -c
real 0m0.027s
user 0m0.004s
sys 0m0.004s
48894
% printf '%d\n' {1..10000} | { time read -rd '' a; echo "$a"; } | wc -c
real 0m0.041s
@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 / reg.txt
Last active June 30, 2016 21:20
bash: Use all elements of all arrays in one for-clause; see https://forum.ubuntuusers.de/topic/verarbeitung-von-arrays/
TEST_A=(
'1a'
'2a'
'3a'
)
TEST_B=(
'1b'
'2b'
'3b'