Posts

Showing posts from November, 2017

Cursor placement with tput

# A crazy clock one liner: clear; while :; do tput cup $(($RANDOM%32)) $(($RANDOM%32)); date;sleep 1; done ...                        Wed 15 Nov 2017 07:55:28 GMT  Wed 15 Nov 2017 07:55:25 GMT:55:21 GMTv 2017 07:55:19 GMT                 Wed 15 Nov 2017 07:55:31 GMT       Wed 15 Nov 2017 Wed 15 Nov 2017 07:55:18 GMT               Wed 15 Nov 2017 07:55:20 GMT     Wed 15 Nov 2017 07:55:26 GMT                      Wed 15 Nov 2017 07:55:17 GMTMT                           ...

print, like echo but simpler and more consistent than oche

# oche (see previous post) was OK, but it still had some consistency issues around mutli-line input. function print { local line="$@"; printf "%s\n" "$line"; } Alexanders-MacBook-Pro:~ alexanderturner$ print a bc a bc Alexanders-MacBook-Pro:~ alexanderturner$ print 'abc > df' abc df Alexanders-MacBook-Pro:~ alexanderturner$ print 1 2 34 5 1 2 34 5 Alexanders-MacBook-Pro:~ alexanderturner$ print {1..3} 1 2 3 Alexanders-MacBook-Pro:~ alexanderturner$ print "$(print {1..3} | tr ' ' '\n')" 1 2 3

oche, lik echo but a bit easier to use.

function oche { printf "%s\n" "$@"; } Alexanders-MacBook-Pro:~ alexanderturner$ oche "a b c" a b c Alexanders-MacBook-Pro:~ alexanderturner$ oche a b c a b c Alexanders-MacBook-Pro:~ alexanderturner$ thing=$(ls -l) Alexanders-MacBook-Pro:~ alexanderturner$ oche "$thing" total 98888 -rw-r--r--     1 alexanderturner  staff      1286 20 Mar  2016 #.emacs# drwxr-xr-x     4 alexanderturner  staff       136 23 Dec  2014 AWS drwxr-xr-x     6 alexanderturner  staff       204 22 May  2014 Accounts drwx------     4 alexanderturner  staff       136 25 Oct  2015 Applications   ...

Cost Of A SubShell

bash-3.2$ time (for x in {1..210000}; do : ; done) real    0m0.934s user    0m0.911s sys    0m0.020s bash-3.2$ time for x in {1..1000}; do (:) ; done real    0m0.967s user    0m0.309s sys    0m0.632s # The sys cost of forking 1000 times in 0.632 seconds just under a millisecond per subshell. # to create the subshell. This is a lot more than I would have expected for a naked fork so  # lots of other stuff is probably going on.

Subprocess without job control spew

# If you don't want to have [3] and [3]+ Done and all that stuff: ( (do_some_work_here; do_some_more)& ) # or ( just_do_some_work arg arg arg & ) # E.g. from bithon. # Write to a fd (which is a named pipe) all of the arguments # to the function then write a special end of stream statement. # Do all this in the background (to avoid pipe deadlock) and  # launch from a subshell to avoid job control spew. Remember # fork on Linux (or Mac OS) is < 1000 cycles... i.e. less than # one millionth of a second, so don't worry too much about this # approach from a performance point of view (though subshells  # are actually quite a bit more expensive). ( (echo "$@" >&3; echo 'print "\nbithon-eos"' >&3)& ) # Putting the background process in a subshell removes the job control spew.

Function which will accept stdin or arguments

