Skip Navigation
shell

Welcome to share all things shell scripting (i.e. bash, zsh) here!

Members
98
Posts
3
Active Today
3
Created
2 yr. ago
  • shell @discuss.tchncs.de
    LibreMonk @linkage.ds8.zone

    Generating reference numbers in bash or sqlite that do not appear sequential (like shuffling a deck of cards)

    TL;DR → The main problem is coming up with a way to reorder an array non-randomly but without introducing bulky code. Like the effect of shuffling a deck of cards in a deterministic cheating way.


    Full background:

    I would like to generate reference numbers for letters sent via postal mail. An sqlite db is used to track the sequence numbers (but not the reference numbers). This is the bash code I have so far:

     undefined
        
    typeset -a symbolset=(a b c d e f g h   j k   m n   p q r s t u v w x y z     2 3 4 5 6 7 8 9)
    ln_symbolset=${#symbolset[@]}; # 41 is the answer, not 42
    itemseq=$(sqlite3 ltr_tracking.db "select max(counter) from $tbl;")
    printf '%s\n' "next letter reference number is: $(date +%Y)-${symbolset[$((itemseq / ln_symbolset))]}${symbolset[$((itemseq % ln_symbolset))]}"
    
      

    An array is defined with alphanumeric symbols, taking care to eliminate symbols that humans struggle to distinguish (e.g. 1l0o). Then integer div and mod operations produce a two character number which is the

  • shell @discuss.tchncs.de
    ode @discuss.tchncs.de

    Top 5 memory consuming tasks

    ps auxf | sort -nr -k 4 | head -5

    (As I shamelessly lifted this from DistroTube, I may as well go the whole hog and suggest you alias it to mem5)

  • shell @discuss.tchncs.de
    oxo @discuss.tchncs.de

    Neat lsblk

    lsblk -i --tree -o name,fstype,uuid,path,size,fsuse%,fsused,label,mountpoint #bash #code #snippet