function launch_python {   exec 3>&-   exec 4<&-   pyin=$(mktemp -u)   pyot=$(mktemp -u)   mkfifo $pyin   mkfifo $pyot   python -u -i <$pyin &>$pyot &   pypid=$!   exec 3> $pyin   exec 4< $pyot   rm $pyin   rm $pyot   echo print >&3   while read -u 4 line && [ "${line}" != '>>>' ]   do     echo  "# ${line}"   done   echo 'import sys;sys.ps2=""' >&3 } function py_read {   local line   while read -u 4 line && [ "${line}" != 'bithon-eos' ]   do     while [ "${line#>>> }" != "${line}" ]     do         line=${line#>>> }     done     [ ${line} == '>>>' ] && continue     echo "${line}"   done } function _ppy {   local line   IFS=''   while r...

Bithon Update 1

# Probably one of many, this is more bash style in the logic statements and better handling of eos. function launch_python {   exec 3>&-   exec 4<&-   pyin=$(mktemp -u)   pyot=$(mktemp -u)   mkfifo $pyin   mkfifo $pyot   python -u -i <$pyin &>$pyot &   pypid=$!   exec 3> $pyin   exec 4< $pyot   rm $pyin   rm $pyot   echo print >&3   while read -u 4 line && [ "${line}" != '>>>' ]   do     echo  "# ${line}"   done   echo 'import sys;sys.ps2=""' >&3 } function py_read {   local line   while read -u 4 line && [ "${line}" != 'bithon-eos' ]   do     [ "${line}" == '>>>' ] && continue     line="${line#>>> }"     [ "${line}" == '>>>' ] && continue     echo "${line#>>>}"   done } function...

Bithon: Run Python Interactively Inside Bash

cat > bithon.sh << EOF # This script allows python to run as child of bash # attached to anonymous pipes and all commands in # bash starting py are sent to python.   function launch_python {   # Close any existing use of these fds.   exec 3>&-   exec 4<&-   pyin=$(mktemp -u)   pyot=$(mktemp -u)   mkfifo $pyin   mkfifo $pyot   python -u -i <$pyin &>$pyot &   pypid=$!   exec 3> $pyin   exec 4< $pyot   # Make the pipes 'anonymous'.   rm $pyin   rm $pyot   echo print >&3   while read -u 4 line   do     if [ "${line}" == '>>>' ]     then       break     fi     echo  "# ${line}"   done   echo 'import sys;sys.ps2=""' >&3 } function py_read {   local line   while read -u 4 line   do     if [ "${line}" == '...

Local scope variables

# local makes variables local. function thing { local l=8;echo $l;} l=3 echo $l 3 thing 8 echo $l 3 ... # declare does exactly the same thing. function thing { declare l=8;echo $l ;} l=3 echo $l 3 thing 8 echo $l 3 ... # otherwise variables are global even inside functions. function thing { l=8;echo $l ;} l=3 echo $l 3 thing 8 echo $l 8

Pretty Print Percentiles Of Decimal Numeric List

function sort_numeric_array {     sort <(echo $@ | tr ' ' "\n") --numeric-sort | tr "\n" ' ' } dataA=($(rand 101 1000)) dataB=($(rand 101 1000000)) for idx in {0..100} do     data[$idx]="${dataA[idx]}.${dataB[idx]}" done data=($(sort_numeric_array ${data[@]})) len=${#data[@]} data[$len]=0 function pctiles {     echo scale=6     for idx in {0..10}     do         if [ $idx == 0 ]         then             place=0         else             place=$((((len-1)*idx*10)/10))         fi         second=$((place%10))         first=$((10-second))         place=$((place/10))         plus1=$((place+1))         if [ $plus1 -lt $len ]         then     ...

Simpler list to data

# Define a list. x='a d c f g' # Just expand the list inside parenthesis a=($x) # Here is the resulting array. echo ${a[@]} a d c f g echo ${a[1]} d

Create list of random numbers

# Echo $1 random numbers between zero and $2. e.g. echo $(rand 8 32) function rand {     declare -i rand_1     declare -i rand_2     rand_2=$(($1*4))     for rand_1 in $(od -vAn -N${rand_2} -tu4 < /dev/urandom); do echo $(($rand_1%$2));done } ... for example ... # See list_to_data in previous post. list_to_data $(rand 100 32) echo ${data[@]} 19 9 27 23 25 16 11 16 5 4 7 3 1 10 29 9 23 4 27 19 9 2 17 12 9 0 27 30 15 12 14 16 26 7 8 31 10 28 20 21 18 26 9 4 24 2 21 1 28 23 30 18 7 2 30 1 26 30 0 13 24 23 28 23 19 31 29 6 17 8 17 0 18 26 30 1 19 30 3 29 6 15 8 14 18 12 3 24 19 10 16 7 2 28 18 4 19 11 0 19

List to data

# Put the values in list into and array called data. function list_to_data {     declare ltd_d     declare -i ltd_idx     unset data     ltd_idx=0     for ltd_d in $@     do         data[$ltd_idx]=$ltd_d         ((ltd_idx++))     done     echo ${data[@]} } list_to_data a b c d e f echo ${data[@]} a b c d e f

Sort Numeric Array

# Treat $@ as a numeric array and echo it sorted. function sort_numeric_array {     sort <(echo $@ | tr ' ' "\n") --numeric-sort | tr "\n" ' ' } ... for x in {1..10}; do data[$x]=$(($x * $x * -1) echo $(sort_numeric_array ${data[@]}) -100 -81 -64 -49 -36 -25 -16 -9 -4 -1

Arrays Of Random Numbers

#!/bin/bash # Create $1 random numbers between zero and $2. e.g. echo $(rand 8 32) function rand {     rand_2=$(($1*4))    for rand_1 in $(od -vAn -N${rand_2} -tu4 < /dev/urandom); do echo $(($rand_1%$2));done } idx=0 declare -a data for x in $(rand 100 32) do     data[$idx]=$x     ((idx++)) done for idx in {0..99} do       echo $idx - ${data[idx]} done